利用zlib进行数据压缩

zlib进行数据压缩,主要利用的函数为compress和uncompress

在编译时加-lzip

一下是简单的一个例子,具体的函数可以参考:

www.zlib.net

其他一些好的例子:

http://blog.csdn.net/pennyliang/archive/2011/04/01/6296102.aspx

http://blog.csdn.net/querw/archive/2006/12/21/1452041.aspx

http://hi.baidu.com/vipxxoxx/blog/item/1b6ffc97693870037bf480df.html

#include<stdio.h>
#include<string.h>
#include<zlib.h>
#define SIZE 100
char *source="hello world~";
void compressInMemory(Byte *dest,uLong *destlen){
	int err=compress(dest,destlen,(Byte*) source,(uLong)strlen(source));
	if(err) perror("compress perror");
	return;
}
void uncompressFromMemory(Byte *dest,uLong *destlen,Byte *source,uLong sourcelen){
	int err=uncompress(dest,destlen,(Byte*)source,sourcelen);
	if(err) perror("uncompress perror");
	return;
}
int main(void){
	//char *source="hello world";
	char result[1000]={'\0'};
	uLong reslen=SIZE;
	Byte dest[1000];
	uLong destlen=SIZE;
	printf("source is %s\n",source);
	compressInMemory(dest,&destlen);
	//compress(dest,&destlen,(Byte *)source,(uLong)strlen(source));
	printf("after compress is %s\n",(char*)dest);
	//uncompress((Byte*) result,&reslen,dest,destlen);
	uncompressFromMemory((Byte*) result,&reslen,dest,destlen);
	printf("after uncompress is %s\n",result);
	return 0;
}

转载于:https://www.cnblogs.com/aLittleBitCool/archive/2011/06/25/2090226.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值