Gzip 格式文件(例如,使用 gzip
程序创建的)使用“deflate”压缩算法,该算法与 zlib 的压缩算法相同。用途。但是,当使用 zlib 膨胀 gzip 压缩文件时,该库会返回 Z_DATA_ERROR
。
如何使用 zlib 解压 gzip 文件?
最佳答案
要使用 zlib 解压缩 gzip 格式文件,请调用 inflateInit2
,并将 windowBits
参数设置为 16+MAX_WBITS
,如下所示:
inflateInit2(&stream, 16+MAX_WBITS);
如果您不这样做,zlib 将提示流格式错误。默认情况下,zlib 创建带有 zlib header 的流,并且在 inflate 上不会识别不同的 gzip header ,除非您告诉它。尽管从 zlib.h
头文件的 1.2.1 版本开始对此进行了记录,但它不在 zlib manual 中。 。来自头文件:
windowBits
can also be greater than 15 for optional gzip decoding. Add 32 towindowBits
to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (the zlib format will return aZ_DATA_ERROR
). If a gzip stream is being decoded,strm->adler
is a crc32 instead of an adler32.
关于gzip - 如何使用 zlib 解压缩 gzip 流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1838699/