Skip to content

Commit b0ee268

Browse files
committed
Set buffers to binmode by default
1 parent 8d91d00 commit b0ee268

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

lib/zip/file.rb

+1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def commit
362362

363363
# Write buffer write changes to buffer and return
364364
def write_buffer(io = ::StringIO.new(''))
365+
io.binmode if io.respond_to?(:binmode)
365366
::Zip::OutputStream.write_buffer(io) do |zos|
366367
@entry_set.each { |e| e.write_to_zip_output_stream(zos) }
367368
zos.comment = comment

lib/zip/output_stream.rb

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def open(file_name, encrypter = nil)
5858

5959
# Same as #open but writes to a filestream instead
6060
def write_buffer(io = ::StringIO.new(''), encrypter = nil)
61+
io.binmode if io.respond_to?(:binmode)
6162
zos = new(io, true, encrypter)
6263
yield zos
6364
zos.close_buffer

test/output_stream_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class ZipOutputStreamTest < MiniTest::Test
66
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
77
TEST_ZIP.zip_name = 'test/data/generated/output.zip'
88

9+
ASCII8BIT = 'ASCII-8BIT'
10+
911
def test_new
1012
zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name)
1113
zos.comment = TEST_ZIP.comment
@@ -32,6 +34,15 @@ def test_write_buffer
3234
assert_test_zip_contents(TEST_ZIP)
3335
end
3436

37+
def test_write_buffer_binmode
38+
io = ::StringIO.new('')
39+
buffer = ::Zip::OutputStream.write_buffer(io) do |zos|
40+
zos.comment = TEST_ZIP.comment
41+
write_test_zip(zos)
42+
end
43+
assert buffer.external_encoding.name === ASCII8BIT
44+
end
45+
3546
def test_write_buffer_with_temp_file
3647
tmp_file = Tempfile.new('')
3748

0 commit comments

Comments
 (0)