Method: Zip::File.open_buffer

Defined in:
lib/zip/file.rb

.open_buffer(io, options = {}) {|zf| ... } ⇒ Object

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.)

Yields:

  • (zf)

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zip/file.rb', line 119

def open_buffer(io, options = {})
  unless IO_METHODS.map { |method| io.respond_to?(method) }.all? || io.is_a?(String)
    raise "Zip::File.open_buffer expects a String or IO-like argument (responds to #{IO_METHODS.join(', ')}). Found: #{io.class}"
  end
  if io.is_a?(::String)
    require 'stringio'
    io = ::StringIO.new(io)
  elsif io.respond_to?(:binmode)
    # https://github.com/rubyzip/rubyzip/issues/119
    io.binmode
  end
  zf = ::Zip::File.new(io, true, true, options)
  zf.read_from_stream(io)
  return zf unless block_given?
  yield zf
  begin
    zf.write_buffer(io)
  rescue IOError => e
    raise unless e.message == 'not opened for writing'
  end
end