在linux上,使用ZLIB的compress()和uncompress()函数,它有时会返回Z_BUFFER_ERROR

时间:2012-07-20 17:43:12

标签: c linux zlib

我想测试压缩和解压缩功能:压缩()uncompresss()由ZLIB库提供;编写了下面的代码来打开一个已经存在的文件,在while()循环中读取内部文件的内容已经存在,压缩部分写入单个文件,解压缩部分写入另一个文件,代码如下所示,已存在的文件大小(originalFile)约为78K,第一次进入while()循环压缩时返回值的解压缩为0,这样第一个条目成功,但第二个和下一个条目成功要输入,返回值是-5(根据官方文档,缓冲输出大小不包含内容),为什么?哪里错了?非常感谢你!

enter code here

#include <string>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "zlib.h"
int main()
{
    unsigned long int fileLength;
    unsigned long int readLength;
    unsigned long int compressBufLength;
    unsigned long int uncompressLength;
    unsigned long int offset;

    unsigned char *readBuf = new unsigned char[512];//the readbuf of the exist file content
    unsigned char *compressBuf = new unsigned char[512];//the compress buffer   
    unsigned char *uncompressBuf = new unsigned char[512];//the uncompress  content buffer
    FILE *originalFile = fopen("/lgw150/temp/src/lg4/original.lg4","a+");//the exist file
    FILE *compressedFile = fopen("/lgw150/temp/src/lg4/compressed.lg4","a+");//compressfile
    FILE *uncompressFile = fopen("/lgw150/temp/src/lg4/uncompressed.lg4","a+");//

    fseek(originalFile,0,2);
    fileLength = ftell(originalFile);
    offset = 0;//
       while(offset <fileLength)//
    {


        printf("offset=%lu;fileLength=%lu\n",offset,fileLength);
        memset(readBuf,0,512);
        memset(compressBuf,0,512);
        memset(uncompressBuf,0,512);
        fseek(originalFile,offset,0);//
        readLength = fread(readBuf,sizeof(char),512,originalFile);
        offset += readLength;//
        int compressValue = compress(compressBuf,&compressBufLength,readBuf,readLength);
        int fwriteValue = fwrite(compressBuf,sizeof(char),compressBufLength,compressedFile);//
        printf("compressValue = %d;fwriteLength = %d;compressBufLength=%lu;readLength = %lu\n",compressValue,fwriteValue,compressBufLength,readLength);

        int uncompressValue = uncompress(uncompressBuf,&uncompressLength,compressBuf,compressBufLength);//
        int fwriteValue2= fwrite(uncompressBuf,sizeof(char),uncompressLength,uncompressFile);//
    }
    fseek(originalFile,0,0);
    fseek(compressedFile,0,0);
    fseek(uncompressFile,0,0);
    if(originalFile != NULL)
    {
        fclose(originalFile);
        originalFile = NULL;
    }

   if(compressedFile != NULL)
    {
        fclose(compressedFile);
        compressedFile = NULL;
    }
     if(uncompressFile != NULL)
    {
        fclose(uncompressFile);
        uncompressFile = NULL;
    }

    delete[] readBuf;
    delete[] compressBuf;
    delete[] uncompressBuf;
return 0;


}

enter code here

1 个答案:

答案 0 :(得分:7)

首先,你得到“缓冲输出大小不足以包含内容”的原因是因为缓冲的输出大小不足以容纳内容。如果给不可压缩的数据进行压缩,它将扩展数据。因此,如果输入为512字节,则512字节不够大。使用compressBound()函数进行最大扩展以调整压缩输出缓冲区的大小。

其次,一次压缩512个字节是愚蠢的。你没有给压缩算法提供足够的数据来获得你应该从压缩得到的里程数。您一次读取512字节块的应用程序不应使用compress()和uncompress()。您应该使用为此目的编写的deflate()和inflate()来通过压缩和解压缩引擎提供数据块。

您需要阅读zlib.h。所有的。您还可以查看example(阅读zlib.h之后)。