Module: Philiprehberger::Tar
- Defined in:
- lib/philiprehberger/tar.rb,
lib/philiprehberger/tar/reader.rb,
lib/philiprehberger/tar/writer.rb,
lib/philiprehberger/tar/version.rb
Defined Under Namespace
Classes: Error, FilteredWriter, ProgressWriter, Reader, Writer
Constant Summary collapse
- VERSION =
'0.4.0'
Class Method Summary collapse
-
.create(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil) {|Writer| ... } ⇒ void
Create a tar archive.
-
.create_gz(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil) {|Writer| ... } ⇒ void
Create a gzip-compressed tar archive (.tar.gz).
-
.extract(input_path, to:, on_progress: nil) ⇒ void
Extract a tar archive to a directory.
-
.extract_gz(input_path, to:, on_progress: nil) ⇒ void
Extract a gzip-compressed tar archive (.tar.gz) to a directory.
-
.find_entry(input_path, name) ⇒ String?
Find an entry by name in a tar archive and return its content.
-
.find_entry_gz(input_path, name) ⇒ String?
Find an entry by name in a gzip-compressed tar archive and return its content.
-
.list(input_path) ⇒ Array<Hash>
List entries in a tar archive.
-
.list_gz(input_path) ⇒ Array<Hash>
List entries in a gzip-compressed tar archive.
Class Method Details
.create(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil) {|Writer| ... } ⇒ void
This method returns an undefined value.
Create a tar archive.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/philiprehberger/tar.rb', line 21 def self.create(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil, &block) File.open(output_path, 'wb') do |io| writer = Writer.new(io) if include || exclude || newer_than filtered_writer = FilteredWriter.new(writer, include: include, exclude: exclude, newer_than: newer_than, on_progress: on_progress) block.call(filtered_writer) else block.call(on_progress ? ProgressWriter.new(writer, on_progress: on_progress) : writer) end writer.close end end |
.create_gz(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil) {|Writer| ... } ⇒ void
This method returns an undefined value.
Create a gzip-compressed tar archive (.tar.gz).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/philiprehberger/tar.rb', line 44 def self.create_gz(output_path, include: nil, exclude: nil, newer_than: nil, on_progress: nil, &block) File.open(output_path, 'wb') do |file_io| gz = Zlib::GzipWriter.new(file_io) writer = Writer.new(gz) if include || exclude || newer_than filtered_writer = FilteredWriter.new(writer, include: include, exclude: exclude, newer_than: newer_than, on_progress: on_progress) block.call(filtered_writer) else block.call(on_progress ? ProgressWriter.new(writer, on_progress: on_progress) : writer) end writer.close gz.close end end |
.extract(input_path, to:, on_progress: nil) ⇒ void
This method returns an undefined value.
Extract a tar archive to a directory.
66 67 68 69 70 71 72 |
# File 'lib/philiprehberger/tar.rb', line 66 def self.extract(input_path, to:, on_progress: nil) raise Error, "directory does not exist: #{to}" unless Dir.exist?(to) File.open(input_path, 'rb') do |io| extract_from_io(io, to: to, on_progress: on_progress) end end |
.extract_gz(input_path, to:, on_progress: nil) ⇒ void
This method returns an undefined value.
Extract a gzip-compressed tar archive (.tar.gz) to a directory.
80 81 82 83 84 85 86 87 88 |
# File 'lib/philiprehberger/tar.rb', line 80 def self.extract_gz(input_path, to:, on_progress: nil) raise Error, "directory does not exist: #{to}" unless Dir.exist?(to) File.open(input_path, 'rb') do |file_io| gz = Zlib::GzipReader.new(file_io) extract_from_io(gz, to: to, on_progress: on_progress) gz.close end end |
.find_entry(input_path, name) ⇒ String?
Find an entry by name in a tar archive and return its content.
118 119 120 121 122 |
# File 'lib/philiprehberger/tar.rb', line 118 def self.find_entry(input_path, name) File.open(input_path, 'rb') do |io| find_entry_in_io(io, name) end end |
.find_entry_gz(input_path, name) ⇒ String?
Find an entry by name in a gzip-compressed tar archive and return its content.
129 130 131 132 133 134 135 136 |
# File 'lib/philiprehberger/tar.rb', line 129 def self.find_entry_gz(input_path, name) File.open(input_path, 'rb') do |file_io| gz = Zlib::GzipReader.new(file_io) result = find_entry_in_io(gz, name) gz.close result end end |
.list(input_path) ⇒ Array<Hash>
List entries in a tar archive.
94 95 96 97 98 |
# File 'lib/philiprehberger/tar.rb', line 94 def self.list(input_path) File.open(input_path, 'rb') do |io| Reader.new(io).list end end |
.list_gz(input_path) ⇒ Array<Hash>
List entries in a gzip-compressed tar archive.
104 105 106 107 108 109 110 111 |
# File 'lib/philiprehberger/tar.rb', line 104 def self.list_gz(input_path) File.open(input_path, 'rb') do |file_io| gz = Zlib::GzipReader.new(file_io) entries = Reader.new(gz).list gz.close entries end end |