class Archive::Tar::Minitar::Reader

The class that reads a tar format archive from a data stream. The data stream may be sequential or random access, but certain features only work with random access data streams.

Public Class Methods

new(anIO) click to toggle source

Creates and returns a new Reader object.

    # File lib/archive/tar/minitar.rb
580 def initialize(anIO)
581   @io     = anIO
582   @init_pos = anIO.pos
583 end
open(anIO) { |reader| ... } click to toggle source

With no associated block, Reader::open is a synonym for Reader::new. If the optional code block is given, it will be passed the new writer as an argument and the Reader object will automatically be closed when the block terminates. In this instance, Reader::open returns the value of the block.

    # File lib/archive/tar/minitar.rb
565 def self.open(anIO)
566   reader = Reader.new(anIO)
567 
568   return reader unless block_given?
569 
570   begin
571     res = yield reader
572   ensure
573     reader.close
574   end
575   
576   res
577 end

Public Instance Methods

close() click to toggle source
    # File lib/archive/tar/minitar.rb
635 def close
636 end
each(&block) click to toggle source

Iterates through each entry in the data stream.

    # File lib/archive/tar/minitar.rb
586 def each(&block)
587   each_entry(&block)
588 end
each_entry() { |entry| ... } click to toggle source

Iterates through each entry in the data stream.

    # File lib/archive/tar/minitar.rb
604 def each_entry
605   loop do
606     return if @io.eof?
607 
608     header = Archive::Tar::PosixHeader.new_from_stream(@io)
609     return if header.empty?
610 
611     entry = EntryStream.new(header, @io)
612     size  = entry.size
613 
614     yield entry
615 
616     skip = (512 - (size % 512)) % 512
617 
618     if @io.respond_to?(:seek)
619         # avoid reading...
620       @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
621     else
622       pending = size - entry.bytes_read
623       while pending > 0
624         bread = @io.read([pending, 4096].min).size
625         raise UnexpectedEOF if @io.eof?
626         pending -= bread
627       end
628     end
629     @io.read(skip) # discard trailing zeros
630       # make sure nobody can use #read, #getc or #rewind anymore
631     entry.close
632   end
633 end
rewind() click to toggle source

Resets the read pointer to the beginning of data stream. Do not call this during a each or each_entry iteration. This only works with random access data streams that respond to rewind and pos.

    # File lib/archive/tar/minitar.rb
593 def rewind
594   if @init_pos == 0
595     raise NonSeekableStream unless @io.respond_to?(:rewind)
596     @io.rewind
597   else
598     raise NonSeekableStream unless @io.respond_to?(:pos=)
599     @io.pos = @init_pos
600   end
601 end