zlib中compress函数的使用方法

  1 #include <cstring> 

  2 #include <cstdlib>

  3 #include <iostream>

  4 #include <zlib.h>

  5 

  6 using namespace std;

  7 

  8 int main()

  9 {

 10     int err;

 11     Byte compr[200], uncompr[200];    // big enough

 12     uLong comprLen, uncomprLen;

 13     const char* hello = "12345678901234567890123456789012345678901234567890";

 14 

 15     uLong len = strlen(hello) + 1;

 16     comprLen  = sizeof(compr) / sizeof(compr[0]);

 17 

 18     err = compress(compr, &comprLen, (const Bytef*)hello, len);

 19 

 20     if (err != Z_OK) {

 21         cerr << "compess error: " << err << '/n';

 22         exit(1);

 23     }

 24     cout << "orignal size: " << len

 25          << " , compressed size : " << comprLen << '/n';

 26 

 27     strcpy((char*)uncompr, "garbage");

 28 

 29     err = uncompress(uncompr, &uncomprLen, compr, comprLen);

 30 

 31     if (err != Z_OK) {

 32         cerr << "uncompess error: " << err << '/n';

 33         exit(1);

 34     }

 35     cout << "orignal size: " << len

 36          << " , uncompressed size : " << uncomprLen << '/n';

 37 

 38     if (strcmp((char*)uncompr, hello)) {

 39         cerr << "BAD uncompress!!!/n";

 40         exit(1);

 41     } else {

 42         cout << "uncompress() succeed: /n" << (char *)uncompr;

 43     }

 44 }

转过来的,很好的例子,比教容易弄懂。

另外编译的时候别忘了加上-lz

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值