我做了一個簡單的函數,它需要一個gzip文件,並從某處提取。出於測試目的,我使用的文本文件已通過通用實用程序gzip gzip進行了gzip壓縮。 但由於某種原因,Uncompress()返回錯誤Z_DATA_ERROR。我使用調試器直到函數,它肯定會得到正確的數據(整個文件的內容,它只有37個字節),所以它似乎是其中的一個:可怕的zlib-bug正在竊取你的時間現在,或者我失去了一些重要的東西,然後我真的很抱歉。'zlib'的解壓縮()返回Z_DATA_ERROR
#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");
}
uncompress()是用deflate算法壓縮的原始數據,它不知道gzip格式。 – nos
@nos我也想過了。現在我正在尋找一個標題規範,如果我找到了,我會在這裏發佈一個工作解決方案。雖然我想知道:應該已經有工作了...... –
您可以在鏈接到的頁面上使用gzopen()函數。或者使用inflate()函數。 gzip格式在RFC 1952 – nos