Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1145650
  • 博文数量: 646
  • 博客积分: 288
  • 博客等级: 二等列兵
  • 技术积分: 5375
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-08 14:33
个人简介

为了技术,我不会停下学习的脚步,我相信我还能走二十年。

文章分类

全部博文(646)

文章存档

2014年(8)

2013年(134)

2012年(504)

分类: C/C++

2013-03-09 14:53:59

原文地址:使用zlib进行压缩,解压缩 作者:bjpiao

头文件:
#include "zlib.h"

编译:
需要加上 -lz

解压缩:
  1. #define CHUNK 100000

  2. int decodeZip(char *source,int len,char **dest)
  3. {
  4.     int ret;
  5.     unsigned have;
  6.     z_stream strm;
  7.     unsigned char out[CHUNK];
  8.     int totalsize = 0;
  9.     
  10.     /* allocate inflate state */
  11.     strm.zalloc = Z_NULL;
  12.     strm.zfree = Z_NULL;
  13.     strm.opaque = Z_NULL;
  14.     strm.avail_in = 0;
  15.     strm.next_in = Z_NULL;

  16.     ret = inflateInit2(&strm, 31);
  17.     
  18.     if (ret != Z_OK)
  19.         return ret;
  20.     
  21.     strm.avail_in = len;
  22.     strm.next_in = (unsigned char*)source;
  23.     
  24.     /* run inflate() on input until output buffer not full */
  25.     do {
  26.         strm.avail_out = CHUNK;
  27.         strm.next_out = out;
  28.         ret = inflate(&strm, Z_NO_FLUSH);
  29.         switch (ret)
  30.         {
  31.             case Z_NEED_DICT:
  32.                 ret = Z_DATA_ERROR; /* and fall through */
  33.             case Z_DATA_ERROR:
  34.             case Z_MEM_ERROR:
  35.                 inflateEnd(&strm);
  36.             return ret;
  37.         }
  38.         
  39.         have = CHUNK - strm.avail_out;
  40.         totalsize += have;
  41.         *dest = (char*)realloc(*dest,totalsize);
  42.         memcpy(*dest + totalsize - have,out,have);
  43.     } while (strm.avail_out == 0);
  44.     
  45.     /* clean up and return */
  46.     inflateEnd(&strm);
  47.     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  48. }

压缩:
  1. #define MaxLen 10000000

  2. /**
  3.  * 对内容进行压缩和编码工作
  4.  */
  5. void encodeZip(const string &buffer, string& zipBuf, int& zipLen)
  6. {
  7.     unsigned char _zipSrc[MaxLen];
  8.     unsigned char _zipDst[MaxLen];
  9.     unsigned char  _scBuffer[MaxLen];

  10.    //先对原始内容进行压缩工作
  11.     unsigned int tmpLen = buffer.length();
  12.     memcpy(_zipSrc, buffer.c_str(), tmpLen);
  13.     z_stream c_stream;
  14.     c_stream.zalloc = (alloc_func)0;
  15.     c_stream.zfree = (free_func)0;
  16.     c_stream.opaque = (voidpf)0;
  17.     c_stream.next_in = (Bytef*)_zipSrc;
  18.     c_stream.avail_in = tmpLen;
  19.     c_stream.next_out = (Bytef*)_zipDst;
  20.     c_stream.avail_out = (unsigned int)MaxLen;
  21.     int ret = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION,Z_DEFLATED, 31,8, Z_DEFAULT_STRATEGY);
  22.     if(ret != Z_OK)
  23.     {
  24.         cout <<"|"<<"deflateInit2 error "<<endl;
  25.         return;
  26.     }
  27.     ret = deflate(&c_stream, Z_FINISH);
  28.     if (ret != Z_STREAM_END)
  29.     {
  30.         deflateEnd(&c_stream);
  31.         cout <<"|"<<"ret != Z_STREAM_END err="<< ret <<endl;
  32.         return;
  33.     }
  34.     
  35.     zipLen = c_stream.total_out;
  36.     ret = deflateEnd(&c_stream);
  37.     if (ret != Z_OK)
  38.     {
  39.         cout <<"|"<<"deflateEnd error "<<endl;
  40.         return;
  41.     }
  42.     //压缩完毕进行返回包组织
  43.     memcpy(_scBuffer, _zipDst, zipLen);
  44.     zipBuf = string(_scBuffer, zipLen);
  45.     return;
  46. }


阅读(903) | 评论(0) | 转发(0) |
0

上一篇:consistent hash代码

下一篇:trie

给主人留下些什么吧!~~