【问题标题】:Programmatically LZMA decompressing a static LZMA-compressed file以编程方式 LZMA 解压缩静态 LZMA 压缩文件
【发布时间】:2012-02-09 22:17:20
【问题描述】:

我正在对一个资源文件进行 LZMA 解压缩,该文件我之前使用终端中的lzma e <infile> <outfile> -lc0 -lp2 压缩并导入到我的项目中。但是,当应用于此文件时,LzmaDec_DecodeToBuf 在第一次迭代中返回 1,即 LZMA 数据错误。 (到那时inLen 总是5outLen0。)

这是为什么呢?

我的代码如下:

SRes static decompress(FILE *inFile, FILE *outFile)
{
  // Position the inFile pointer at the start.
  fseek(inFile, 0, SEEK_SET);

  // Read in LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) to header.
  char unsigned header[LZMA_PROPS_SIZE+8];
  fgets(header, sizeof(header), inFile);
  CLzmaDec state;
  LzmaDec_Construct(&state);
  SRes res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &SzAllocForLzma);

  if (res != SZ_OK) {
    // Free all allocated structures.
    LzmaDec_Free(&state, &SzAllocForLzma);
    return res;
  }

  char unsigned inBuf[IN_BUF_SIZE];
  char unsigned outBuf[OUT_BUF_SIZE];
  LzmaDec_Init(&state);

  ELzmaStatus status;
  long unsigned outLen = sizeof(outBuf);
  long unsigned inLen = sizeof(inBuf);
  long unsigned inPos = ftell(inFile);

  while (fgets(inBuf, sizeof(inBuf), inFile) != NULL) {
    inLen = MIN(sizeof(inBuf), MAX(ftell(inFile)-inPos, 0));
    outLen = sizeof(outBuf);

    SRes res = LzmaDec_DecodeToBuf(&state,
                                   outBuf,
                                   &outLen,
                                   inBuf,
                                   &inLen,
                                   LZMA_FINISH_ANY,
                                   &status);
// continues...

【问题讨论】:

  • 水晶球有雾。尝试调试,看看哪里出错了。但说真的,这是一个非常难以尝试和互联网调试的问题。也许您缺少或在数据中添加了标题?也许你正在传递垃圾?也许你不小心踩到了它的一些数据。调试可能会为您指明正确的方向,但祝您好运。

标签: objective-c c ios lzma


【解决方案1】:

这是一个相当老的帖子要回复,但是我遇到了同样的问题。

问题是标题不应该是您正在解压缩的数据的一部分。解决方案是在读取数据时从 sizeof(header) 而不是 0 开始,并且不要忘记将其总长度也调整为 sizeof(标题)

【讨论】:

    【解决方案2】:

    您确定输入不是 7zArchive 吗?这需要调用 SzArEx_Open 和 SzArEx_Extract。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 2012-03-22
      相关资源
      最近更新 更多