gzip - 如何使用 zlib 解压缩 gzip 流?

标签 gzip zlib inflate

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 to windowBits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (the zlib format will return a Z_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/

相关文章:

java - 将响应编码为 Gzip 导致无法在浏览器中设置 Cookie

c++ - SDL 和 zlib 问题,无法从 zip 创建有效的 rwops 结构

delphi - 如何吃掉从流中流出的字节?

android - 如何在 XML 中扩展自定义 View ?

xml - 使用 gzip 编码对同一服务器内的通信有意义吗?

html - 将 gzip 压缩的 css 文件导入 html 标题?

c# - Unity 包管理器错误 : zlib: incorrect data check. 没有加载包

android - 如何使用 onFinishInflate 知道 View 何时膨胀

android - InflateException 我有一个错误膨胀类

hadoop - 大 gz 文件上的 Mapreduce