Gzip压缩减少网络通信

本文讨论了如何使用zlib库解压gzip压缩文件,强调了inflateInit2函数的windowBits参数设置,解释了设置为-MAX_WBITS与47的区别。内容涵盖了gzip和deflate压缩算法的差异,以及在Java和C++中处理gzip数据的代码示例。
摘要由CSDN通过智能技术生成

 核心内容:

Question:

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?

Answer:

To decompress a gzip format file with zlib, call inflateInit2 with the windowBits parameter as16+MAX_WBITS, like this:

inflateInit2(&stream, 16+MAX_WBITS);

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:

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不可以。

 

使用zlib实现gzip格式数据的压缩和解压 http://www.chengxuyuans.com/code/C++/65459.html  核心内容

http://www.oschina.net/code/piece_full?code=22542

 

Question: http://bbs.csdn.net/topics/350183493

现在正在学习用zlib库解压抓来的HTTP包(gzip压缩包)
我看了各位老鸟的文档或是代码,有些了解.
但我有地方始终不明白:
1.解压gzip数据包需要用inflateInit2(z_streamp strm,int windowBits)进行初始化,然后再用inflate()进行解压。
问题就在这里,inflateInit2的第二个参数到底是什么意思(请详细解释下,谢了);
我自己写了个gzip压缩数据,我用inflateInit2进行初始化解压时,第二个参数,设为-MAX_WBITS就OK 但设为47就不好用..
(但是我看各位老鸟都是设定的47,具体有什么区别,谢谢)
2.如果我要压缩一个gzip数据(我要测试下我写的解压代码),我要用什么函数(请讲解下,关键是函数的参数设定为什么才会,
可以用inflateInit2(XX,47)进行解压..)
3.请各位大虾帮助,主要是inflateInit2和inflateInit的区别,还有设定47 和 MAX_WBITS(15)的区别(谢了!!!)

Answer:

deflate与gzip解压的代码几乎相同,应该可以合成一块代码。
区别仅有:
deflate使用inflateInit(),而gzip使用inflateInit2()进行初始化,比 inflateInit()多一个参数: -MAX_WBITS,表示处理raw deflate数据。因为gzip数据中的zlib压缩数据块没有zlib header的两个字节。使用inflateInit2时要求zlib库忽略zlib header。在zlib手册中要求windowBits为8..15,但是实际上其它范围的数据有特殊作用,见zlib.h中的注释,如负数表示raw deflate。
Apache的deflate变种可能也没有zlib header,需要添加假头后处理。即MS的错误deflate (raw deflate).zlib头第1字节一般是0×78, 第2字节与第一字节合起来的双字节应能被31整除,详见rfc1950。例如Firefox的zlib假头为0×7801,python zlib.compress()结果头部为0×789c。




++++++++++++++++++++++++++++++++++++++++++华丽丽的分割符+++++++++++++++++++++++++++++++++++

 

  zlib是一个通用的压缩开源库,提供了在内存中压缩和解压的函数,包括对解压后数据的校验。目前版本的zlib只支持deflate方法,但是其它的方法将会被添加进来并且拥有同样的接口。

—— zlib manaul
    deflate算法在rfc1951中有详细的说明。

    zlib同时又是一种数据格式,使用zlib库压缩后的数据会在deflate数据的头和尾添加信息,形成zlib格式的数据。

   gzip也是一种数据压缩格式,可以大体分为头部,数据部和尾部三个部分,其中头部和尾部主要是一些文档属性和校验信息(rfc1952),数据部主要是用deflate方法压缩得到的数据。
   
    zlib库默认的压缩方法并不是gzip的,而是zlib的,因此使用zlib压缩得到gzip格式的数据有两种方法:

  1. 使用zlib提供的gz***系列函数可以直接把想要的内容写入一个磁盘gzip文件;
  2. 如果想在内存中生成gzip格式的数据,可以在初始化的时候调用inflateInit2函数,并指定为gzip格式,代码如下:

   

  z_stream d_stream;
  d_stream.zalloc = NULL;
  d_stream.zfree = NULL;
  d_stream.opaque = NULL;
  int ret = deflateInit2(&d_stream, Z_DEFAULT_COMPRESSION

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值