From 84c208982f717f10f7133cdfc1f016607398858d Mon Sep 17 00:00:00 2001 From: Robert Haines Date: Wed, 4 Apr 2018 15:40:38 +0100 Subject: [PATCH] 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. --- lib/zip/file.rb | 10 ++++------ test/file_test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/zip/file.rb b/lib/zip/file.rb index c0a44207..b5b85eea 100644 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -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? diff --git a/test/file_test.rb b/test/file_test.rb index 53124bea..8bbf7cf8 100644 --- a/test/file_test.rb +++ b/test/file_test.rb @@ -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|