我正在寻找一种方法来获取使用xz实用程序压缩的LZMA2/.xz文件的解压缩流大小。如何获得LZMA2文件的解压缩大小(.xz/liblzma)
我使用Windows/Linux的liblzma来完成这个任务,所以我想我正在寻找一些liblzma中的C/C++ API来完成这个任务。
我正在寻找一种方法来获取使用xz实用程序压缩的LZMA2/.xz文件的解压缩流大小。如何获得LZMA2文件的解压缩大小(.xz/liblzma)
我使用Windows/Linux的liblzma来完成这个任务,所以我想我正在寻找一些liblzma中的C/C++ API来完成这个任务。
我想我找到了解决方案。
这是一个非常粗糙的代码示例,但似乎工作正常。
我假设我有一个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;
}
已经从sourceforge下载源和这里一看,我引用这个从主头文件LzmaLib.h
/* LzmaUncompress -------------- In: dest - output data destLen - output data size src - input data srcLen - input data size Out: destLen - processed output size srcLen - processed input size Returns: SZ_OK - OK SZ_ERROR_DATA - Data error SZ_ERROR_MEM - Memory allocation arror SZ_ERROR_UNSUPPORTED - Unsupported properties SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) */ MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, const unsigned char *props, size_t propsSize);
它看起来是destLen
是未压缩的数据的大小。
希望这会有所帮助, 最好的问候, 汤姆。
我觉得XZ文件格式不直LZMA,而是对各种压缩算法 – 2010-01-31 13:44:57
我想LZMA2机库用于编写Hadoop的压缩codec.If您有任何API文档或包装帮助压缩/解压相关的理解将会非常有帮助。 – samarth 2012-04-16 12:47:42
有两个问题。 (1)你需要在调用'lzma_index_buffer_decode'之前初始化'memlimit',或者在某些情况下它会返回'LZMA_MEMLIMIT_ERROR'。 (2)你不应该调用'lzma_index_init'。 'index'的初始值被'lzma_index_buffer_decode'忽略。 – Patrick 2013-10-02 16:42:39