Remove `File::add_buffer` from the API.

Its functionality is now replicated in `File::open_buffer` but in a more
secure way.
This commit is contained in:
Robert Haines 2022-01-22 07:34:00 +00:00
parent 044759f502
commit e2e0e23763
2 changed files with 3 additions and 11 deletions

View File

@ -126,19 +126,11 @@ module Zip
end
end
# Same as #open. But outputs data to a buffer instead of a file
def add_buffer
io = ::StringIO.new
zf = ::Zip::File.new(io, create: true, buffer: true)
yield zf
zf.write_buffer(io)
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, **options)
def open_buffer(io = ::StringIO.new, create: false, **options)
unless IO_METHODS.map { |method| io.respond_to?(method) }.all? || io.kind_of?(String)
raise 'Zip::File.open_buffer expects a String or IO-like argument' \
"(responds to #{IO_METHODS.join(', ')}). Found: #{io.class}"
@ -149,7 +141,7 @@ module Zip
# https://github.com/rubyzip/rubyzip/issues/119
io.binmode if io.respond_to?(:binmode)
zf = ::Zip::File.new(io, create: true, buffer: true, **options)
zf = ::Zip::File.new(io, create: create, buffer: true, **options)
return zf unless block_given?
yield zf

View File

@ -16,7 +16,7 @@ class ZipFileTest < MiniTest::Test
def test_create_from_scratch_to_buffer
comment = 'a short comment'
buffer = ::Zip::File.add_buffer do |zf|
buffer = ::Zip::File.open_buffer(create: true) do |zf|
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
zf.mkdir('dir1')
zf.comment = comment