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.#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");} 解决方案 You should use 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.You can take the source code for 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. 这篇关于'zlib'的Uncompress()返回Z_DATA_ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 19:23