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
586 def initialize(anIO)
587   @io     = anIO
588   @init_pos = anIO.pos
589 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
571 def self.open(anIO)
572   reader = Reader.new(anIO)
573 
574   return reader unless block_given?
575 
576   begin
577     res = yield reader
578   ensure
579     reader.close
580   end
581   
582   res
583 end

Public Instance Methods

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

Iterates through each entry in the data stream.

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

Iterates through each entry in the data stream.

    # File lib/archive/tar/minitar.rb
610 def each_entry
611   loop do
612     return if @io.eof?
613 
614     header = Archive::Tar::PosixHeader.new_from_stream(@io)
615     return if header.empty?
616 
617     entry = EntryStream.new(header, @io)
618     size  = entry.size
619 
620     yield entry
621 
622     skip = (512 - (size % 512)) % 512
623 
624     if @io.respond_to?(:seek)
625         # avoid reading...
626       @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
627     else
628       pending = size - entry.bytes_read
629       while pending > 0
630         bread = @io.read([pending, 4096].min).size
631         raise UnexpectedEOF if @io.eof?
632         pending -= bread
633       end
634     end
635     @io.read(skip) # discard trailing zeros
636       # make sure nobody can use #read, #getc or #rewind anymore
637     entry.close
638   end
639 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
599 def rewind
600   if @init_pos == 0
601     raise NonSeekableStream unless @io.respond_to?(:rewind)
602     @io.rewind
603   else
604     raise NonSeekableStream unless @io.respond_to?(:pos=)
605     @io.pos = @init_pos
606   end
607 end