2015-07-03 56 views
0

我正在使用qUncompress的Qt和頭問題,我無法找到更多的信息來解決它。 這裏是我的代碼:qUncompress:Z_DATA_ERROR:輸入數據已損壞

#include <QCoreApplication> 
    #include <QByteArray> 
    #include <QFile> 
    #include <QDebug> 

    int main(int argc, char *argv[]) 
    { 
     QCoreApplication a(argc, argv); 
     QFile f("/user/XXXXX/home/AgeRegression"); // hided 
     if (f.exists()) 
      qDebug() << "File exists"; 
     else 
      qDebug() << "Missing file"; 
     f.open(QIODevice::ReadOnly); 

     QByteArray qb = f.readAll(); 
     qb = qUncompress(qb); 
     qDebug()<<"Successfully"; 
     const char *qt_version = qVersion(); 
     qDebug()<< QString(qt_version); 
     return a.exec(); 
    } 

這裏是輸出:

 File exists 
    qUncompress: Z_DATA_ERROR: Input data is corrupted 
    Successfully 
    "5.3.2" 

出自QT的文件(你可以找到here):

Note: If you want to use this function to uncompress external data that was compressed using zlib, you first need to prepend a four byte header to the byte array containing the data. The header must contain the expected length (in bytes) of the uncompressed data, expressed as an unsigned, big-endian, 32-bit integer.

那麼究竟應該怎麼辦這裏?我是否必須找到未壓縮數據的長度(有沒有辦法?我只是有壓縮數據。)?一個例子,將不勝感激。

回答

1

qUncompress不是通用的解壓縮功能。它只能與使用qCompress壓縮的數據一起使用。

如果你的數據是用比其他qCompress壓縮的東西,你必須以同樣的方式解壓 - 使用zlib的直接使用外部工具等

使用qUncompress像你一樣,你依賴在任何時候可能會改變的實現細節。不要這樣做。簡單地假設qCompress是一個黑盒子,並使用其他人無法實現的外部壓縮器實現。

+0

我知道這可能是危險的,你的方式當然更安全。但是這個代碼只是用來測試我在測試一個庫時遇到的問題。謝謝。 :) – nguyeh