Original Post
Well... I'm trying to use zlib (1.1.4) to decompress the files contained in a .zip file, but inflate keeps Z_DATA_ERROR-ing me
I found out how to read zip file headers (I made a test prog to list the files in an archive)... I skip the "extra field length" and read the compressed file into a buffer (I'm sure the compression method isn't store) and try to decompress the file to another buffer. This, for some reason, doesn't work (probably due to eigher my ignorance or stupid mistakes).
here's the (pseudo) code I use (thanks flipcode):
void ZipUncompress(CZipFileHeader &Header, FILE *l_pFile, const char *a_pFileName)
{
FILE *l_pOut = fopen(a_pFileName, "wb+");
if (!l_pOut) return;
z_stream stream;
int err;
// buffers
unsigned char *l_pBuffer = new unsigned char[Header.iGetUnpackedSize()];
unsigned char *l_pData = new unsigned char[Header.iGetPackedSize()];
fread(l_pData, 1, Header.iGetPackedSize(), l_pFile);
// now inflate
stream.total_out = 0;
stream.zalloc = (alloc_func) 0;
stream.zfree = (free_func) 0;
stream.opaque = (void *) 0;
err = inflateInit (&stream);
if (err != Z_OK){
MessageBox(0, "Zip Corrupt", 0, 0);
return;// E_ZIP_CORRUPT;
}
stream.next_out = (Bytef *) l_pBuffer;
stream.avail_out = Header.iGetUnpackedSize();
stream.next_in = (Bytef *) l_pData;
stream.avail_in = Header.iGetPackedSize();
int flush = Z_FINISH; // tried Z_SYNC_blah as well
err = inflate (&stream, Z_FINISH);
// crc32 check
if (crc32 (0, l_pBuffer, Header.iGetUnpackedSize()) != Header.iGetCRC32())
{
MessageBox(0, "CRC32 Failed", 0, 0);
//return; // why does the CRC32 Fail? E_DATA_ERROR
}
//if (err == Z_DATA_ERROR) { MessageBox(0, "E_ZIP_DATAERROR", 0, 0); return; }
//if (err == Z_MEM_ERROR) { MessageBox(0, "E_ZIP_MEMERROR", 0, 0); return; }
//if (err == Z_BUF_ERROR) { MessageBox(0, "E_ZIP_BUFERROR", 0, 0); return; }
//if (err == Z_STREAM_END) { MessageBox(0, "E_ZIP_CORRUPT", 0, 0); return; }
inflateEnd (&stream);
fwrite(l_pBuffer, Header.iGetUnpackedSize(), 1, l_pOut);
fclose(l_pOut);
delete [] l_pBuffer;
delete [] l_pData;
}
I think the CZipHeader class is quite clear (it has been read off disk before calling the above function). The MessageBox things are for debugging.. I hope to some day be able to remove them...
I took the code from a similar topic at flipcode, but it doesn't work for me.
Please help...
edit: typos
[edited by - Kurioes on November 28, 2002 5:35:44 PM]