Search code examples
zlibapi-design

What's special about deflateInit2 that prevents deflateSetHeader be used with deflateInit?


I'm trying to learn how to use zlib functions in my C language project. I need to support both RFC-1950 zlib format as well as RFC-1952 gzip format in my application.

To use gzip format, the deflateSetHeader must be used. But while reading the documentation from the zlib source, it appears to say that deflateSetHeader must be used with deflateInit2 rather than deflateInit. But the reason for this is not explained.

Q: Can deflateSetHeader be used with deflateInit? Or why does it has to be used with deflateInit2?


Solution

  • No, you do not need to use deflateSetHeader() in order to produce the gzip format. You just need to use deflateInit2() and request the gzip format with the windowBits parameter value. The use of deflateSetHeader() is entirely optional, and is used only when you want the gzip header contents to be different from the default header.

    Yes, you can only use deflateSetHeader() after deflateInit2(), because deflateSetHeader() only applies to the gzip header, and the only way to request the gzip format in the first place is to use deflateInit2().