'zlib'的Uncompress()返回Z_DATA_ERROR [英] Uncompress() of 'zlib' returns Z_DATA_ERROR
问题描述
我做了一个简单的功能,该功能可以将压缩后的文件提取出来.为了进行测试,我使用了通过通用实用程序 gzip 压缩过的文本文件.
但是由于某些原因, Uncompress()返回错误
您应该使用 您可以在zlib中获取 I made a simple function which takes a gzipped file, and extracts somewhere. For testing purposes i'm using a text file which had been gzipped through a generic utility gzip.
But for some reason the Uncompress() returns an error Z_DATA_ERROR. I walked in a debugger till the function, and it surely gets the right data(a whole file content, it's just 37 bytes), so it seems to be one of two: the frightful zlib-bug is stealing your time for now, or I am missing something important and then I really sorry.
You should use You can take the source code for 这篇关于'zlib'的Uncompress()返回Z_DATA_ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!inflate()
,而不是uncompress()
.在inflateInit2()
中,您可以指定gzip格式(或自动检测zlib或gzip格式).请参阅zlib.h中的文档.uncompress()
的源代码,并进行简单的更改以使用inflateInit2()
而不是inflateInit()
来创建自己的gzipuncompress()
或任何您想调用的名称.>#include <zlib.h>
#include <cstdio>
int UngzipFile(FILE* Dest, FILE* Source){
#define IN_SIZE 256
#define OUT_SIZE 2048
bool EOFReached=false;
Bytef in[IN_SIZE];
Bytef out[OUT_SIZE];
while(!EOFReached){//for no eof
uLong In_ReadCnt = fread(in,1,IN_SIZE,Source);//read a bytes from a file to input buffer
if(In_ReadCnt!=IN_SIZE){
if(!feof(Source) ){
perror("ERR");
return 0;
}
else EOFReached=true;
}
uLong OutReadCnt = OUT_SIZE;//upon exit 'uncompress' this will have actual uncompressed size
int err = uncompress(out, &OutReadCnt, in, In_ReadCnt);//uncompress the bytes to output
if(err!=Z_OK){
printf("An error ocurred in GZIP, errcode is %i\n", err);
return 0;
}
if(fwrite(out,1,OutReadCnt,Dest)!=OUT_SIZE ){//write to a 'Dest' file
perror("ERR");
return 0;
}
}
return 1;
}
int main(int argc, char** argv) {
FILE* In = fopen("/tmp/Kawabunga.gz", "r+b");
FILE* Out = fopen("/tmp/PureKawabunga", "w+b");
if(!In || !Out){
perror("");
return 1;
}
if(!UngzipFile(Out,In))printf("An error encountered\n");
}
inflate()
, not uncompress()
. In inflateInit2()
, you can specify the gzip format (or auto-detection of the zlib or gzip format). See the documentation in zlib.h.uncompress()
in zlib and make a simple change to use inflateInit2()
instead of inflateInit()
to create your own gzipuncompress()
, or whatever you'd like to call it.