【转】C++ ZLib压缩与解压缩

来源:互联网 发布:java通用版手机qq 编辑:程序博客网 时间:2024/05/28 11:50
原文:http://www.cnblogs.com/chuncn/archive/2011/03/23/1992481.html
 
  1. #include "stdafx.h"
  2. #include "zlib.h"
  3. #include <iostream>
  4. using namespace std;
  5. #pragma comment(lib,"zlib.lib")

  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.     int err;
  9.     Byte compr[200], uncompr[200]; //足够大
  10.     uLong comprLen, uncomprLen;
  11.     const char* hello = "1234567890"; //原文

  12.     uLong len = strlen(hello);// + 1;
  13.     comprLen = sizeof(compr) / sizeof(compr[0]);

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

  15.     if (err != Z_OK)
  16.     {
  17.         cerr << "压缩错误: " << err << '\n ';
  18.     }

  19.     cout << "原文长度: " << len << "\n压缩后长度:" << comprLen << '\n ';
  20.     strcpy((char*)uncompr, "garbage ");

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

  22.     if (err != Z_OK)
  23.     {
  24.         cerr << "解压错误: " << err << '\n ';
  25.     }

  26.     cout << "\n原文长度:" << len << "\n解压后长度:" << uncomprLen << '\n ';

  27.     if (strcmp((char*)uncompr, hello))
  28.     {
  29.         cerr << "\n解压失败!!!\n ";
  30.     }
  31.     else
  32.     {
  33.         cout << "\n解压成功:" << (char*)uncompr;
  34.     }

  35.     FILE *f= fopen("d:\\b1.bin","wb");
  36.     fwrite(compr,comprLen,1,f);
  37.     fclose(f);

  38.     getchar();
  39.     return 0;
  40. }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(3215) | 评论(0) | 转发(0) |
0

上一篇:【转】Log4j 配置最全说明

下一篇:【转】程序debug正常release错误

相关热门文章
  • 如何解压加上uboot头的ramdisk...
  • 欢迎zlibbxh在ChinaUnix博客安...
  • rpm包管理
  • zabbix php nginx 监控搭建...
  • google play store及youtube完...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击