ZLib 创建损坏的存档
ZLib creates a damaged archive
我使用zlib 1.211作为动态库和VS2019
我从 txt 文件创建一个简单的 zip
FILE* input = fopen("D:\1.txt", "rb");
FILE* output = fopen("D:\1.zip", "wb");
/* do compression if no arguments */
bool ok = compress_file(input, output);
fclose(output);
fclose(input);
压缩函数为:
bool compress_file(FILE* src, FILE* dst)
{
uint8_t inbuff[CHUNK_SIZE];
uint8_t outbuff[CHUNK_SIZE];
z_stream stream = { 0 };
if (deflateInit(&stream, COMPRESSION_LEVEL) != Z_OK)
{
fprintf(stderr, "deflateInit(...) failed!\n");
return false;
}
int flush;
do {
stream.avail_in = fread(inbuff, 1, CHUNK_SIZE, src);
if (ferror(src))
{
fprintf(stderr, "fread(...) failed!\n");
deflateEnd(&stream);
return false;
}
flush = feof(src) ? Z_FINISH : Z_NO_FLUSH;
stream.next_in = inbuff;
do {
stream.avail_out = CHUNK_SIZE;
stream.next_out = outbuff;
deflate(&stream, flush);
uint32_t nbytes = CHUNK_SIZE - stream.avail_out;
if (fwrite(outbuff, 1, nbytes, dst) != nbytes ||
ferror(dst))
{
fprintf(stderr, "fwrite(...) failed!\n");
deflateEnd(&stream);
return false;
}
} while (stream.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&stream);
return true;
}
但是当我尝试通过 TotalCommander 或 WinRAR 打开创建的 zip 时,我得到 "Error in package file"。
zlib 不制作 zip 存档。您的代码生成 zlib 流。两种不同的东西。
我使用zlib 1.211作为动态库和VS2019
我从 txt 文件创建一个简单的 zip
FILE* input = fopen("D:\1.txt", "rb");
FILE* output = fopen("D:\1.zip", "wb");
/* do compression if no arguments */
bool ok = compress_file(input, output);
fclose(output);
fclose(input);
压缩函数为:
bool compress_file(FILE* src, FILE* dst)
{
uint8_t inbuff[CHUNK_SIZE];
uint8_t outbuff[CHUNK_SIZE];
z_stream stream = { 0 };
if (deflateInit(&stream, COMPRESSION_LEVEL) != Z_OK)
{
fprintf(stderr, "deflateInit(...) failed!\n");
return false;
}
int flush;
do {
stream.avail_in = fread(inbuff, 1, CHUNK_SIZE, src);
if (ferror(src))
{
fprintf(stderr, "fread(...) failed!\n");
deflateEnd(&stream);
return false;
}
flush = feof(src) ? Z_FINISH : Z_NO_FLUSH;
stream.next_in = inbuff;
do {
stream.avail_out = CHUNK_SIZE;
stream.next_out = outbuff;
deflate(&stream, flush);
uint32_t nbytes = CHUNK_SIZE - stream.avail_out;
if (fwrite(outbuff, 1, nbytes, dst) != nbytes ||
ferror(dst))
{
fprintf(stderr, "fwrite(...) failed!\n");
deflateEnd(&stream);
return false;
}
} while (stream.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&stream);
return true;
}
但是当我尝试通过 TotalCommander 或 WinRAR 打开创建的 zip 时,我得到 "Error in package file"。
zlib 不制作 zip 存档。您的代码生成 zlib 流。两种不同的东西。