xz ユーティリティで圧縮された LZMA2 / .xz ファイルの非圧縮ストリーム サイズを取得する方法を探しています。
私はこのタスクに Windows/Linux の liblzma を使用しているので、このトリックを実行する liblzma の C/C++ API を探していると思います。
私は解決策を見つけたと思います。
これは非常に大まかなコード サンプルですが、問題なく動作するようです。
ファイル全体を読み取り専用としてメモリにマップし、マップされた合計サイズを返す do_mmap() 関数があると仮定しています。これは当然、read/fread/ReadFile またはその他のファイル 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 -------------- の: dest-出力データ destLen-出力データサイズ src-入力データ srcLen-入力データサイズ 外: destLen-処理された出力サイズ srcLen-処理された入力サイズ 戻り値: SZ_OK-OK SZ_ERROR_DATA-データエラー SZ_ERROR_MEM-メモリ割り当ての遅延 SZ_ERROR_UNSUPPORTED-サポートされていないプロパティ SZ_ERROR_INPUT_EOF-入力バッファ(src)にさらにバイトが必要です * / MY_STDAPI LzmaUncompress(unsigned char * dest、size_t * destLen、const unsigned char * src、SizeT * srcLen、 const unsigned char * props、size_t propsSize);
destLen
圧縮されていないデータのサイズであるように見えます。
これがお役に立てば幸いです、よろしく、トム。