class Ronin::Support::Archive::Tar::Reader

Handling reading tar encoded archive data.

@see rubydoc.info/stdlib/rubygems/Gem/Package/TarReader

@api public

@since 1.0.0

Public Class Methods

new(io_or_buffer, mode: 'r', &block) click to toggle source

Initializes the tar writer.

@param [IO, StringIO, String] io_or_buffer

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.

@param [String] mode

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

@yield [tar]

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

@yieldparam [Reader] tar

The tar reader object.

@return [Reader]

The gzip reader object.

@example Initializing with an IO object:

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

@example Initializing with a buffer:

buffer = "..."
tar   = Archive::Tar::Reader.new(buffer)
Calls superclass method
# 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,&block) click to toggle source

Opens the tar archive file for reading.

@param [String] path

The path to the tar archive.

@yield [tar]

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

@yieldparam [Reader] tar

The newly created tar reader object.

@return [Reader]

If no block is given, than the tar reader object will be returned.
# 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

Public Instance Methods

[](name) click to toggle source

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

@param [String] name

The entry name to search for.

@return [Entry, nil]

The matching entry or `nil` if none could be found.
# File lib/ronin/support/archive/tar/reader.rb, line 109
def [](name)
  find { |entry| entry.full_name == name }
end
read(name, length: nil) click to toggle source

Reads the contents of an entry from the tar archive.

@param [String] name

The name of the entry to read.

@param [Integer, nil] length

Optional number of bytes to read.

@return [String]

The read data.
# File lib/ronin/support/archive/tar/reader.rb, line 125
def read(name, length: nil)
  if (entry = self[name])
    entry.read(length)
  end
end