linux解压lzma,如何获得LZMA2文件的解压缩大小(.xz/liblzma)

我想我找到了解决方案。

这是一个非常粗糙的代码示例,但似乎工作正常。

我假设我有一个do_mmap()函数将整个文件作为只读映射到内存中,并返回映射的总大小。 这可以自然适应使用read/fread/ReadFile或任何其他File API。

extern size_t get_uncompressed_size(const char *filename)

{

lzma_stream_flags stream_flags;

int file_size;

const uint8_t *data = (uint8_t *) do_mmap(filename, &file_size);

// 12 is the size of the footer per the file-spec...

const uint8_t *footer_ptr = data + file_size - 12;

// Something is terribly wrong

if (footer_ptr < data) {

do_unmap((void *)data, file_size);

return -1;

}

// Decode the footer, so we have the backward_size pointing to the index

lzma_stream_footer_decode(&stream_flags, (const uint8_t *)footer_ptr);

// This is the index pointer, where the size is ultimately stored...

const uint8_t *index_ptr = footer_ptr - stream_flags.backward_size;

// Allocate an index

lzma_index *index = lzma_index_init(NULL);

uint64_t memlimit;

size_t in_pos = 0;

// decode the index we calculated

lzma_index_buffer_decode(&index, &memlimit, NULL, index_ptr, &in_pos, footer_ptr - index_ptr);

// Just make sure the whole index was decoded, otherwise, we might be

// dealing with something utterly corrupt

if (in_pos != stream_flags.backward_size) {

do_unmap((void *)data, file_size);

lzma_index_end(index, NULL);

return -1;

}

// Finally get the size

lzma_vli uSize = lzma_index_uncompressed_size(index);

lzma_index_end(index, NULL);

return (size_t) uSize;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值