- #include <stdio.h>
- #include "zlib.h"
-
- int main()
- {
- //原始数据
- const unsigned char strSrc[]="hello world!\n\
- aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
- aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
- aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
- aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试";
-
- unsigned char buf[1024]={0},strDst[1024]={0};
- unsigned long srcLen=sizeof(strSrc),bufLen=sizeof(buf),dstLen=sizeof(strDst);
-
- printf("Src string:%s\nLength:%d\n",strSrc,srcLen);
- //压缩
- compress(buf,&bufLen,strSrc,srcLen);
- printf("\nAfter Compressed Length:%d\n",bufLen);
- printf("Compressed String:%s\n",buf);
- //解压缩
- uncompress(strDst,&dstLen,buf,bufLen);
- printf("\nAfter UnCompressed Length:%d\n",dstLen);
- printf("UnCompressed String:%s\n",strDst);
- return 0;
- }
代码很好,差不多就是这样的,建议看看zlib的说明文档,看看例子就明白了,我也有大量用到,zlib比较好,不过并不是最快的,lzo速度比较快,也可以参考,不过做网站应用的话zlib锁压缩出来的gz格式比较好。