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时,出现“包文件错误”。

评论