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?

How to unzip a zip file with qt fuction?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 8 Posters 31.3k Views
  • 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.
  • L Offline
    L Offline
    Leon
    wrote on 19 Aug 2012, 12:26 last edited by
    #1

    Is there a way to unzip a zip file with a qt fuction..? ( lets say the zip file has some songs in it )
    I have heard about quncompress.. when i tried this:

    @QFile infile(QDir::homePath()+"/Downloads/ena.zip");
    QFile outfile(QDir::homePath()+"/Downloads");
    infile.open(QIODevice::ReadOnly);
    outfile.open(QIODevice::WriteOnly);
    QByteArray uncompressedData = infile.readAll();
    QByteArray compressedData = qUncompress(uncompressedData);
    outfile.write(compressedData);
    infile.close();
    outfile.close();@

    an error like this appeared:

    @qUncompress: Z_DATA_ERROR: Input data is corrupted
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 19 Aug 2012, 14:12 last edited by
      #2

      The archives have typically some file management involved. That is not part of zlib.
      Some useful information you may find "here":http://qt-project.org/faq/answer/how_to_compress_data_with_qt

      [quote]
      How to compress data with Qt?

      Qt uses the zlib library to provide compression. This means that you can compress and decompress your bytearrays of data using Qt. See the documentation on qUncompress() [qt.nokia.com]

      Note that this does not produce file archives that can be opened by other applications, to do that you will need to write it yourself using Qt’s custom file handling system [qt.nokia.com] or use a 3rd party library.
      [/quote]

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on 19 Aug 2012, 16:07 last edited by
        #3

        i didn't want to use something that is not in the default library.. but it seems i will.. i will try to use Quazip.. if i find any difficulties i will post in this thread.. :)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 20 Aug 2012, 15:10 last edited by
          #4

          zlib != zip

          zip usually does contain meta information about files stored which is not handled at all by qUncompress, which just takes a chunk of memory and returns another chunk that is compressed.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            matti-
            wrote on 20 Aug 2012, 15:30 last edited by
            #5

            Go with Quazip.

            Author of <a href="http://mmark.777-team.org/">MMark13</a>

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on 21 Dec 2013, 01:26 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 21 Dec 2013, 12:13 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 22 Dec 2013, 20:13 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 22 Dec 2013, 20:19 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 23 Dec 2013, 02:21 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 23 Dec 2013, 09:09 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 14 Jan 2014, 17:48 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
                          • C Offline
                            C Offline
                            ChrisGoz
                            wrote on 2 Feb 2015, 23:40 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
                            • C Offline
                              C Offline
                              ChrisGoz
                              wrote on 2 Feb 2015, 23:40 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