我正在尝试通过调用inflate函数来解压文件,但无论我使用网站上的示例程序,它都会始终失败并显示Z_DATA_ERROR。我认为可能是我拥有的zip文件不受支持。下面附上zip头的图片。
以下是我编写的执行解压缩的函数。我一次性读取整个文件(约34KB)并将其传递给此函数。请注意,我已经尝试过同时传递具有zip头和跳过zip文件头仅传递压缩数据,但两者在调用inflate()时均失败并显示Z_DATA_ERROR。
int CHttpDownloader::unzip(unsigned char * pDest, unsigned long * ulDestLen, unsigned char * pSource, int iSourceLen){
int ret = 0;
unsigned int uiUncompressedBytes = 0; // Number of uncompressed bytes returned from inflate() function
unsigned char * pPositionDestBuffer = pDest; // Current position in dest buffer
unsigned char * pLastSource = &pSource[iSourceLen]; // Last position in source buffer
z_stream strm;
// Skip over local file header
SLocalFileHeader * header = (SLocalFileHeader *) pSource;
pSource += sizeof(SLocalFileHeader) + header->sFileNameLen + header->sExtraFieldLen;
// We should now be at the beginning of the stream data
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, 16+MAX_WBITS);
if (ret != Z_OK){
return -1;
}
// Uncompress the data
strm.avail_in = header->iCompressedSize; //iSourceLen;
strm.next_in = pSource;
do {
strm.avail_out = *ulDestLen;
strm.next_out = pPositionDestBuffer;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return -2;
}
uiUncompressedBytes = *ulDestLen - strm.avail_out;
*ulDestLen -= uiUncompressedBytes; // ulDestSize holds number of free/empty bytes in buffer
pPositionDestBuffer += uiUncompressedBytes;
} while (strm.avail_out == 0);
// Close the decompression stream
inflateEnd(&strm);
ASSERT(ret == Z_STREAM_END);
return 0;
}
所以我的问题是,我读入的ZIP文件类型不被ZLib的inflate()函数支持吗?还是我的CHttpDownloader::unzip()函数有什么问题?感谢任何帮助 :)
所以我的问题是,我读入的ZIP文件类型是否不受ZLib的inflate()函数支持?还是我的CHttpDownloader::unzip()函数存在问题?感谢任何帮助 :)