Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Zip::ZipFile.open_buffer #49

Merged
merged 1 commit into from May 21, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/zip/zip_file.rb
Expand Up @@ -103,6 +103,24 @@ def add_buffer
zf.write_buffer
end

# Like #open, but reads zip archive contents from a String or open IO
# stream, and outputs data to a buffer.
# (This can be used to extract data from a
# downloaded zip archive without first saving it to disk.)
def open_buffer(io)
zf = ZipFile.new('',true,true)
if io.is_a? IO
zf.read_from_stream(io)
elsif io.is_a? String
require 'stringio'
zf.read_from_stream(StringIO.new(io))
else
raise "Zip::ZipFile.open_buffer expects an argument of class String or IO. Found: #{io.class}"
end
yield zf
zf.write_buffer
end

# Iterates over the contents of the ZipFile. This is more efficient
# than using a ZipInputStream since this methods simply iterates
# through the entries in the central directory structure in the archive
Expand Down