如何将 deflate/inflate SetDictionary 与原始 deflate/inflate 一起使用?

How to use deflate/inflate SetDictionary with raw deflate/inflate?

我正在尝试了解使用字典时原始压缩的功能。我知道以下内容。 1. 当我们使用字典时,应用程序应该为 deflate() 和 inflate() 提供相同的字典。 2. 在进行原始放气时,必须在任何放气调用之前或放气块完成后立即调用此函数,即在使用任何冲洗选项时消耗所有输入并交付所有输出之后 Z_BLOCKZ_PARTIAL_FLUSHZ_SYNC_FLUSHZ_FULL_FLUSH。 (来自 zlib 文档)。

但是以下应用程序无法解压缩先前使用同一应用程序压缩的内容。压缩解压成功但输入文件与解压文件不匹配

放气:

    do {
        ret = deflateSetDictionary(&strm, dictionary, sizeof(dictionary));
        if(ret != Z_OK) {
            fprintf(stderr, "Failed to set deflate dictionary\n");
            return Z_STREAM_ERROR;
        }
        strm.avail_in = fread(in, 1, CHUNK, source);
        if (ferror(source)) {
            (void)deflateEnd(&strm);
            return Z_ERRNO;
        }
        flush = feof(source) ? Z_FINISH : Z_FULL_FLUSH;
        strm.next_in = in;

        /* run deflate() on input until output buffer not full, finish
           compression if all of source has been read in */
        do {
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = deflate(&strm, flush);    /* no bad return value */
            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
            have = CHUNK - strm.avail_out;
            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
                (void)deflateEnd(&strm);
                return Z_ERRNO;
            }
        } while (strm.avail_out == 0);
        assert(strm.avail_in == 0);     /* all input will be used */

        /* done when last data in file processed */
    } while (flush != Z_FINISH);
    assert(ret == Z_STREAM_END);     

膨胀:

    do {
        ret = inflateSetDictionary(&strm, dictionary, sizeof(dictionary));
        if(ret != Z_OK) {
            fprintf(stderr, "Failed to set inflate dictionary\n");
            return Z_STREAM_ERROR;
        }
        strm.avail_in = fread(in, 1, CHUNK, source);
        if (ferror(source)) {
            (void)inflateEnd(&strm);
            return Z_ERRNO;
        }
        if (strm.avail_in == 0)
            break;
        strm.next_in = in;


        /* run inflate() on input until output buffer not full */
        do {
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = inflate(&strm, Z_FULL_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 ret;
            }
            have = CHUNK - strm.avail_out;
            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
                (void)inflateEnd(&strm);
                return Z_ERRNO;
            }
        } while (strm.avail_out == 0);

        /* done when inflate() says it's done */
    } while (ret != Z_STREAM_END);

放气时,每 CHUNK 个输入字节设置相同的字典。为什么?您应该在 deflateInit2() 之后使用一次 deflateSetDictionary()。从那时起,输入数据本身应该作为比您可能提供的字典更好的匹配字符串来源。

在膨胀方面,您必须知道压缩块的结束位置,以便您可以在与压缩时完全相同的位置执行 inflateSetDictionary()。这将需要某种标记、计数或搜索完整冲洗模式。