LzmaLib:C中的压缩/解压缩缓冲区

时间:2016-11-22 02:43:08

标签: c compression 7zip lzma

我正在尝试将LzmaLibLzmaCompress()LzmaDecompress()用于缓冲区,并调整提供的示例here

我正在使用~3MB缓冲区进行测试,压缩函数似乎工作正常(产生~1.2MB的压缩缓冲区),但是当我尝试解压缩时,它只提取~300字节并返回SZ_ERROR_DATA

提取的少数字节是正确的,但我不知道为什么它会停在那里。

我的代码:

#include <stdio.h>
#include <stdlib.h>

#include "LzmaLib.h"

void compress(
    unsigned char **outBuf, size_t *dstLen,
    unsigned char *inBuf, size_t srcLen)
{
    unsigned propsSize = LZMA_PROPS_SIZE;
    *dstLen = srcLen + srcLen / 3 + 128;

    *outBuf = (unsigned char*)malloc(propsSize + *dstLen);

    int res = LzmaCompress(
        (unsigned char*)(*outBuf + LZMA_PROPS_SIZE), dstLen,
        inBuf, srcLen,
        *outBuf, &propsSize,
        -1, 0, -1, -1, -1, -1, -1);

    assert(res == SZ_OK);

    *dstLen = *dstLen + LZMA_PROPS_SIZE;
}

void uncompress(
    unsigned char **outBuf, size_t *dstLen,
    unsigned char *inBuf,  size_t srcLen
) {
    *dstLen = 5000000;
    *outBuf = (unsigned char*)malloc(*dstLen);

    srcLen = srcLen - LZMA_PROPS_SIZE;
    int res = LzmaUncompress(
        *outBuf, dstLen,
        (unsigned char*)(inBuf + LZMA_PROPS_SIZE), &srcLen,
        inBuf, LZMA_PROPS_SIZE);

    assert(res == SZ_OK);
}

void do_compress() {
    FILE* file = fopen("Module.dll", "r");
    size_t size, decSize;
    unsigned char *data, *dec = NULL;

    fseek(file, 0L, SEEK_END);
    size = ftell(file);
    fseek(file, 0L, SEEK_SET);

    data = (unsigned char*)malloc(size);
    fread(data, 1, size, file);
    fclose(file);

    compress((unsigned char**)&dec, &decSize, data, size);

    file = fopen("Module.lzma", "w");
    fwrite(dec, 1, decSize, file);
    fclose(file);
}

void do_uncompress() {
    FILE* file = fopen("Module.lzma", "r");
    size_t size, decSize;
    unsigned char *data, *dec = NULL;

    fseek(file, 0L, SEEK_END);
    size = ftell(file);
    fseek(file, 0L, SEEK_SET);

    data = (unsigned char*)malloc(size);
    fread(data, 1, size, file);
    fclose(file);

    uncompress((unsigned char**)&dec, &decSize, data, size);

    file = fopen("Module_DEC.dll", "w");
    fwrite(dec, 1, decSize, file);
    fclose(file);
}

int main()
{
    do_compress();
    do_uncompress();

    return 0;
}

如果此代码不是使用LzmaLib压缩缓冲区的更好方法,我很乐意接受建议。

3 个答案:

答案 0 :(得分:8)

我打赌这个问题潜伏在你如何读/写文件中。您需要以二进制模式打开它们以防止在读/写操作期间进行任何替换。

更改以下所有实例:

  • fopen(xxx, "r") - &gt; fopen(xxx, "rb")
  • fopen(xxx, "w") - &gt; fopen(xxx, "wb")

答案 1 :(得分:0)

我没有特别检查LzmaCompress,但大多数其他压缩库如libz处理的功能类似于标准的读/写或fread / fwrite函数,即允许你连续调用用于在一个流中压缩越来越多的数据的功能。因此,在某些时候,你必须说'我已经完成了,请刷新目前未编写的所有内容&#34;。可能,你忘记了那一部分。如果没有,Minimal, Complete, and Verifiable example会很酷。

答案 2 :(得分:0)

压缩时,将压缩输出字节数传递给调用者。但是您的缓冲区包含LZMA_PROPS_SIZE个附加字节。因此,在编写lzma文件时,您实际上忘记了最后LZMA_PROPS_SIZE个字节,并且在稍后阅读时,这些字节丢失了。