class TarReader

This class reads a tar archive. The API is designed to be compatible with minitar package Archive::Tar::Minitar::Reader (www.rubydoc.info/gems/minitar/Archive/Tar/Minitar/Reader).

Extensions to minitar:

Public Class Methods

new(file) click to toggle source
# File lib/tarreader.rb, line 110
def initialize file
  if IO === file then
    @io = file
    @pos = nil
  elsif /\.t?gz$/ === file then
    require 'zlib'
    @io = Zlib::GzipReader.open(file)
    @pos = 0
    $stderr.puts "# TarReader#new GzipReader" if $DEBUG
  else
    @io = File.open(file, RDONLY|BINARY).set_encoding('BINARY')
    @pos = nil
    $stderr.puts "# TarReader#new File" if $DEBUG
  end
end
open(fnam) { |tar| ... } click to toggle source
# File lib/tarreader.rb, line 99
def TarReader::open fnam
  tar = TarReader.new(fnam)
  return tar unless block_given?
  begin
    yield tar
  ensure
    tar.close
  end
  fnam
end

Public Instance Methods

close() click to toggle source
# File lib/tarreader.rb, line 168
def close
  @io.close
end
each_entry() { |ent| ... } click to toggle source
# File lib/tarreader.rb, line 158
def each_entry
  while ent = gethdr
    begin
      yield ent
    ensure
      ent.end_entry
    end
  end
end
gethdr() click to toggle source
# File lib/tarreader.rb, line 152
def gethdr
  hdr = nil
  return hdr if catch(:TarReaderEof) { hdr = Entry.new(self) }
  hdr
end
pos() click to toggle source
# File lib/tarreader.rb, line 132
def pos
  @pos or @io.pos
end
pos=(ipos) click to toggle source
# File lib/tarreader.rb, line 136
def pos= ipos
  $stderr.puts "# TarReader#pos=(#{ipos})" if $DEBUG
  if @pos then
    return if @pos == ipos
    raise Errno::ESPIPE, "cannot seek backword #{@pos} #{ipos}" if ipos < @pos
    span = ipos - @pos
    # this is tuned at tako.toyoda-eizi.net January 2019.
    skipsize = 20 * 512
    (span / skipsize).times { @io.read(skipsize) }
    @io.read(span % skipsize)
    @pos = ipos
  else
    @io.pos = ipos
  end
end
read(size) click to toggle source
# File lib/tarreader.rb, line 126
def read size
  $stderr.puts "# TarReader#read(#{size})" if $DEBUG
  @pos += size if @pos
  @io.read(size)
end