我正在尋找一種方法來獲取使用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