Gzip format files (created with the gzip
program, for example) use the "deflate" compression algorithm, which is the same compression algorithm as what zlib uses. However, when using zlib to inflate a gzip compressed file, the library returns a Z_DATA_ERROR
.
How can I use zlib to decompress a gzip file?
Best Answer
To decompress a gzip format file with zlib, call
inflateInit2
with thewindowBits
parameter as16+MAX_WBITS
, like this:If you don't do this, zlib will complain about a bad stream format. By default, zlib creates streams with a zlib header, and on inflate does not recognise the different gzip header unless you tell it so. Although this is documented starting in version 1.2.1 of the
zlib.h
header file, it is not in the zlib manual. From the header file: