zlib进行数据压缩,主要利用的函数为compress和uncompress
在编译时加-lzip
一下是简单的一个例子,具体的函数可以参考:
其他一些好的例子:
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;
}