在linux上,使用ZLIB的compress()和uncompress()函数,有时会返回Z_BUFFER_错误
我想测试压缩和解压缩函数:compress()解压()由ZLIB库提供;编写以下代码以打开已存在的文件,在while()循环中读取使文件的内容已存在,压缩部分写入单个文件,解压缩部分写入另一个文件,下面显示的代码,已存在的文件(原始文件)的大小约为78K,第一次输入while()循环压缩并解压缩返回值是0,这样第一次输入成功,但第二次和接下来几次输入返回值是-5(根据官方文件,缓冲输出大小不足以容纳内容),为什么?哪里错了?非常感谢在linux上,使用ZLIB的compress()和uncompress()函数,有时会返回Z_BUFFER_错误,c,linux,zlib,C,Linux,Zlib,我想测试压缩和解压缩函数:compress()解压()由ZLIB库提供;编写以下代码以打开已存在的文件,在while()循环中读取使文件的内容已存在,压缩部分写入单个文件,解压缩部分写入另一个文件,下面显示的代码,已存在的文件(原始文件)的大小约为78K,第一次输入while()循环压缩并解压缩返回值是0,这样第一次输入成功,但第二次和接下来几次输入返回值是-5(根据官方文件,缓冲输出大小不足以容纳内容),为什么?哪里错了?非常感谢 enter code here #include <
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
在此处输入代码
#包括
#包括
#包括
#包括
#包括
#包括“zlib.h”
int main()
{
无符号长整型文件长度;
无符号长整型读取长度;
无符号长整型压缩长度;
无符号长整型不压缩长度;
无符号长整型偏移量;
unsigned char*readBuf=new unsigned char[512];//现有文件内容的readBuf
unsigned char*compressBuf=新的unsigned char[512];//压缩缓冲区
unsigned char*uncompressBuf=新的unsigned char[512];//解压缩内容缓冲区
FILE*originalFile=fopen(“/lgw150/temp/src/lg4/original.lg4”,“a+”);//存在的文件
FILE*compressedFile=fopen(“/lgw150/temp/src/lg4/compressed.lg4”,“a+”);//compressfile
FILE*uncompressFile=fopen(“/lgw150/temp/src/lg4/uncompressed.lg4”,“a+”)//
fseek(原始文件,0,2);
fileLength=ftell(原始文件);
偏移量=0//
(offset首先,您得到“缓冲输出大小不足以包含内容”的原因是因为缓冲输出大小不够大,无法包含内容。如果提供不可压缩数据进行压缩,则会扩展数据。因此,如果输入为512字节,则512字节不够大。请使用compressBound()函数的最大扩展来调整压缩输出缓冲区的大小
其次,一次压缩512字节是愚蠢的。你没有给压缩算法提供足够的数据,以获得你应该从压缩中获得的里程数。你一次读取512字节块的应用程序不应该使用compress()和uncompress()。你应该使用deflate()和inflate(),它是为此目的编写的--通过压缩和解压缩引擎提供数据块
您需要阅读。所有内容。您还可以查看(在阅读zlib.h之后)。首先,您得到“缓冲输出大小不足以容纳内容”的原因是因为缓冲输出大小不够大,无法包含内容。如果提供不可压缩数据进行压缩,则会扩展数据。因此,如果输入为512字节,则512字节不够大。请使用compressBound()函数的最大扩展来调整压缩输出缓冲区的大小
其次,一次压缩512字节是愚蠢的。你没有给压缩算法提供足够的数据,以获得你应该从压缩中获得的里程数。你一次读取512字节块的应用程序不应该使用compress()和uncompress()。你应该使用deflate()和inflate(),它是为此目的编写的--通过压缩和解压缩引擎提供数据块
您需要阅读。所有这些。您还可以查看(在阅读zlib.h之后).当我看到你的名字时,我拍了两张照片。马克·阿德勒?是的,更像是马克·阿德勒。谢谢,马克·阿德勒,我会仔细阅读zlib.h,如果我这样做了,我会再次给你发信息并表示感谢。谢谢,马克·阿德勒,当我使用1024*30字节而不是512字节时,效果很好,我会尝试deflate()和infalte()很快。当我看到你的名字时,我拍了两张照片。马克·阿德勒?是的,更像是马克·阿德勒。谢谢,马克·阿德勒,我会仔细阅读zlib.h,如果我这样做了,我会再次给你发信息并感谢你。谢谢,马克·阿德勒,当我使用1024*30字节而不是512字节时,效果很好,我会很快尝试deflate()和infalte()。