Permalink
Browse files

added ZipInputStream::open_buffer

  • Loading branch information...
skoji committed Jan 26, 2012
1 parent b269f0e commit 1625f76265c3e1f8f0dc35809db5c2b677088461
Showing with 28 additions and 3 deletions.
  1. +15 −3 lib/zip/zip_input_stream.rb
  2. +13 −0 test/ziptest.rb
@@ -46,10 +46,14 @@ class 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.
def initialize(filename, offset = 0)
def initialize(filename, offset = 0, io = nil)
super()
@archiveIO = File.open(filename, "rb")
@archiveIO.seek(offset, IO::SEEK_SET)
if (io.nil?)
@archiveIO = File.open(filename, "rb")
@archiveIO.seek(offset, IO::SEEK_SET)
else
@archiveIO = io
end
@decompressor = NullDecompressor.instance
@currentEntry = nil
end
@@ -70,6 +74,14 @@ def ZipInputStream.open(filename)
zio.close if zio
end
def ZipInputStream.open_buffer(io)
return new('',0,io) unless block_given?
zio = new('',0,io)
yield zio
ensure
zio.close if zio
end
# 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
View
@@ -436,6 +436,19 @@ def test_openWithBlock
end
def test_openWithoutBlock
zis = ZipInputStream.open_buffer(File.new(TestZipFile::TEST_ZIP2.zip_name, "rb"))
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
end
def test_openBufferWithBlock
ZipInputStream.open_buffer(File.new(TestZipFile::TEST_ZIP2.zip_name, "rb")) {
|zis|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
}
end
def test_openBufferWithoutBlock
zis = ZipInputStream.open(TestZipFile::TEST_ZIP2.zip_name)
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
end

0 comments on commit 1625f76

Please sign in to comment.