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

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.

Parameters:

  • output_path (String)

    path to the output tar file

  • include (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to include

  • exclude (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to exclude

  • newer_than (Time, nil) (defaults to: nil)

    only add files modified after this time

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

Yields:

  • (Writer)

    the writer for adding files



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).

Parameters:

  • output_path (String)

    path to the output .tar.gz file

  • include (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to include

  • exclude (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to exclude

  • newer_than (Time, nil) (defaults to: nil)

    only add files modified after this time

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

Yields:

  • (Writer)

    the writer for adding files



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.

Parameters:

  • input_path (String)

    path to the tar file

  • to (String)

    destination directory

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

Raises:



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.

Parameters:

  • input_path (String)

    path to the .tar.gz file

  • to (String)

    destination directory

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

Raises:



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.

Parameters:

  • input_path (String)

    path to the tar file

  • name (String)

    entry name to search for

Returns:

  • (String, nil)

    entry content or nil if not found



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.

Parameters:

  • input_path (String)

    path to the .tar.gz file

  • name (String)

    entry name to search for

Returns:

  • (String, nil)

    entry content or nil if not found



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.

Parameters:

  • input_path (String)

    path to the tar file

Returns:

  • (Array<Hash>)

    entry info hashes with :name, :size, :mode, :typeflag, :linkname keys



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.

Parameters:

  • input_path (String)

    path to the .tar.gz file

Returns:

  • (Array<Hash>)

    entry info hashes with :name, :size, :mode, :typeflag, :linkname keys



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