gzip解压缩c语言linux,zlib库解压http报文中的gzip数据

数据Content-Encoding:gzip   Transfer-Encoding: chunked

已经将数据包里的内容保存到.zip文件里,可以手动解压出来数据,证明数据是完整的。

我的数据为1F 8B 08 00 00 00 00 00 04 00 04 C1 B1 0D 00 20 08 04

C0 55 CC D7 16 10 31 10 B6 B1 60 01 C1 CA B8 BB 77 17

59 AB 02 2E 44 BD E1 64 6C 38 D8 8C 98 C7 54 51 BC 3F

00 2D E7 34 0B 23 00 00 00,

但是我用zlib库,内存解压的方法,以下为代码(在网上copy滴):

int  inflate_read(char  *source,int  len,char  **dest,int  gzip)

{

int  ret;

unsigned  have;

z_stream  strm;

unsigned  char  out[CHUNK];

int  totalsize  =  0;

/*  allocate  inflate  state  */

strm.zalloc  =  Z_NULL;

strm.zfree  =  Z_NULL;

strm.opaque  =  Z_NULL;

strm.avail_in  =  0;

strm.next_in  =  Z_NULL;

if(gzip)

ret  =  inflateInit2(&strm,  47);

else

ret  =  inflateInit(&strm);

if  (ret  !=  Z_OK)

return  ret;

strm.avail_in  =  len;

strm.next_in  =  source;

/*  run  inflate()  on  input  until  output  buffer  not  full  */

do  {

strm.avail_out  =  CHUNK;

strm.next_out  =  out;

ret  =  inflate(&strm,  Z_NO_FLUSH);

assert(ret  !=  Z_STREAM_ERROR);    /*  state  not  clobbered  */

switch  (ret)  {

case  Z_NEED_DICT:

ret  =  Z_DATA_ERROR;          /*  and  fall  through  */

case  Z_DATA_ERROR:

case  Z_MEM_ERROR:

inflateEnd(&strm);

return  ret;

}

have  =  CHUNK  -  strm.avail_out;

totalsize  +=  have;

*dest  =  realloc(*dest,totalsize);

memcpy(*dest  +  totalsize  -  have,out,have);

}  while  (strm.avail_out  ==  0);

/*  clean  up  and  return  */

(void)inflateEnd(&strm);

return  ret  ==  Z_STREAM_END  ?  Z_OK  :  Z_DATA_ERROR;

}

总是在ret = inflateInit2(&strm, 47); 返回Z_STREAM_ERROR,把47改为15后,这一步不会出错,但是在ret = inflate(&strm, Z_NO_FLUSH); 会出现Z_DATA_ERROR,请赐教下是什么原因呢?谢谢大虾们

阅读(5382) | 评论(1) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值