Class: Zip::ZipInputStream

### Home Classes Methods

### In Files

- lib/zip/zip_input_stream.rb

### Parent

Object

### Methods

- ::new
- ::open
- ::open_buffer
- #close
- #eof
- #eof?
- #get_next_entry
- #input_finished?
- #open_entry
- #produce_input
- #rewind
- #sysread

### Included Modules

- IOExtras::AbstractInputStream

### Class/Module Index

Quicksearch

- Zip
- Zip::ZipCentralDirectory
- Zip::ZipCompressionMethodError
- Zip::ZipDestinationFileExistsError
- Zip::ZipEntry
- Zip::ZipEntryExistsError
- Zip::ZipEntryNameError
- Zip::ZipError
- Zip::ZipExtraField
- Zip::ZipExtraField::Generic
- Zip::ZipExtraField::IUnix
- Zip::ZipExtraField::UniversalTime
- Zip::ZipFile
- Zip::ZipFileSystem
- Zip::ZipFileSystem::ZipFsDir
- Zip::ZipFileSystem::ZipFsFile
- Zip::ZipFileSystem::ZipFsFile::ZipFsStat
- Zip::ZipInputStream
- Zip::ZipInternalError
- Zip::ZipOutputStream
- Zip::ZipStreamableDirectory
- Zip::ZipStreamableStream
- IOExtras
- IOExtras::AbstractInputStream
- IOExtras::AbstractOutputStream
- IOExtras::FakeIO
- BugFix
- Object

No matching classes.

# Zip::ZipInputStream

ZipInputStream is the basic class for reading zip entries in a zip file. It is possible to create a ZipInputStream object directly, passing the zip file name to the constructor, but more often than not the ZipInputStream will be obtained from a ZipFile (perhaps using the ZipFileSystem interface) object for a particular entry in the zip archive.

A ZipInputStream inherits IOExtras::AbstractInputStream in order to provide an IO-like interface for reading from a single zip entry. Beyond methods for mimicking an IO-object it contains the method get_next_entry for iterating through the entries of an archive. get_next_entry returns a ZipEntry object that describes the zip entry the ZipInputStream is currently reading from.

Example that creates a zip archive with ZipOutputStream and reads it back again with a ZipInputStream.

    require 'zip/zip'

    Zip::ZipOutputStream::open("my.zip") {
      |io|

      io.put_next_entry("first_entry.txt")
      io.write "Hello world!"

      io.put_next_entry("adir/first_entry.txt")
      io.write "Hello again!"
    }

    Zip::ZipInputStream::open("my.zip") {
      |io|

      while (entry = io.get_next_entry)
        puts "Contents of #{entry.name}: '#{io.read}'"
      end
    }

java.util.zip.ZipInputStream is the original inspiration for this class.

### Public Class Methods

new(filename, offset = 0, io = nil) click to toggle source

Opens the indicated zip file. An exception is thrown if the specified offset in the specified filename is not a local zip entry header.

    # File lib/zip/zip_input_stream.rb, line 49
    def initialize(filename, offset = 0, io = nil)
      super()
      if (io.nil?)
        @archiveIO = ::File.open(filename, "rb")
        @archiveIO.seek(offset, IO::SEEK_SET)
      else
        @archiveIO = io
      end
      @decompressor = NullDecompressor.instance
      @currentEntry = nil
    end

open(filename) click to toggle source

Same as initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.

    # File lib/zip/zip_input_stream.rb, line 68
    def ZipInputStream.open(filename)
      return new(filename) unless block_given?

      zio = new(filename)
      yield zio
    ensure
      zio.close if zio
    end

open_buffer(io) click to toggle source

    # File lib/zip/zip_input_stream.rb, line 77
    def ZipInputStream.open_buffer(io)
      return new('',0,io) unless block_given?
      zio = new('',0,io)
      yield zio
    ensure
      zio.close if zio
    end

### Public Instance Methods

close() click to toggle source

    # File lib/zip/zip_input_stream.rb, line 61
    def close
      @archiveIO.close
    end

eof() click to toggle source

    # File lib/zip/zip_input_stream.rb, line 109
    def eof
      @outputBuffer.empty? && @decompressor.eof
    end

Also aliased as: eof?
eof?() click to toggle source
Alias for: eof
get_next_entry() click to toggle source

Returns a ZipEntry object. It is necessary to call this method on a newly created ZipInputStream before reading from the first entry in the archive. Returns nil when there are no more entries.

    # File lib/zip/zip_input_stream.rb, line 90
    def get_next_entry
      @archiveIO.seek(@currentEntry.next_header_offset, IO::SEEK_SET) if @currentEntry
      open_entry
    end

rewind() click to toggle source

Rewinds the stream to the beginning of the current entry

    # File lib/zip/zip_input_stream.rb, line 96
    def rewind
      return if @currentEntry.nil?
      @lineno = 0
      @archiveIO.seek(@currentEntry.localHeaderOffset,
                      IO::SEEK_SET)
      open_entry
    end

sysread(numberOfBytes = nil, buf = nil) click to toggle source

Modeled after IO.sysread

    # File lib/zip/zip_input_stream.rb, line 105
    def sysread(numberOfBytes = nil, buf = nil)
      @decompressor.sysread(numberOfBytes, buf)
    end

### Protected Instance Methods

input_finished?() click to toggle source

    # File lib/zip/zip_input_stream.rb, line 136
    def input_finished?
      @decompressor.input_finished?
    end

open_entry() click to toggle source

    # File lib/zip/zip_input_stream.rb, line 116
    def open_entry
      @currentEntry = ZipEntry.read_local_entry(@archiveIO)
      if @currentEntry.nil?
              @decompressor = NullDecompressor.instance
      elsif @currentEntry.compression_method == ZipEntry::STORED
              @decompressor = PassThruDecompressor.new(@archiveIO, @currentEntry.size)
      elsif @currentEntry.compression_method == ZipEntry::DEFLATED
              @decompressor = Inflater.new(@archiveIO)
      else
              raise ZipCompressionMethodError,
              "Unsupported compression method #{@currentEntry.compression_method}"
      end
      flush
      return @currentEntry
    end

produce_input() click to toggle source

    # File lib/zip/zip_input_stream.rb, line 132
    def produce_input
      @decompressor.produce_input
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.