zlib库compress和uncompress函数

1、compress函数

/*
     Compresses the source buffer into the destination buffer.  sourceLen is
   the byte length of the source buffer. Upon entry, destLen is the total
   size of the destination buffer, which must be at least the value returned
   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
   compressed buffer.
     This function can be used to compress a whole file at once if the
   input file is mmap'ed.
     compress returns Z_OK if success, Z_MEM_ERROR if there was not
   enough memory, Z_BUF_ERROR if there was not enough room in the output
   buffer.
*/

int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
        compress函数将 source 缓冲区中的内容压缩到 dest 缓冲区。 sourceLen 表示source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen表示 dest 缓冲区的大小,zlib本身有提供compressBound函数用于计算压缩后缓冲区长度的上限值,不需要额外再设计一些不适当的预测算法(注意2002年的版本没有compressBound函数,2004年及以后的版本都是有的)。当函数退出后,destLen 表示压缩后缓冲区的实际大小。此时 destLen / sourceLen 正好是压缩率。
        若compress 若成功,则返回 Z_OK;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR。
 

2、uncompress函数

/*
     Decompresses the source buffer into the destination buffer.  sourceLen is
   the byte length of the source buffer. Upon entry, destLen is the total
   size of the destination buffer, which must be large enough to hold the
   entire uncompressed data. (The size of the uncompressed data must have
   been saved previously by the compressor and transmitted to the decompressor
   by some mechanism outside the scope of this compression library.)
   Upon exit, destLen is the actual size of the compressed buffer.
     This function can be used to decompress a whole file at once if the
   input file is mmap'ed.
 
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
   enough memory, Z_BUF_ERROR if there was not enough room in the output
   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
*/
int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
uncompress 函数将 source 缓冲区的内容解压缩到 dest 缓冲区。sourceLen 是 source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen 表示 dest 缓冲区的大小, dest 缓冲区要足以容下解压后的数据。在进行解压缩时,需要提前知道被压缩的数据解压出来会有多大。这就要求在进行压缩之前,保存原始数据的大小(也就是解压后的数据的大小)。这不是 zlib 函数库的功能,需要我们做额外的工作。当函数退出后, destLen 是解压出来的数据的实际大小。
        若uncompress 若成功,则返回 Z_OK ;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR。若输入数据有误,则返回 Z_DATA_ERROR。

 

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <zlib.h>

int main(int argc, char* argv[])
{
	char text[] = "zlib compress and uncompress test\nturingo@163.com\n2012-11-05\n";
	uLong tlen = strlen(text) + 1;	/* 需要把字符串的结束符'\0'也一并处理 */
	char* buf = NULL;
	uLong blen;
 
	/* 计算缓冲区大小,并为其分配内存 */
	blen = compressBound(tlen);	/* 压缩后的长度是不会超过blen的 */
	if((buf = (char*)malloc(sizeof(char) * blen)) == NULL)
	{
		printf("no enough memory!\n");
		return -1;
	}
 
	/* 压缩 */
	if(compress(buf, &blen, text, tlen) != Z_OK)
	{
		printf("compress failed!\n");
		return -1;
	}
 
	/* 解压缩 */
	if(uncompress(text, &tlen, buf, blen) != Z_OK) 
	{
		printf("uncompress failed!\n");
		return -1;
	}
 
	/* 打印结果,并释放内存 */
	printf("%s", text);
	if(buf != NULL)
	{
		free(buf);
		buf = NULL;
	}
 
	return 0;
}

zlib处理的对象是Bytef*字节流,很多人遇到字符串就会混淆了,其实很简单,字节流是没有结束符的,需要配备长度信息,所以处理字符串的时候需要把结束符也当成一个普通的字节,这样计算长度的时候也需要算它一份。

另外绝大部分人都想动态分配缓冲区,也就是说需要多少再给多少,其实zlib本身有提供compressBound函数用于计算压缩后缓冲区长度的上限值,不需要额外再设计一些不适当的预测算法,不过解压缩的时候没有提供长度的预测,由于compress和uncompress一般都是成对使用的,预先保存好原文的长度即可。

#include <cstring>
#include <cstdlib>
#include <iostream>
#include "zlib.h"
 
using namespace std;
 
int main()
{
    int err;
    Byte compr[200], uncompr[200];    // big enough
    uLong comprLen, uncomprLen;
 
    const char* hello = "12345678901234567890123456789012345678901234567890";
    uLong len = strlen(hello) + 1;
    comprLen  = sizeof(compr) / sizeof(compr[0]);
 
    err = compress(compr, &comprLen, (const Bytef*)hello, len);
    if (err != Z_OK) 
    {
        cerr << "compess error: " << err << '\n';
        exit(1);
    }
 
    cout << "orignal size: " << len
         << " , compressed size : " << comprLen << '\n';
 
    strcpy((char*)uncompr, "garbage");
 
    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    if (err != Z_OK) 
    {
        cerr << "uncompess error: " << err << '\n';
        exit(1);
    }
 
    cout << "orignal size: " << len
         << " , uncompressed size : " << uncomprLen << '\n';
 
    if (strcmp((char*)uncompr, hello)) 
    {
        cerr << "BAD uncompress!!!\n";
        exit(1);
    } else 
    {
        cout << "uncompress() succeed: \n" << (char *)uncompr;
    }
}

运行结果

orignal size: 51 , compressed size : 22
orignal size: 51 , uncompressed size : 51
uncompress() succeed:
12345678901234567890123456789012345678901234567890 

### 回答1: zlib是一个用于数据压缩和解压缩的开源。它提供了一些函数和数据类型,可以用于在各种应用中进行数据压缩和解压缩的操作。以下是关于zlib安装与使用的一些说明: 安装zlib非常简单。首先,需要从官方网站(https://zlib.net/)下载zlib的源代码。然后,在终端中进入解压后的文件夹,并执行以下命令: $ ./configure $ make $ sudo make install 这些命令将会编译并安装zlib。请确保系统中已经安装了必要的编译工具(如gcc)。 安装完成后,就可以在自己的应用中使用zlib了。首先,需要在源代码中包含zlib头文件: #include <zlib.h> 接下来,可以使用zlib提供的函数进行压缩和解压缩的操作。最常用的函数包括: int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); 这些函数可以分别用于压缩和解压缩数据。其中,dest是用于存储压缩或解压缩结果的缓冲区,destLen是dest缓冲区的大小,source是待压缩或解压缩的数据,sourceLen是source数据的大小。这些函数的返回值代表操作的结果,如果返回值为Z_OK,则表示操作成功。 除了上述基本函数外,zlib还提供了一些其他函数和数据类型,用于更复杂的压缩和解压缩操作,如gzip文件的读写和压缩级别的设置等。具体的使用方法可以参考zlib的官方文档和示例代码。 总结来说,zlib是一个方便易用的数据压缩和解压缩,可以通过简单的安装和使用步骤在应用中实现数据的高效压缩和解压缩操作。 ### 回答2: zlib是一个开源的数据压缩,可用于许多不同的应用程序中。它提供了一套简单而强大的API,用于在应用程序中进行数据的压缩和解压缩操作。下面是关于zlib的安装和使用的简要介绍: 安装zlib的步骤如下: 1. 下载zlib的源代码包,官方网站提供了源码的下载链接。 2. 解压缩源码包到一个目录中。 3. 打开命令行终端,进入解压后的源码目录。 4. 运行配置脚本,执行以下命令:./configure 配置脚本将会根据系统的环境和设置自动选择一些编译选项。 5. 编译源代码,执行以下命令:make 这一步会根据配置生成相应的目标文件。 6. 安装生成的文件和头文件,执行以下命令:sudo make install 这将会把文件和头文件复制到系统中相应的目录,以便其他程序可以使用。 使用zlib的步骤如下: 1. 在你的C/C++程序中包含zlib的头文件:#include <zlib.h> 2. 声明一个z_stream结构体,这个结构体将被用来进行数据的压缩和解压缩操作。 3. 初始化z_stream结构体的成员变量,设置压缩或解压缩的参数,例如输入和输出的缓冲区等。 4. 调用zlib中提供的函数进行压缩或解压缩操作。 对于压缩操作,可使用函数:int deflate(&stream, Z_FINISH) 对于解压缩操作,可使用函数:int inflate(&stream, Z_FINISH) 5. 根据返回值判断操作是否成功,并进行相应的处理。 6. 在程序结束时,释放相关资源,例如关闭文件指针,释放内存等。 总结一下,zlib提供了方便易用的API,能够帮助我们实现数据的压缩和解压缩。通过简单的安装和使用步骤,我们可以轻松地在我们的应用程序中引入zlib,并通过调用相应的函数进行数据的压缩和解压缩操作。 ### 回答3: zlib是一个用于数据压缩和解压缩的C语言。下面是关于zlib的安装和使用的简要步骤: 1. 下载zlib:可以从官方网站(https://www.zlib.net/)下载最新版本的zlib,也可以使用包管理器(如apt、yum等)进行安装。 2. 解压缩zlib:使用压缩工具(如WinRAR、7-Zip等)解压缩下载的zlib压缩文件。 3. 打开终端(命令提示符):在命令行界面下进入解压缩后的zlib目录。 4. 配置zlib:运行configure命令,该命令会根据当前系统环境生成相应的Makefile,用于编译和安装zlib。 5. 编译zlib:运行make命令,该命令会根据Makefile文件编译zlib。 6. 安装zlib:运行make install命令,该命令将zlib的文件复制到系统指定的目录下。 7. 在项目中使用zlib:在项目中包含zlib.h头文件,并链接zlib( z一般通过-lz来链接)即可使用zlib提供的函数进行数据压缩和解压缩的操作。 使用zlib时,常用的函数有: - deflate():使用zlib进行数据压缩。 - inflate():使用zlib进行数据解压缩。 - compress():使用zlib进行数据压缩,并产生gzip格式的压缩数据。 - uncompress():使用zlib进行gzip格式的数据解压缩。 总结:zlib是一个用于数据压缩和解压缩的C语言,安装和使用步骤包括下载、解压缩、配置、编译和安装zlib,以及在项目中包含头文件和链接进行使用。常用函数包括deflate()、inflate()、compress()和uncompress()。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值