Skip to content

Commit

Permalink
Set buffers to binmode by default
Browse files Browse the repository at this point in the history
  • Loading branch information
henkesn committed Feb 19, 2020
1 parent 8d91d00 commit b0ee268
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/zip/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/zip/output_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/output_stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('')

Expand Down

0 comments on commit b0ee268

Please # to comment.