2015-07-03 451 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