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. How to unzip a zip file with qt fuction?
Qt 6.11 is out! See what's new in the release blog

How to unzip a zip file with qt fuction?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 8 Posters 35.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #5

    Go with Quazip.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leon
      wrote on last edited by
      #6

      [quote author="matti-" date="1345476621"]Go with Quazip.[/quote]

      I am bringing this post back.

      1. I downloaded quazip
      2. i copied the folder quazip to my projects src folder
      3. i included at my .pro file all the headers and sources of quazip

      @HEADERS += src/quazip/crypt.h
      src/quazip/ioapi.h
      src/quazip/quazip.h
      src/quazip/quazipfile.h
      src/quazip/quazipfileinfo.h
      src/quazip/quazipnewinfo.h
      src/quazip/unzip.h
      src/quazip/zip.h
      src/quazip/JlCompress.h
      src/quazip/quaadler32.h
      src/quazip/quachecksum32.h
      src/quazip/quacrc32.h
      src/quazip/quagzipfile.h
      src/quazip/quaziodevice.h
      src/quazip/quazip_global.h
      src/quazip/quazipdir.h

      SOURCES += src/quazip/quazip.cpp
      src/quazip/quazipfile.cpp
      src/quazip/quazipnewinfo.cpp
      src/quazip/quazipfileinfo.cpp
      src/quazip/quazipdir.cpp
      src/quazip/quaziodevice.cpp
      src/quazip/quagzipfile.cpp
      src/quazip/quacrc32.cpp
      src/quazip/qioapi.cpp
      src/quazip/unzip.c
      src/quazip/JlCompress.cpp
      src/quazip/zip.c@

      1. i added the headers
        @#include "quazip/quazip.h"
        #include "quazip/quazipfile.h"@
      2. And added this code
        @static bool extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName = QString("")) {

      QuaZip zip(filePath);

      if (!zip.open(QuaZip::mdUnzip)) {
      qWarning("testRead(): zip.open(): %d", zip.getZipError());
      return false;
      }

      zip.setFileNameCodec("IBM866");

      qWarning("%d entries\n", zip.getEntriesCount());
      qWarning("Global comment: %s\n", zip.getComment().toLocal8Bit().constData());

      QuaZipFileInfo info;

      QuaZipFile file(&zip);

      QFile out;
      QString name;
      char c;
      for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {

      if (!zip.getCurrentFileInfo(&info)) {
          qWarning("testRead(): getCurrentFileInfo(): %d\n", zip.getZipError());
          return false;
      }
      
      if (!singleFileName.isEmpty())
          if (!info.name.contains(singleFileName))
              continue;
      
      if (!file.open(QIODevice::ReadOnly)) {
          qWarning("testRead(): file.open(): %d", file.getZipError());
          return false;
      }
      
      name = QString("%1/%2").arg(extDirPath).arg(file.getActualFileName());
      
      if (file.getZipError() != UNZ_OK) {
          qWarning("testRead(): file.getFileName(): %d", file.getZipError());
          return false;
      }
      
      //out.setFileName("out/" + name);
      out.setFileName(name);
      
      // this will fail if "name" contains subdirectories, but we don't mind that
      out.open(QIODevice::WriteOnly);
      // Slow like hell (on GNU/Linux at least), but it is not my fault.
      // Not ZIP/UNZIP package's fault either.
      // The slowest thing here is out.putChar(c).
      while (file.getChar(&c)) out.putChar(c);
      
      out.close();
      
      if (file.getZipError() != UNZ_OK) {
          qWarning("testRead(): file.getFileName(): %d", file.getZipError());
          return false;
      }
      
      if (!file.atEnd()) {
          qWarning("testRead(): read all but not EOF");
          return false;
      }
      
      file.close();
      
      if (file.getZipError() != UNZ_OK) {
          qWarning("testRead(): file.close(): %d", file.getZipError());
          return false;
      }
      

      }

      zip.close();

      if (zip.getZipError() != UNZ_OK) {
      qWarning("testRead(): zip.close(): %d", zip.getZipError());
      return false;
      }

      return true;
      }@

      After compiling all i get is 200 warnings=errors all like

      @warning: 'static bool JlCompress::compressFiles(QString, QStringList)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
      bool JlCompress::compressFiles(QString fileCompressed, QStringList files)

      error: undefined reference to `_imp___ZNK6QuaZip11getZipErrorEv'@

      What am i doing wrong?
      steps/code taken from http://stackoverflow.com/questions/2598117/zipping-a-folder-file-using-qt
      ^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #7

        I have to add here:
        Seems like there is a problem with quazip on qt5 at windows 7+

        https://qt-project.org/forums/viewthread/26167/

        At quazip site
        Requirements
        Just zlib and Qt 4/5. Well, Qt 4 depends on zlib anyway, but you will need zlib headers to compile QuaZIP. With Qt5 sometimes you need the zlib library as well (on Windows, for example)

        But i don't get what files of the zlib folder should i copy and what to include ...

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seba84
          wrote on last edited by
          #8

          Why don't you just do the unzipping with a system call to the unzip executable?
          @QString program = "./path/to/Qt/examples/widgets/analogclock";
          QStringList arguments;
          arguments << "-style" << "motif";

          QProcess *myProcess = new QProcess(parent);
          myProcess->start(program, arguments);@

          Example from Qt Doc "link":http://qt-project.org/doc/qt-4.8/qprocess.html.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leon
            wrote on last edited by
            #9

            [quote author="Seba" date="1387743195"]Why don't you just do the unzipping with a system call to the unzip executable?
            @QString program = "./path/to/Qt/examples/widgets/analogclock";
            QStringList arguments;
            arguments << "-style" << "motif";

            QProcess *myProcess = new QProcess(parent);
            myProcess->start(program, arguments);@

            Example from Qt Doc "link":http://qt-project.org/doc/qt-4.8/qprocess.html.[/quote]

            this works with linux, with the command unzip
            but i want it for Windows now

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #10

              Hi, you can use QZipReader and QZipWriter classes directly which in gui-private module.

              @
              QT+=gui-private
              @

              1 Reply Last reply
              1
              • S Offline
                S Offline
                Seba84
                wrote on last edited by
                #11

                QProcess works in Windows! You should only replace the strings with the program path and arguments you want to use in your computer/platform.

                Possible example with Windows:
                @QString program = "c:/program files/winrar/winrar.exe";
                QStringList arguments;
                arguments << "c:/documents/my_zip_file.zip";
                QProcess *myProcess = new QProcess(parent);
                myProcess->start(program, arguments);
                @

                You have to see the program documentation in order to give the correct command line arguments to do all the unzipping in the background (if this is what you want).

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alexandros
                  wrote on last edited by
                  #12

                  @Seba really?

                  Do you expect to add a dependency to winrar for his program?

                  There is no pre-installed MS windows executable for unzipping as far as I know.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #13

                    Has anyone had any luck unzipping a large zip file (250MB) in iOS?

                    I have successfully used QZipReader and QZipWriter classes for handling large files (following Bryan's excellent tutorial at: http://voidrealms.com/index.php?r=tutorial/view&id=365) on OSX and Android, but when I deploy my project to iOS, I get a memory error. In particular, my iPad does not want to fetch all the inflated data into memory before writing it to file.

                    Has anyone had any success accessing zlib's inflate method within a loop that reads chunks / writes them to file in each pass, until Z_STREAM_END is returned?

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #14

                      Has anyone had any luck unzipping a large zip file (250MB) in iOS?

                      I have successfully used QZipReader and QZipWriter classes for handling large files (following Bryan's excellent tutorial at: http://voidrealms.com/index.php?r=tutorial/view&id=365) on OSX and Android, but when I deploy my project to iOS, I get a memory error. In particular, my iPad does not want to fetch all the inflated data into memory before writing it to file.

                      Has anyone had any success accessing zlib's inflate method within a loop that reads chunks / writes them to file in each pass, until Z_STREAM_END is returned?

                      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