I dug a little into the JDK source code. It does indeed set the level. If you follow the setLevel()
with compresser.deflate(new byte[0]);
, then it will work.
What's happening is that the first call of deflate()
after the setLevel()
will see that the level is changed, and call zlib's deflateParams()
function to change it. deflateParams()
will then compress the available data, but the fact that you asked for a finish()
is not passed along. The JDK implementation then does not call deflate()
with Z_FINISH
. As a result, the data you gave is sent for compression, the compressor accumulates the data, but it does not emit a compressed block since it was not asked to finish. So you get nothing.
You need the call to deflate()
after the setLevel()
to actually set the level. Then subsequent data will be compressed with the new level.
It is important to note that data provided up to the first deflate()
call after the setLevel()
will be compressed with the old compression level. Only data provided after that deflate()
call will use the new level. So if in your example you simply did another deflate()
after the last one, it would apply the finish()
and you would get your compressed data, but it would use the default compression level.