2012-02-09 106 views
0

我是LZMA解​​壓縮資源文件,我之前使用lzma e <infile> <outfile> -lc0 -lp2從終端壓縮並導入到我的項目中。然而,當應用於該文件時LzmaDec_DecodeToBuf在第一次迭代中返回1,即LZMA數據錯誤。 (此時inLen總是5,outLen0。)以編程方式解壓LZMA靜態壓縮文件

爲什麼這樣?

我的代碼如下:

SRes static decompress(FILE *inFile, FILE *outFile) 
{ 
    // Position the inFile pointer at the start. 
    fseek(inFile, 0, SEEK_SET); 

    // Read in LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) to header. 
    char unsigned header[LZMA_PROPS_SIZE+8]; 
    fgets(header, sizeof(header), inFile); 
    CLzmaDec state; 
    LzmaDec_Construct(&state); 
    SRes res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &SzAllocForLzma); 

    if (res != SZ_OK) { 
    // Free all allocated structures. 
    LzmaDec_Free(&state, &SzAllocForLzma); 
    return res; 
    } 

    char unsigned inBuf[IN_BUF_SIZE]; 
    char unsigned outBuf[OUT_BUF_SIZE]; 
    LzmaDec_Init(&state); 

    ELzmaStatus status; 
    long unsigned outLen = sizeof(outBuf); 
    long unsigned inLen = sizeof(inBuf); 
    long unsigned inPos = ftell(inFile); 

    while (fgets(inBuf, sizeof(inBuf), inFile) != NULL) { 
    inLen = MIN(sizeof(inBuf), MAX(ftell(inFile)-inPos, 0)); 
    outLen = sizeof(outBuf); 

    SRes res = LzmaDec_DecodeToBuf(&state, 
            outBuf, 
            &outLen, 
            inBuf, 
            &inLen, 
            LZMA_FINISH_ANY, 
            &status); 
// continues... 
+0

水晶球霧。嘗試調試,看看它出錯的地方。但嚴肅地說,這是一個非常難以嘗試和互聯網調試的問題。也許你錯過了或向數據添加了標題?也許你正在傳遞垃圾?也許你意外地跺了一些數據。調試可能會指向正確的方向,但我希望你好運。 – 2012-02-09 22:21:34

回答

0

你確定輸入不是7zArchive?這需要調用SzArEx_Open和SzArEx_Extract。

0

這是一個比較舊的帖子,要回復,但是我遇到了同樣的問題。

問題是標題不應該是你正在解壓的數據的一部分。解決的辦法是從sizeof(header)而不是,當讀取數據時,不要忘記也調整它的總長度sizeof(header)以及。