Use Zlib::SYNC_FLUSH so buffer does not grow until finished

With JRuby implementation deflate would always return empty string
without Zlib::SYNC_FLUSH. That can cause memory problems when large
files are in deflate buffer as whole and red there with finish call at
once.
This commit is contained in:
Joni Lahtinen 2020-04-29 16:38:33 +03:00
parent b653d57635
commit 5af76cecb5
1 changed files with 4 additions and 2 deletions

View File

@ -13,7 +13,7 @@ module Zip
val = data.to_s
@crc = Zlib.crc32(val, @crc)
@size += val.bytesize
buffer = @zlib_deflater.deflate(data)
buffer = @zlib_deflater.deflate(data, Zlib::SYNC_FLUSH)
if buffer.empty?
@output_stream
else
@ -22,7 +22,9 @@ module Zip
end
def finish
@output_stream << @encrypter.encrypt(@zlib_deflater.finish) until @zlib_deflater.finished?
buffer = @zlib_deflater.finish
@output_stream << @encrypter.encrypt(buffer) unless buffer.empty?
@zlib_deflater.close
end
attr_reader :size, :crc