akasi
akasi

Reputation: 93

setup zlib to generate gzipped data

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

Upvotes: 7

Views: 6968

Answers (2)

user2088188
user2088188

Reputation:

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

Upvotes: 10

Mark Adler
Mark Adler

Reputation: 112239

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().

Upvotes: 3

Related Questions