Backport ZlibTest and fix the bugs revealed

Related: #3107

Motivation:

ZlibEn/Decoder and JdkZlibEncoder in 3.9 do not have any unit tests.
Before applying any patches, we should backport the tests in 4.x so that
we can make sure we do not break anything.

Modification:

- Backport ZlibTest and its subtypes
  - Remove the test for automatic GZIP header detection because the
    ZlibDecoders in 3.9 do not have that feature
- Initialize JdkZlibEncoder.out and crc only when necessary for reduced
  memory footprint
- Fix the bugs in the ZlibEncoders where it fails to compress correctly
  when there are not enough room in the output buffer

Result:

We are more confident when we make changes in ZlibEncoder/Decoder
Bugs have been squashed