使用 zlib uncompress() 时出现未处理的异常
Unhandled exception while using zlib uncompress()
我在使用 zlib uncompress() 函数执行解压缩时遇到了一些问题。我压缩的文件作为 fileSource 传递给 uncompressFile(),没有抛出运行时异常并且工作得很好。我所做的基本上是使用 fread 从文件中读取字节,然后将其存储在缓冲区中,然后 compressing/decompressing。
我的解压缩函数:
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 (none),所以基本上没有压缩,我的 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 时出现这种或其他类型的错误。正如马克阿德勒所说:
Those are third-party contributions to zlib and are not supported. Due
to reported issues with both the compression and decompression
assembler code, I plan to remove them from the contrib directory in
the next version.
我在使用 zlib uncompress() 函数执行解压缩时遇到了一些问题。我压缩的文件作为 fileSource 传递给 uncompressFile(),没有抛出运行时异常并且工作得很好。我所做的基本上是使用 fread 从文件中读取字节,然后将其存储在缓冲区中,然后 compressing/decompressing。 我的解压缩函数:
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 (none),所以基本上没有压缩,我的 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 时出现这种或其他类型的错误。正如马克阿德勒所说:
Those are third-party contributions to zlib and are not supported. Due to reported issues with both the compression and decompression assembler code, I plan to remove them from the contrib directory in the next version.