Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

.DAT file not extracted using Quazip library in qt



  • I am trying to extract .DAT file using Quazip library in Qt 5.14. I integrated this quazip library into my project and try to extract files using it. .txt file is get extracted but .DAT file is not get extracted. .DAT files are created but it does not contain any data. Here is my source code

    bool fileHelper::extractAll( QString folderPath, QString filePath ) {
        QuaZip zip(filePath);
        zip.open(QuaZip::mdUnzip);
        bool isSuccess = false;
        for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile())
        {
            // set source file in archive
            QString filePath = zip.getCurrentFileName();
            QuaZipFile zFile( zip.getZipName(), filePath );
            // open the source file
            zFile.open( QIODevice::ReadOnly );
            // create a bytes array and write the file data into it
            //QByteArray ba = zFile.read()
            QByteArray ba = zFile.readAll();
            // close the source file
            zFile.close();
            // set destination file
            //QFile dstFile( getfileStoreRootDir()+filePath );
            QFile dstFile( folderPath+filePath );
            qDebug() << "dstFile :" << dstFile;
            // open the destination file
            dstFile.open( QIODevice::WriteOnly | QIODevice::Text );
            // write the data from the bytes array into the destination file
            dstFile.write( ba.data() );
            //close the destination file
            dstFile.close();
            //mark extraction sucess
            isSuccess = true;
        }
        zip.close();
        return isSuccess; 
    }
    

    Please tell me am I doing something wrong or any other extra flag or something is required for it.


  • Lifetime Qt Champion

    You should check if the file which not gets extracted really contains data with another program which can extract zip files.
    Then add some debug output to see the size of ba which is returned by zFile.readAll(). Also IODevice::Text is for sure not correct for non text-files.
    And you should check if QFile::open() and write() really succeed - they have return values for a reason...



  • yes I tried that. if I directly extract zip file then the 3mb file is created but when I tried to extract using code then 0kb file is created. and I also print debug log for ba, it prints nothing.


  • Lifetime Qt Champion

    Then there seems to be a problem inside QUAZip - nothing we can do against.



  • so can you suggest any other library or method which solves this problem


  • Lifetime Qt Champion

    QUZiap is open source. Simply debug into the functions to see what's going wrong in there.



  • Try this:

    bool fileHelper::extractAll(QString folderPath, QString filePath) {
        QuaZip zip(filePath);
        if(!zip.open(QuaZip::mdUnzip))
            return false;
        QuaZipFile file(&zip);
        for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile()){
            if(!file.open(QIODevice::ReadOnly))
                return false;
            QFile dstFile(folderPath+zip.getCurrentFileName());
            if(!dstFile.open(QIODevice::WriteOnly))
                return false;
            while(!file.atEnd())
                dstFile.write(file.read(1024*1024));
            dstFile.close();
            file.close();
        }
        zip.close();
        return true;
    }
    

    If it still doesn't work

    can you suggest any other library

    KArchive https://api.kde.org/frameworks/karchive/html/index.html



  • it worked. thanks @VRonin