You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LibLZMA解压Apache Commons Compression生成的XZ文件遇LZMA_BUF_ERROR

问题:LibLZMA解压Apache Commons Compression生成的XZ文件触发LZMA_BUF_ERROR

我正在使用LibLZMA(即xz-utils)解压由apache-commons-compression库生成的二进制文件,已确认Java程序输出的是标准XZ二进制文件,且7zip识别其格式为lzma2:23 crc64。

我的解压函数如下:

void decompress_content(const uint8_t *compressed_data, int compressed_length)
{
    lzma_stream strm = LZMA_STREAM_INIT;
    lzma_ret ret = lzma_stream_decoder(&strm, UINT32_MAX, LZMA_CONCATENATED);

    if (ret != LZMA_OK)
        throw std::runtime_error("Failed to initialize XZ decoder.");

    std::vector<uint8_t> decompressed_data;
    uint8_t outbuf[65536];

    strm.next_in = compressed_data;
    strm.avail_in = compressed_length;

    strm.next_out = outbuf;
    strm.avail_out = sizeof(outbuf);

    try
    {
        while (true)
        {
            ret = lzma_code(&strm, LZMA_RUN);

            // Check if output data was produced
            if (strm.avail_out < sizeof(outbuf))
            {
                size_t write_size = sizeof(outbuf) - strm.avail_out;
                decompressed_data.insert(decompressed_data.end(), outbuf, outbuf + write_size);
                strm.next_out = outbuf;
                strm.avail_out = sizeof(outbuf);
            }

            if (ret == LZMA_STREAM_END)
            {
                break;
            }

            if (ret == LZMA_OK)
            {
                continue;
            }

            if (ret == LZMA_BUF_ERROR && strm.avail_in == 0)
            {
                // No more input data, and decoder cannot make progress
                lzma_end(&strm);
                throw std::runtime_error("Compressed data is truncated or corrupted.");
            }
            // Other errors
            lzma_end(&strm);
            handle_lzma_error(ret);
        }
    }
    catch (...)
    {
        lzma_end(&strm);
        throw;
    }

    lzma_end(&strm);
    parse_entries(decompressed_data);
}

执行该函数时触发错误:Compressed data is truncated or corrupted.,对应返回码LZMA_BUF_ERROR。

相关XZ二进制数据如下:

unsigned char rawData[80] = {
    0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00, 0x00, 0x04, 0xE6, 0xD6, 0xB4, 0x46,
    0x02, 0x00, 0x21, 0x01, 0x16, 0x00, 0x00, 0x00, 0x74, 0x2F, 0xE5, 0xA3,
    0x01, 0x00, 0x17, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x5F, 0x77, 0x6F, 0x72,
    0x6C, 0x64, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x04, 0x74,
    0x65, 0x73, 0x74, 0x00, 0x78, 0x83, 0x76, 0x08, 0x82, 0x49, 0x3C, 0x9C,
    0x00, 0x01, 0x30, 0x18, 0x8E, 0x1B, 0xAC, 0xEC, 0x1F, 0xB6, 0xF3, 0x7D,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5A
};

内容的提问来源于stack exchange,提问作者Rabbit0w0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:47:33