Zlib库-1.2.11 的函数说明

zlib库函数的使用

1、int compress(Bytef *dest, uLongf *destLen, const Bytef* source, uLong sourceLen);

compress函数将source缓冲区的内容压缩到dest压缩区。sourceLen表示source缓冲区的大小(以字节计)。

destLen是传址调用,当调用函数的时候,destLen表示dest缓冲区的大小 destLen>(sourceLen + 12)*100.1%

或者使用compressBound(sourceLen),当函数退出,destLen表示压缩后缓冲区的实际大小

compress 若成功,返回Z_OK,若没有足够内存,返回Z_MEM_ERROR,若缓冲区不够大,则返回Z_BUF_ERROR

 

2、int uncompress(Bytef *dest ,uLongf *destLen, const Bytef*source, uLong sourceLen);

uncompress函数将source缓冲区的内容解压缩到dest缓冲区。sourceLen是source缓冲区的大小,destLen是传址调用,dest缓冲区必须足以容下解压后的数据,函数退出后,destLen是解压后的数据的实际大小

uncompress若成功,则返回Z_OK,若没有足够内存,则返回Z_MEM_ERROR,若输出缓冲区不够,则Z_BUF_ERROR,若输入数据有误,则返回Z_DATA_ERROR

#include "zconf.h"
#include "zlib.h"
#include <iostream>
using namespace std;
 
#pragma comment(lib,"zdll.lib")
 
int main()
{
	int err;
	Byte compr[200],uncompr[200];
	uLong comprLen,uncomprLen;
	const char* hello = "1213135454646544665456465465457877874655312333131";
 
	uLong len = strlen(hello)+1;
	comprLen = sizeof(compr)/sizeof(compr[0]);
 
	err = compress(compr,&comprLen,(const Bytef*)hello,len);
 
	if(err != Z_OK){
		cerr<<"compress error: "<<err<<endl;
		exit(1);
	}
	cout<<"original size: "<<len<<" ,compressed size: "<<comprLen<<endl;
	strcpy((char*)uncompr,"garbage");
 
	err = uncompress(uncompr,&uncomprLen,compr,comprLen);
	if(err != Z_OK){
		cerr<<"uncompress error: "<<err<<endl;
		exit(1);
	}else
	{
		cout<<"uncompress() succeed: "<<endl;
		cout<<(char*)uncompr<<endl;
	}
	return 0;
}

结果:


 

文章标签: zlib解压缩

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章