From b0ee2683b0e89e46f73f589057562e2f2d6e29d7 Mon Sep 17 00:00:00 2001 From: Sebastian Henke Date: Wed, 19 Feb 2020 18:48:13 +0100 Subject: [PATCH 1/3] Set buffers to binmode by default --- lib/zip/file.rb | 1 + lib/zip/output_stream.rb | 1 + test/output_stream_test.rb | 11 +++++++++++ 3 files changed, 13 insertions(+) diff --git a/lib/zip/file.rb b/lib/zip/file.rb index c768d07d..5aafe2e1 100644 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -362,6 +362,7 @@ def commit # Write buffer write changes to buffer and return def write_buffer(io = ::StringIO.new('')) + io.binmode if io.respond_to?(:binmode) ::Zip::OutputStream.write_buffer(io) do |zos| @entry_set.each { |e| e.write_to_zip_output_stream(zos) } zos.comment = comment diff --git a/lib/zip/output_stream.rb b/lib/zip/output_stream.rb index 2f628931..266083cd 100644 --- a/lib/zip/output_stream.rb +++ b/lib/zip/output_stream.rb @@ -58,6 +58,7 @@ def open(file_name, encrypter = nil) # Same as #open but writes to a filestream instead def write_buffer(io = ::StringIO.new(''), encrypter = nil) + io.binmode if io.respond_to?(:binmode) zos = new(io, true, encrypter) yield zos zos.close_buffer diff --git a/test/output_stream_test.rb b/test/output_stream_test.rb index 40205076..e25fb84a 100644 --- a/test/output_stream_test.rb +++ b/test/output_stream_test.rb @@ -6,6 +6,8 @@ class ZipOutputStreamTest < MiniTest::Test TEST_ZIP = TestZipFile::TEST_ZIP2.clone TEST_ZIP.zip_name = 'test/data/generated/output.zip' + ASCII8BIT = 'ASCII-8BIT' + def test_new zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name) zos.comment = TEST_ZIP.comment @@ -32,6 +34,15 @@ def test_write_buffer assert_test_zip_contents(TEST_ZIP) end + def test_write_buffer_binmode + io = ::StringIO.new('') + buffer = ::Zip::OutputStream.write_buffer(io) do |zos| + zos.comment = TEST_ZIP.comment + write_test_zip(zos) + end + assert buffer.external_encoding.name === ASCII8BIT + end + def test_write_buffer_with_temp_file tmp_file = Tempfile.new('') From e33c07a6e757eed653b56b045b8a0ecff40c1533 Mon Sep 17 00:00:00 2001 From: Sebastian Henke Date: Mon, 2 Mar 2020 11:00:39 +0100 Subject: [PATCH 2/3] Use existing constant for ASCII_8BIT Co-Authored-By: John Lees-Miller --- test/output_stream_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/output_stream_test.rb b/test/output_stream_test.rb index e25fb84a..61c9ac37 100644 --- a/test/output_stream_test.rb +++ b/test/output_stream_test.rb @@ -40,7 +40,7 @@ def test_write_buffer_binmode zos.comment = TEST_ZIP.comment write_test_zip(zos) end - assert buffer.external_encoding.name === ASCII8BIT + assert_equal Encoding::ASCII_8BIT, buffer.external_encoding end def test_write_buffer_with_temp_file From 66324a711cc7311b9e022bfc0badcbbaebc7308e Mon Sep 17 00:00:00 2001 From: Sebastian Henke Date: Mon, 2 Mar 2020 11:06:50 +0100 Subject: [PATCH 3/3] Remove duplicate binmode call --- lib/zip/file.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/zip/file.rb b/lib/zip/file.rb index 5aafe2e1..c768d07d 100644 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -362,7 +362,6 @@ def commit # Write buffer write changes to buffer and return def write_buffer(io = ::StringIO.new('')) - io.binmode if io.respond_to?(:binmode) ::Zip::OutputStream.write_buffer(io) do |zos| @entry_set.each { |e| e.write_to_zip_output_stream(zos) } zos.comment = comment