我在使用 zlib uncompress() 函数执行解压缩时遇到一些问题。我压缩的文件作为 fileSource 传递给 uncompressFile(),没有引发运行时异常并且工作得很好。我所做的基本上是使用 fread 从文件中读取字节,然后将其存储在缓冲区中,然后压缩/解压缩。 我的解压缩功能:
bool unCompressFile(const char* fileSource, const char* fileDestination) {
std::cout << "\nFilesize: " << fileSize << std::endl << "compressedSize: " << compressedSize << std::endl;
// Test printout: fileSize = 164008 && compressedSize = 77778
// These were the values from a test program
char* bufferSource = new char[(sizeof(char) * compressedSize + 1)];
if (!bufferSource) {
std::cout << "Error allocating memory \n"; return false;
}
// Reading from previously compressed file
FILE* inputFile = NULL;
inputFile = fopen(fileSource, "rb");
if (!inputFile) { return false; } // Error handling
if (!fread(bufferSource, compressedSize, 1, inputFile)) {
fclose(inputFile);
delete[] bufferSource;
return false;
}
fclose(inputFile);
uLong destinationLen = fileSize;
Bytef* bufferDestination = new Bytef[fileSize + 1];
int result = uncompress(bufferDestination, &destinationLen, (const Bytef*)bufferSource, compressedSize); // Here is the error
fclose(outputFile);
delete[] bufferSource;
delete[] bufferDestination;
return true;
}
字符串 fileSource 是已压缩文件的路径,fileDestination 将是输出,我使用 fwrite(此处省略)。
compressedSize
和 fileSize
都是全局变量。 fileSize 的值是原始文件的字节长度,compressedSize 是压缩数据的大小(由 compress() 修改)。解压缩函数时发生异常,否则不会显示错误打印。我看不出是什么导致了这个运行时错误。
编辑1:我尝试使用compress2(),对于压缩级别我尝试了0(无),所以基本上没有压缩,我的uncompressFile函数确实有效。有些东西搞乱了减压,但我无法弄清楚。
编辑2:压缩文件的前30个字节:
78 9C EC B9 77 54 53 4D F4 2E 7C D2 03 84 10 31 90 84 1A A4 2B 1D 04 14 84 D0 51 EA 2B 55
禁用
ASM686
选项以避免在构建 zlib 时出现此类错误或其他类型的错误。正如马克·阿德勒所说:
这些是第三方对 zlib 的贡献,不受支持。到期的 报告的压缩和解压问题 汇编代码,我计划将它们从 contrib 目录中删除 下一个版本。
我的帐户被错误锁定,无法访问我的 Facebook 帐户,因为我的姓名有误且无法确认身份。请检查我的帐户,因为我很想念它,我想尽快找回。谢谢你,Facebook 支持