Search code examples
javacompressionzlibinflategzipinputstream

How does zlib inflate GZIP trailer correctly in JAVA?


When using GZIPInputStream in JAVA, it is not passing the whole buffer to zlib and inflate it as a GZIP format. Instead, It seems that GZIP header is been processed in JAVA side, and then pass the remaining part(raw deflate data + GZIP trailer)to zlib and do the inflate work as a DEFLATE format.
My question is how does zlib do inflate work correctly, as the data passed in is not just a raw deflate data? In other words, how does zlib keeps the 8-bytes trailer and not treated it as the data needs to be decompressed?


Solution

  • The deflate format is self-terminating. zlib stops decompressing the raw deflate stream until the deflate block marked as the last block is processed. What's left over is the trailer for the Java code to deal with.