6
votes

I have a question about using zlib library for compressing data. I want to setup zlib (namely deflateInit function) in such a way that the compressed data is binary equal to the data generated by command: gzip -9. Is this possible? Thank you in advance

2

2 Answers

9
votes

windowsBits argument's default value is 15.
Adding 16 to it will be 31.
15 | 16 returns 31.

z_stream strm;
unsigned char* in = DATA TO COMPRESS;

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;

int windowsBits = 15;
int GZIP_ENCODING = 16;

deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
              windowsBits | GZIP_ENCODING,
              8,
              Z_DEFAULT_STRATEGY));

http://www.lemoda.net/c/zlib-open-write/index.html

3
votes

You cannot get exactly the same output as gzip. You can however get output that is compatible with gzip, so that gzip will be able to decompress it. You need to use deflateInit2().