class GZippedTar::Tar::Reader

Attributes

io[R]

Public Class Methods

new(io) { |reader| ... } click to toggle source

Creates a new TarReader on io and yields it to the block, if given.

Calls superclass method
# File lib/gzipped_tar/tar/reader.rb, line 16
def self.new(io)
  reader = super

  return reader unless block_given?

  begin
    yield reader
  ensure
    reader.close
  end

  nil
end
new(io) click to toggle source

Creates a new tar file reader on io which needs to respond to pos, eof?, read, getc and pos=

# File lib/gzipped_tar/tar/reader.rb, line 32
def initialize(io)
  @io       = io
  @init_pos = io.pos
end

Public Instance Methods

close() click to toggle source

Close the tar file

# File lib/gzipped_tar/tar/reader.rb, line 38
def close; end
each() { |entry| ... } click to toggle source

Iterates over files in the tarball yielding each entry

# File lib/gzipped_tar/tar/reader.rb, line 41
def each
  return enum_for __method__ unless block_given?

  until @io.eof?
    header = GZippedTar::Tar::Header.from @io
    return if header.empty?
    entry = GZippedTar::Tar::Entry.new header, @io

    yield entry

    skip_past_entry header.size, entry.bytes_read
    skip_past_trailing header.size

    # make sure nobody can use #read, #getc or #rewind anymore
    entry.close
  end
end
Also aliased as: each_entry
each_entry()
Alias for: each
rewind() click to toggle source

NOTE: Do not call rewind during each

# File lib/gzipped_tar/tar/reader.rb, line 62
def rewind
  if @init_pos.zero?
    raise GZippedTar::Tar::NonSeekableIO unless @io.respond_to? :rewind
    @io.rewind
  else
    raise GZippedTar::Tar::NonSeekableIO unless @io.respond_to? :pos=
    @io.pos = @init_pos
  end
end
seek(name) { |found| ... } click to toggle source

Seeks through the tar file until it finds the entry with name and yields it. Rewinds the tar file to the beginning when the block terminates.

# File lib/gzipped_tar/tar/reader.rb, line 75
def seek(name)
  found = find do |entry|
    entry.full_name == name
  end

  return unless found

  return yield found
ensure
  rewind
end

Private Instance Methods

skip_by_read(pending) click to toggle source
# File lib/gzipped_tar/tar/reader.rb, line 100
def skip_by_read(pending)
  while pending > 0
    bytes_read = io.read([pending, 4096].min).size
    pending -= bytes_read
    raise GZippedTar::Tar::UnexpectedEOF if io.eof? && !pending.zero?
  end
end
skip_by_seek() click to toggle source
# File lib/gzipped_tar/tar/reader.rb, line 108
def skip_by_seek
  # avoid reading...
  io.seek pending, IO::SEEK_CUR
end
skip_past_entry(size, read) click to toggle source
# File lib/gzipped_tar/tar/reader.rb, line 91
def skip_past_entry(size, read)
  # skip over the rest of the entry
  pending = size - read

  skip_by_seek
rescue Errno::EINVAL, NameError
  skip_by_read pending
end
skip_past_trailing(size) click to toggle source
# File lib/gzipped_tar/tar/reader.rb, line 113
def skip_past_trailing(size)
  # discard trailing zeros
  skip = (512 - (size % 512)) % 512

  skip_by_read skip
end