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. walk a directory while ziping the directory

walk a directory while ziping the directory

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 452 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.
  • StyxS Offline
    StyxS Offline
    Styx
    wrote on last edited by
    #1

    I am trying to walk a directory the user selects the directory and zip file name. When I go to add the filepath of the directory and the file name it adds the entire file path..

    example:

    "C:\Users\1\Desktop\2\3\release\4\5\Numbers"

    I am trying to append the last folder name while walking the directory.

    CODE:

    void MainWindow::saveFile()
    {
        QString existingDirectory = QFileDialog::getExistingDirectory(0, ("Select Zip Folder"),QDir::currentPath());
    	QString zipFileName = QFileDialog::getSaveFileName(this, tr("Save Zip File Name"), "", tr("Zip Files (*.zip)"));
    	
    	qint32 err = 0;
        struct zip *packZip = zip_open(zipName.toLatin1().constData(), ZIP_CREATE | ZIP_EXCL, &err);
        if (!packZip)
        {
            zip_error_t error;
            zip_error_init_with_code(&error, err);
            QMessageBox::warning(nullptr, "",QString("Can't pack open packed zip file %1 with error %2 and err_code %3").arg(zipFileName, zip_error_strerror(&error)).arg(err));
            zip_error_fini(&error);
            return false;
        }
    
        walkDirectory(existingDirectory, packZip);
        zip_close(packZip);
    }
    
    void MainWindow::walkDirectory( const QString& sDir, struct zip *packZip)
    {
        QDir dir(sDir);
        dir.setCurrent(sDir);
        QFileInfoList list = dir.entryInfoList();
        for (int iList=0;iList<list.count();iList++)
        {
            QFileInfo info = list[iList];
            QString sFilePath = info.filePath();
            if (info.isDir())
            {
                // recursive
                if (info.fileName()!=".." && info.fileName()!=".")
                {
                    if (zip_dir_add(packZip, sFilePath.toLatin1().constData(), ZIP_FL_ENC_UTF_8) < 0)
                    {
                        QMessageBox::warning(nullptr, "",QString("Failed to add packed directory to zip"));
    					return;
                    }
                    qDebug() << "sFilePath" << sFilePath;
                    walkDirectory(sFilePath, packZip);
                }
            }
            else
            {
                zip_source_t *source = zip_source_file(packZip, sFilePath.toLatin1().constData(), 0, 0);
                if (source == nullptr)
                {
                    QMessageBox::warning(nullptr, "",QString("Failed to add packed file to zip source"));
                    return;
                }
                if (zip_file_add(packZip, sFilePath.toLatin1().constData(), source, ZIP_FL_ENC_UTF_8) < 0)
                {
                    zip_source_free(source);
                    QMessageBox::warning(nullptr, "",QString("Failed to add packed file to zip"));
                    return;
                }
            }
        }
    }
    

    The code works but it adds the entire file path rather than the selected folder name and the rest going forward.

    also how would i add say ../qt/books the "../" in the path?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Styx said in walk a directory while ziping the directory:

      QString sFilePath = info.filePath();

      Change this to:

      QString sFilePath = info.filename();
      

      (Z(:^

      1 Reply Last reply
      2
      • StyxS Offline
        StyxS Offline
        Styx
        wrote on last edited by
        #3

        The method you suggested didn't work. It should be copying the folder name to the zip directory but it doesn't.

        /numbers
        /numbers/1/file1
        /numbers/2/2/file2
        /numbers/3/3/file3
        etc...

        C 1 Reply Last reply
        0
        • StyxS Styx

          The method you suggested didn't work. It should be copying the folder name to the zip directory but it doesn't.

          /numbers
          /numbers/1/file1
          /numbers/2/2/file2
          /numbers/3/3/file3
          etc...

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Styx It is not clear whether your last post show what you get, or what you expect to get.

          If your user gives you directory "C:\Users\1\Desktop\2\3\release\4\5\Numbers" you will not get the "numbers" part of the path because you start your recursion inside that folder. That is fairly easily fixed.

          To get a relative path to each file, you will have to either build it as you recurse or derive it by using QDir::relativeFilePath() on the QDir object representing the top of the tree with the full path to each file.

          Something like this with a good sprinkling of sanity and error checks (handling for sym links etc. too)

          #include <QCoreApplication>
          #include <QDir>
          #include <QDebug>
          
          void walkDirectory(const QString& sDir)
          {
              QDir d(sDir);
              QFileInfoList entries = d.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name);
              foreach (QFileInfo entry, entries) {
                  if (entry.isDir()) {
                      qDebug() << "Adding directory" << sDir + "/" + entry.fileName();
                      walkDirectory(sDir + "/" + entry.fileName());
                  }
                  else {
                      qDebug() << "Adding file" << sDir + "/" + entry.fileName();
                  }
              }
          }
          
          void recursiveZip(const QString& sDir)
          {
              QString oldPWD = QDir::currentPath();
          
              QDir topDir(sDir);
              QString dirName = topDir.dirName();
              topDir.cdUp();
              QDir::setCurrent(topDir.path());
              walkDirectory(dirName);
          
              QDir::setCurrent(oldPWD);
          }
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              recursiveZip("/tmp/test/numbers");
              return 0;
          }
          
          1 Reply Last reply
          4

          • Login

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