感谢网友贡献的测试示例,使用很方便。
/*
* main.c
*
* Created on: Aug 12, 2012
* Author: zhuguangsheng
*/
#include <stdio.h>
#include <zlib.h>
int main(int argc, char *argv[])
{
printf("hello my zlib test!\n");
//原始数据
const unsigned char strSrc[]="很简单,要编译安装linux下的静态库(.a)文件,那么在命令行下输入 ./configure;make;make install;就可以了,而如果要编译安装共享库(.so,类似windows下面的.dll),那么在命令行下输入 ./configure -s;make;make install;如果出现的都是Yes,那么就成功了。";
unsigned char buf[1024]={0},strDst[1024]={0};
unsigned long srcLen=sizeof(strSrc),bufLen=sizeof(buf),dstLen=sizeof(strDst);
printf("Src string:%s\nLength:%ld\n",strSrc,srcLen);
//压缩
compress(buf,&bufLen,strSrc,srcLen);
printf("\nAfter Compressed Length:%ld\n",bufLen);
printf("Compressed String:%s\n",buf);
//解压缩
uncompress(strDst,&dstLen,buf,bufLen);
printf("\nAfter UnCompressed Length:%ld\n",dstLen);
printf("UnCompressed String:%s\n",strDst);
return 0;
}
传入,传出,指针,得到数据,展示即可。so easy!
作者将此函数进行封装后,更利于使用:
unsigned char* CompressByZlib(unsigned char *Addr, unsigned long RawLength,unsigned long *CompressedLength)
{
/*------------------------------------------------------
*输入:数据源指针,数据长度,及压缩目标程度存储变量地址
*输出:压缩完成数据指针,数据长度被赋值。
*Input: source pointer , source length ,and compress output length variable
*Output: compress output data pointer ,and compress output length variable will be initialised
------------------------------------------------------*/
printf("Zlib Compressing \n");
unsigned char BufCompressed[RawLength];
memset(BufCompressed,0,RawLength);
int err =0 ; *CompressedLength = RawLength ;
err = compress(BufCompressed,CompressedLength,Addr,RawLength);
if (err != Z_OK)
{
printf("Compress error\n");
exit(1);
}
printf("\nAfter Compressed Length:%ld\n",*CompressedLength);
printf("Compressed String:%s\n",BufCompressed);
printf("\nRaw Data Length:%ld\n",RawLength);
unsigned char *OutCompressedData = (unsigned char*)malloc(sizeof(char)*(*CompressedLength)); memset(OutCompressedData,0,*CompressedLength);
memcpy(OutCompressedData,BufCompressed,*CompressedLength);
/*-------------------------------------------
* 自测使用,解压缩
* uncompress for self-test.
*-------------------------------------------*/
// unsigned char BufUncompressed[RawLength]; unsigned long UncompressLen=RawLength ;
// uncompress(BufUncompressed,&UncompressLen,OutCompressedData,*CompressedLength);
// printf("\nAfter UnCompressed Length:%ld\n",UncompressLen);
// for(unsigned int i=0;i<RawLength;i++)
// {
// if(Addr[i]!=BufUncompressed[i])
// {
// printf("Data Not Real\n");
// }
// }
// printf("Data Real kept\n");
return OutCompressedData;
}
解压缩函数
unsigned char* UnCompressByZlib(unsigned char *Addr, unsigned long RawLength,unsigned long CompressedLength)
{
/*-----------------------------------------------------
* 解压缩,内存使用完成后,请释放
* uncompress ,free pointer after memory used
-----------------------------------------------------*/
unsigned char *BufUncompressed = (unsigned char*)malloc(sizeof(char)*RawLength) ;
unsigned long UncompressLen=RawLength ; int err =0 ;
err=uncompress(BufUncompressed,&RawLength,Addr,CompressedLength);
if (err != Z_OK)
{
printf("Uncompress error\n");
exit(-1);
}
return BufUncompressed ;
}