8000 Fix #280 - `open_buffer` mangles the content of the buffer it is given. by hainesr · Pull Request #360 · rubyzip/rubyzip · GitHub
[go: up one dir, main page]

Skip to content

Fix #280 - open_buffer mangles the content of the buffer it is given. #360

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

Merged
merged 5 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Switch newly created StringIOs to binmode.
StringIO objects created within File.open_buffer were not being switched into
binmode, but those passed in were. Fix this inconsistency and add a test.
  • Loading branch information
hainesr committed Apr 4, 2018
commit 84c208982f717f10f7133cdfc1f016607398858d
10 changes: 4 additions & 6 deletions lib/zip/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ def open_buffer(io, options = {})
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)
io = ::StringIO.new(io)
elsif io.respond_to?(:binmode)
# https://github.com/rubyzip/rubyzip/issues/119
io.binmode
end
io = ::StringIO.new(io) if io.is_a?(::String)

# https://github.com/rubyzip/rubyzip/issues/119
io.binmode if io.respond_to?(:binmode)

zf = ::Zip::File.new(io, true, true, options)
return zf unless block_given?
Expand Down
7 changes: 7 additions & 0 deletions test/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def test_get_output_stream
end
end

def test_open_buffer_with_string
string = File.read('test/data/rubycode.zip')
::Zip::File.open_buffer string do |zf|
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
end
end

def test_open_buffer_with_stringio
string_io = StringIO.new File.read('test/data/rubycode.zip')
::Zip::File.open_buffer string_io do |zf|
Expand Down
0