c - 无法提取根据 IETF RFC 1951 : "DEFLATE Compressed Data Format Specification version 1.3" 压缩的数据

标签 c compression zlib

我想解压缩 RFC 1951 中指定的 DEFLATE 压缩格式(或应该按照我所指的规范)的数据。我在 C 中使用 zlib 库。 我在github上引用了这个例子: https://gist.github.com/gaurav1981/9f8d9bb7542b22f575df

并修改它只是为了解压缩我的数据:

    char dData[MAX_LENGTH];
    char cData[MAX_LENGTH]; 

    for(i=0; i < (size-4); i++)
    {
        cData[i] = *(data + i);
    }
    //cData[i] = '\0';
    printf("Compressed size is: %lu\n", strlen(cData));

    z_stream infstream;
    infstream.zalloc = Z_NULL;
    infstream.zfree = Z_NULL;
    infstream.opaque = Z_NULL;
    // setup "b" as the input and "c" as the compressed output
    //infstream.avail_in = (uInt)((char*)defstream.next_out - b); // size of input
    //infstream.avail_in = (uInt)((char*)defstream.next_out - cData);
    infstream.avail_in = (uInt)(size - 4);
    infstream.next_in = (Bytef *)cData; // input char array
    infstream.avail_out = (uInt)sizeof(dData); // size of output
    infstream.next_out = (Bytef *)dData; // output char array

    // the actual DE-compression work.
    inflateInit(&infstream);
    inflate(&infstream, Z_NO_FLUSH);
    inflateEnd(&infstream);

    printf("Uncompressed size is: %lu\n", strlen(dData));

    size = strlen(dData);

我的未压缩大小是 0。那么有人可以告诉我的代码有什么问题吗?

我什至将数据写入文件并将其保存为 .gz 和 .zip,但当我尝试提取它时出现错误(我正在运行 ubuntu 14.04)

如果可能的话,有人可以分析我的数据并提取它吗?我的数据:

6374 492d 2d29 4ece c849 cc4b
294a 4cc9 cc57 f02e cd29 292d 6292 7780
30f2 1293 338a 3293 334a 52f3 98c4 0b9c
4a93 33b2 8b32 4b32 b399 d405 4212 d353
8b4b 320b 0a00 

最佳答案

您需要调用 inflateInit2()(第二个参数为 -15),而不是 inflate(),以解压缩原始数据缩小数据。

关于c - 无法提取根据 IETF RFC 1951 : "DEFLATE Compressed Data Format Specification version 1.3" 压缩的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43697250/

相关文章:

通过宏组合两个指定的初始化器

c - 如何编写一个程序来列出用户输入的数字的所有可能的三位数加法解决方案。 (不能使用零)

c - 为什么从 int 的地址读回不给我返回 int 的正确值(C 和 gdb)

javascript - 使用 JavaScript 压缩包含嵌入图像的 HTML 字符串

f# - 无法在 F# 中创建 ZipArchive

ruby-on-rails - 当我不断收到此 zlib 错误时,如何使用 gem 进行安装?

c - 如何像在 jdoodle 中那样在 C 在线编译器中指定文件结尾 (EOF)?

c - 为什么我的解压缩显示错误?

multithreading - 如何使用多线程进行 zlib 压缩

.net - .net 中用于 deflate、gzip 和 zlib 的一个库