linux下zlib的使用(linux下名字为libz)


感谢网友贡献的测试示例,使用很方便。

/*
 * 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 ;
}









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值