QUncompress can't read a file which was compressed with qCompress



  • Alright, all I want to do is to compress and uncompress a file with qCompress/qUncompress:

    @QCoreApplication a(argc, argv);
    QStringList args = QCoreApplication::arguments();

    if(args.count() > 1){

    if(args.at(1) == "-d"){//Decompress
    QFile source(args.at(2));
    if(source.open(QFile::ReadOnly)){
    QByteArray compressedData = source.readAll();
    QByteArray deCompressedData = qUncompress(compressedData);
    source.close();
    QFileInfo info(source);
    QDir destinationDir(info.dir());
    QFileInfo destinationInfo(destinationDir, info.baseName());

    QFile destination(destinationInfo.absoluteFilePath());
    destination.open(QFile::WriteOnly);
    QTextStream destStream(&destination);
    destStream << deCompressedData;
    destStream.flush();
    destination.close();
    

    }
    }
    else{//Compress
    QFile source(args.at(1));
    if(source.open(QFile::ReadOnly)){
    QByteArray compressedData = qCompress(source.readAll());
    source.close();
    QFileInfo info(source);
    QDir destinationDir(info.dir());
    QFileInfo destinationInfo(destinationDir, info.fileName() + ".gz");

    QFile destination(destinationInfo.absoluteFilePath());
    destination.open(QFile::WriteOnly);
    QTextStream destStream(&destination);
    destStream << compressedData;
    destStream.flush();
    destination.close();
    

    }
    }
    }@

    Oddly, qUncompress gives me "qUncompress: Z_DATA_ERROR: Input Data is corrupted", eventhough the original data was compressed with qCompress?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.