首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取解压后的LZMA2文件大小(.xz / liblzma)

如何获取解压后的LZMA2文件大小(.xz / liblzma)
EN

Stack Overflow用户
提问于 2010-01-31 21:14:54
回答 1查看 4.5K关注 0票数 5

我正在寻找一种方法来获得使用xz实用程序压缩的LZMA2 / .xz文件的未压缩流大小。

我使用来自Windows/Linux的liblzma来完成这个任务,所以我想我应该在liblzma中寻找一些C/C++ API来完成这个任务。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-01 10:32:55

我想我找到了一个解决方案。

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

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

代码语言:javascript
运行
复制
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;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2171775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档