Class: Zip::ZipInputStream
- Includes:
- IOExtras::AbstractInputStream
- Defined in:
- lib/zip/zip.rb
Overview
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.
Instance Attribute Summary
Attributes included from IOExtras::AbstractInputStream
Class Method Summary collapse
-
.open(filename) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
Instance Method Summary collapse
- #close ⇒ Object
- #eof ⇒ Object (also: #eof?)
-
#get_next_entry ⇒ Object
Returns a ZipEntry object.
-
#initialize(filename, offset = 0) ⇒ ZipInputStream
constructor
Opens the indicated zip file.
-
#rewind ⇒ Object
Rewinds the stream to the beginning of the current entry.
-
#sysread(numberOfBytes = nil, buf = nil) ⇒ Object
Modeled after IO.sysread.
Methods included from IOExtras::AbstractInputStream
#each_line, #flush, #gets, #read, #readline, #readlines
Methods included from IOExtras::FakeIO
Methods included from Enumerable
#deep_clone, #deep_dup, #inject, #select_map
Constructor Details
#initialize(filename, offset = 0) ⇒ ZipInputStream
Opens the indicated zip file. An exception is thrown if the specified offset in the specified filename is not a local zip entry header.
82 83 84 85 86 87 88 |
# File 'lib/zip/zip.rb', line 82 def initialize(filename, offset = 0) super() @archiveIO = File.open(filename, "rb") @archiveIO.seek(offset, IO::SEEK_SET) @decompressor = NullDecompressor.instance @currentEntry = nil end |
Class Method Details
.open(filename) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
97 98 99 100 101 102 103 104 |
# File 'lib/zip/zip.rb', line 97 def ZipInputStream.open(filename) return new(filename) unless block_given? zio = new(filename) yield zio ensure zio.close if zio end |
Instance Method Details
#close ⇒ Object
90 91 92 |
# File 'lib/zip/zip.rb', line 90 def close @archiveIO.close end |
#eof ⇒ Object Also known as: eof?
131 132 133 |
# File 'lib/zip/zip.rb', line 131 def eof @outputBuffer.empty? && @decompressor.eof end |
#get_next_entry ⇒ Object
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.
111 112 113 114 115 |
# File 'lib/zip/zip.rb', line 111 def get_next_entry @archiveIO.seek(@currentEntry.next_header_offset, IO::SEEK_SET) if @currentEntry open_entry end |
#rewind ⇒ Object
Rewinds the stream to the beginning of the current entry
118 119 120 121 122 123 124 |
# File 'lib/zip/zip.rb', line 118 def rewind return if @currentEntry.nil? @lineno = 0 @archiveIO.seek(@currentEntry.localHeaderOffset, IO::SEEK_SET) open_entry end |
#sysread(numberOfBytes = nil, buf = nil) ⇒ Object
Modeled after IO.sysread
127 128 129 |
# File 'lib/zip/zip.rb', line 127 def sysread(numberOfBytes = nil, buf = nil) @decompressor.sysread(numberOfBytes, buf) end |