Class: Ronin::Support::Archive::Tar::Reader

Inherits:
Gem::Package::TarReader
  • Object
show all
Defined in:
lib/ronin/support/archive/tar/reader.rb

Overview

Handling reading tar encoded archive data.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(io_or_buffer, mode: 'r') {|tar| ... } ⇒ Reader

Initializes the tar writer.

Examples:

Initializing with an IO object:

tar = Archive::Tar::Reader.new(io)

Initializing with a buffer:

buffer = "..."
tar   = Archive::Tar::Reader.new(buffer)

Parameters:

  • io_or_buffer (IO, StringIO, String)

    The IO object or buffer to read from. If a String is given, then it will be wrapped in a StringIO object using the optional mode argument.

  • mode (String) (defaults to: 'r')

    The optional mode to initialize the StringIO object to wrap around the given buffer String.

Yields:

  • (tar)

    If a block is given, it will be passed the new tar reader object.

Yield Parameters:

  • tar (Reader)

    The tar reader object.

Returns:

  • (Reader)

    The gzip reader object.

Since:

  • 1.0.0



65
66
67
68
69
70
71
72
# File 'lib/ronin/support/archive/tar/reader.rb', line 65

def self.new(io_or_buffer, mode: 'r', &block)
  io = case io_or_buffer
       when String then StringIO.new(io_or_buffer,mode)
       else             io_or_buffer
       end

  return super(io,&block)
end

.open(path) {|tar| ... } ⇒ Reader

Opens the tar archive file for reading.

Parameters:

  • path (String)

    The path to the tar archive.

Yields:

  • (tar)

    If a block is given, then it will be passed the new tar reader object.

Yield Parameters:

  • tar (Reader)

    The newly created tar reader object.

Returns:

  • (Reader)

    If no block is given, than the tar reader object will be returned.

Since:

  • 1.0.0



90
91
92
93
94
95
96
97
98
# File 'lib/ronin/support/archive/tar/reader.rb', line 90

def self.open(path,&block)
  if block
    File.open(path,'rb') do |file|
      new(file,&block)
    end
  else
    new(File.new(path,'rb'))
  end
end

Instance Method Details

#[](name) ⇒ Entry?

Finds an entry in the tar archive with the matching name.

Parameters:

  • name (String)

    The entry name to search for.

Returns:

  • (Entry, nil)

    The matching entry or nil if none could be found.

Since:

  • 1.0.0



109
110
111
# File 'lib/ronin/support/archive/tar/reader.rb', line 109

def [](name)
  find { |entry| entry.full_name == name }
end

#read(name, length: nil) ⇒ String

Reads the contents of an entry from the tar archive.

Parameters:

  • name (String)

    The name of the entry to read.

  • length (Integer, nil) (defaults to: nil)

    Optional number of bytes to read.

Returns:

Since:

  • 1.0.0



125
126
127
128
129
# File 'lib/ronin/support/archive/tar/reader.rb', line 125

def read(name, length: nil)
  if (entry = self[name])
    entry.read(length)
  end
end