Class: Zip::InputStream
- Inherits:
-
Object
- Object
- Zip::InputStream
- Includes:
- Zip::IOExtras::AbstractInputStream
- Defined in:
- lib/zip/input_stream.rb
Overview
InputStream is the basic class for reading zip entries in a zip file. It is possible to create a InputStream object directly, passing the zip file name to the constructor, but more often than not the InputStream will be obtained from a File (perhaps using the ZipFileSystem interface) object for a particular entry in the zip archive.
A InputStream 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 Entry object that describes the zip entry the InputStream is currently reading from.
Example that creates a zip archive with ZipOutputStream and reads it back again with a InputStream.
require 'zip'
Zip::OutputStream.open("my.zip") do |io|
io.put_next_entry("first_entry.txt")
io.write "Hello world!"
io.put_next_entry("adir/first_entry.txt")
io.write "Hello again!"
end
Zip::InputStream.open("my.zip") do |io|
while (entry = io.get_next_entry)
puts "Contents of #{entry.name}: '#{io.read}'"
end
end
java.util.zip.ZipInputStream is the original inspiration for this class.
Instance Attribute Summary
Attributes included from Zip::IOExtras::AbstractInputStream
Class Method Summary collapse
-
.open(filename_or_io, offset = 0) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
- .open_buffer(filename_or_io, offset = 0) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #eof ⇒ Object (also: #eof?)
-
#get_next_entry ⇒ Object
Returns a Entry object.
-
#initialize(context, offset = 0) ⇒ InputStream
constructor
Opens the indicated zip file.
-
#rewind ⇒ Object
Rewinds the stream to the beginning of the current entry.
-
#sysread(number_of_bytes = nil, buf = nil) ⇒ Object
Modeled after IO.sysread.
Methods included from Zip::IOExtras::AbstractInputStream
#each_line, #flush, #gets, #read, #readline, #readlines, #ungetc
Methods included from Zip::IOExtras::FakeIO
Constructor Details
#initialize(context, offset = 0) ⇒ InputStream
Opens the indicated zip file. An exception is thrown if the specified offset in the specified filename is not a local zip entry header.
50 51 52 53 54 55 |
# File 'lib/zip/input_stream.rb', line 50 def initialize(context, offset = 0) super() @archive_io = get_io(context, offset) @decompressor = ::Zip::NullDecompressor @current_entry = nil end |
Class Method Details
.open(filename_or_io, offset = 0) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
94 95 96 97 98 99 100 101 102 |
# File 'lib/zip/input_stream.rb', line 94 def open(filename_or_io, offset = 0) zio = self.new(filename_or_io, offset) return zio unless block_given? begin yield zio ensure zio.close if zio end end |
.open_buffer(filename_or_io, offset = 0) ⇒ Object
104 105 106 107 |
# File 'lib/zip/input_stream.rb', line 104 def open_buffer(filename_or_io, offset = 0) puts "open_buffer is deprecated!!! Use open instead!" self.open(filename_or_io, offset) end |
Instance Method Details
#close ⇒ Object
57 58 59 |
# File 'lib/zip/input_stream.rb', line 57 def close @archive_io.close end |
#eof ⇒ Object Also known as: eof?
84 85 86 |
# File 'lib/zip/input_stream.rb', line 84 def eof @output_buffer.empty? && @decompressor.eof end |
#get_next_entry ⇒ Object
Returns a Entry object. It is necessary to call this method on a newly created InputStream before reading from the first entry in the archive. Returns nil when there are no more entries.
65 66 67 68 |
# File 'lib/zip/input_stream.rb', line 65 def get_next_entry @archive_io.seek(@current_entry.next_header_offset, IO::SEEK_SET) if @current_entry open_entry end |
#rewind ⇒ Object
Rewinds the stream to the beginning of the current entry
71 72 73 74 75 76 77 |
# File 'lib/zip/input_stream.rb', line 71 def rewind return if @current_entry.nil? @lineno = 0 @pos = 0 @archive_io.seek(@current_entry.local_header_offset, IO::SEEK_SET) open_entry end |
#sysread(number_of_bytes = nil, buf = nil) ⇒ Object
Modeled after IO.sysread
80 81 82 |
# File 'lib/zip/input_stream.rb', line 80 def sysread(number_of_bytes = nil, buf = nil) @decompressor.sysread(number_of_bytes, buf) end |