Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QuaZip does not unpack zip compressed folders
Forum Updated to NodeBB v4.3 + New Features

QuaZip does not unpack zip compressed folders

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 520 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • posktomtenP Offline
    posktomtenP Offline
    posktomten
    wrote on last edited by posktomten
    #1

    Hello!
    I manage to unzip zip compressed binaries. But not zip-compressed folders with files.

    Example of error message

    
    "C:\\Users\\posktomten\\PROGRAMMING\\TEST\\quazip_test\\quazip_testprg\\bin\\tls\\qopensslbackend.dll"
    QIODevice::write (QFile, "C:\Users\posktomten\PROGRAMMERING\TEST\quazip_test\quazip_testprg\bin\tls\qopensslbackend.dll"): device not open
    
    

    All files seem to be found. But "device not open"
    Appreciate if someone with more knowledge than I have can give me a clue!

    The code:

    connect(ui->pbUnpack, &QPushButton::clicked, []() {
            QString pack = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/bin.zip";
            QString till = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/";
            QuaZip zip(pack);
    
            if(zip.open(QuaZip::mdUnzip)) {
                qDebug() << "OPEN";
    
                for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
                    // set source file in archive
                    QString filePath = zip.getCurrentFileName();
                    //  qDebug() << QDir::toNativeSeparators(till + filePath);
                    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.readAll();
                    //qDebug() << "ba " << ba;
                   // Doesn't work with QByteArray, I've tried!
                    // Reads all files
                    // close the source file
                    zFile.close();
                    // set destination file
                    QFile dstFile(till + filePath);
                    //qDebug() << till + filePath;
                    // All files are found
                    // open the destination file
                    dstFile.open(QIODevice::WriteOnly);
                    dstFile.write(ba.data());
                    //close the destination file
                    dstFile.close();
                }
            } else {
                qDebug() << "NOT OPEN";
            }
    
            zip.close();
        });
    

    posktomten

    C 1 Reply Last reply
    0
    • posktomtenP posktomten

      Hello!
      I manage to unzip zip compressed binaries. But not zip-compressed folders with files.

      Example of error message

      
      "C:\\Users\\posktomten\\PROGRAMMING\\TEST\\quazip_test\\quazip_testprg\\bin\\tls\\qopensslbackend.dll"
      QIODevice::write (QFile, "C:\Users\posktomten\PROGRAMMERING\TEST\quazip_test\quazip_testprg\bin\tls\qopensslbackend.dll"): device not open
      
      

      All files seem to be found. But "device not open"
      Appreciate if someone with more knowledge than I have can give me a clue!

      The code:

      connect(ui->pbUnpack, &QPushButton::clicked, []() {
              QString pack = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/bin.zip";
              QString till = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/";
              QuaZip zip(pack);
      
              if(zip.open(QuaZip::mdUnzip)) {
                  qDebug() << "OPEN";
      
                  for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
                      // set source file in archive
                      QString filePath = zip.getCurrentFileName();
                      //  qDebug() << QDir::toNativeSeparators(till + filePath);
                      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.readAll();
                      //qDebug() << "ba " << ba;
                     // Doesn't work with QByteArray, I've tried!
                      // Reads all files
                      // close the source file
                      zFile.close();
                      // set destination file
                      QFile dstFile(till + filePath);
                      //qDebug() << till + filePath;
                      // All files are found
                      // open the destination file
                      dstFile.open(QIODevice::WriteOnly);
                      dstFile.write(ba.data());
                      //close the destination file
                      dstFile.close();
                  }
              } else {
                  qDebug() << "NOT OPEN";
              }
      
              zip.close();
          });
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @posktomten Hi. Some thoughts:

      Your code does not check that the destination file is successfully opened before trying to write (and write() is the source of the error message).
      The destination location may not exist, i.e . ...quazip_test/quazip_testprg/.
      If it exists, the destination location may not be writeable. This seems unlikely given the path in your code.
      QFile will not automatically create intermediate directories. You may need to check for these (based on the path to the zip file entry and the base extract path) and create them as needed.

      1 Reply Last reply
      3
      • posktomtenP Offline
        posktomtenP Offline
        posktomten
        wrote on last edited by posktomten
        #3

        @ChrisW67 Thanks! I should have realized that!
        Now it works perfectly!

        connect(ui->pbUnpack, &QPushButton::clicked, []() {
                QString pack = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/bin.zip";
                QString till = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/";
                QuaZip zip(pack);
        
                if(zip.open(QuaZip::mdUnzip)) {
                    qDebug() << "OPEN";
                    QDir dir;
        
                    for(bool more = zip.goToFirstFile(); more; more = 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.readAll();
                        // Create folders recursively
                        dir.mkdir(till + filePath);
                        // close the source file
                        zFile.close();
                        // set destination file
                        QFile dstFile(till + filePath);
                        // open the destination file
                        dstFile.open(QIODevice::WriteOnly);
                        // Write data from QByteArray to file
                        dstFile.write(ba.data());
                        //close the destination file
                        dstFile.close();
                    }
                } else {
                    qDebug() << "NOT OPEN";
                }
        
                zip.close();
            });
        

        posktomten

        1 Reply Last reply
        0
        • posktomtenP posktomten has marked this topic as solved on
        • posktomtenP Offline
          posktomtenP Offline
          posktomten
          wrote on last edited by posktomten
          #4

          @ChrisW67
          I was way too fast. But now I dare to say that it works. But it is faster to unzip with QProcess and 7za.exe

          connect(ui->pbUnpack, &QPushButton::clicked, [this]() {
                  QString pack = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/svtplay-dl-4.28.1.zip";
                  //  QString till = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/";
                  QuaZip zip(pack);
          
                  if(zip.open(QuaZip::mdUnzip)) {
                      qDebug() << "OPEN";
                      QDir dir;
          
                      for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
                          // set source file in archive
                          QString filePath = zip.getCurrentFileName();
                          QFileInfo fi(filePath);
                          // The path to the folder
                          QString dirpath = fi.path();
                          // The folder is created recursively
                          dir.mkdir(dirpath);
                          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.readAll();
                          // close the source file
                          zFile.close();
                          QFile dstFile(filePath);
                          // open the destination file
                          dstFile.open(QIODevice::WriteOnly);
                          // Write data from QByteArray to file
                          dstFile.write(ba.data(), ba.size());
                          //close the destination file
                          dstFile.close();
                      }
                  } else {
                      qDebug() << "NOT OPEN";
                  }
          
                  zip.close();
                  qDebug() << "DONE";
              });
          

          posktomten

          1 Reply Last reply
          0
          • posktomtenP Offline
            posktomtenP Offline
            posktomten
            wrote on last edited by posktomten
            #5

            Error checking read/write

                connect(ui->pbUnpack, &QPushButton::clicked, [this]() {
                    QString ziped = "../svtplay-dl-4.28.1.zip";
                    QString unziped = "..";
                    QuaZip zip(ziped);
            
                    // Open the zip file for reading
                    if(zip.open(QuaZip::mdUnzip)) {
                        //  qDebug() << "Zip file opened successfully.";
                        QDir dir;
            
                        for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
                            QString filePath = zip.getCurrentFileName();
                            QFileInfo fi(filePath);
                            QString dirpath = fi.path();
                            dir.mkpath(unziped + "/" + dirpath);
                            QuaZipFile zFile(zip.getZipName(), filePath);
            
                            // Open the source file in the zip file for reading
                            if(zFile.open(QIODevice::ReadOnly)) {
                                // qDebug() << "Opened file in zip for reading: " << filePath;
                                QByteArray ba = zFile.readAll();
                                zFile.close();
                                QFile dstFile(unziped + "/" + filePath);
                                // Open the destination for writing
                                QFileInfo fi(dstFile);
            
                                // If it is a directory, an error message will appear
                                if(fi.isDir()) {
                                    continue;
                                }
            
                                if(dstFile.open(QIODevice::WriteOnly)) {
                                    if(dstFile.isOpen()) {
                                        dstFile.write(ba.data(), ba.size());
                                        dstFile.close();
                                        //  qDebug() << "Write successful.";
                                    } else {
                                        qDebug() << "Destination file is not open for writing.";
                                    }
                                } else {
                                    qDebug() << "Could not open destination file (dstFile) for writing: " << dstFile.errorString();
                                }
                            } else {
                                qDebug() << "Could not open file in zip for reading: " << filePath;
                            }
                        }
            
                        zip.close();
                        qDebug() << "Done.";
                    } else {
                        qDebug() << "Could not open zip file: " << ziped;
                    }
                });
            

            posktomten

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved