Zipping and unzipping files with Qt

zip_and_qt

If you want to zip and unzip files within your Qt application, you will discover a world of pain in front of you >_<

The only two functions that the Qt library includes to compress an uncompress data are qCompress and qUncompress, both part of the QByteArray class.

The problem is that they use a format not compatible with external applications like WinZIP, WinRAR or 7-Zip, which means that if you try to use these functions with a normal ZIP file, you will probably get an error like this:

But do not worry! There is a way to resolve this problem! ^_^

If you search on the Internet, you will figure out many examples of code using these two functions that, of course, are valid are not valid for compressing or uncompressing ZIP files.

I have created my own application if you want to test a bad example by yourself:

Finally, if you search on the Qt FAQ, you will get a more technical explanation of why it does not work:

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.

At this point, the only two things that we can do to compress and uncompress ZIP files within our Qt application are:

  • Create our own library.
  • Use an external library.

Because the first option could be a bit tricky and I do not have enough time to develop it, I finally chose the second one.

Researching a bit on the Qt forums and other web pages related, I discovered that probably the best library that implements this feature is QuaZIP:

QuaZIP is a simple C++ wrapper that can be used to access ZIP archives. It uses the Qt toolkit, which means that it is a cross-platform (Windows/Linux/BSD/UNIX/Mac/Others) library.

It depends on zlib library, so you need to download them from their official websites. I have used the latest versions of both for this example:

First of all, uncompress the file zlib128.zip in your computer wherever you want. I have chosen the path C:\Qt\zlib-1.2.8 because in the C:\Qt folder I have my original installation of Qt.

After that, open a Windows Command Prompt and write the next commands:

I am using Qt v5.3.2 and MinGW v4.8.2. so, depending on the compiler that you use, the syntax could be different. Make sure that the installation folder of both, Qt and MinGW, is accessible through the PATH system variable.

The next step is uncompress the file quazip-0.7.zip. For this library I have chosen the folder C:\Qt\quazip-0.7.

Again, open a Windows Command Prompt and write the next commands:

Take into account that if the folder in which you have uncompressed zlib is different than mine, you need to modify the commands above to write the correct path.

Again, if you do not get any error, it means that you have build QuaZIP (and zlib) correctly!!! ^_^

Now, for testing the library I have created a very simple Qt project with the name “zip”. It includes by default two files: zip.pro and main.cpp:

qt_project

First, open the file zip.pro and add these lines:

Next, open the file main.cpp and remplace the contents with the following:

Finally, if you comment the lines 5 or 6 you can check how the Qt application zip or unzip an specific file.

That is all! I hope this tutorial has been helpful and if you have any questions leave a comment! ^_^

Tweet about this on Twitter0Share on Facebook0Share on Google+0Share on LinkedIn6Buffer this pageEmail this to someonePrint this page

19 thoughts on “Zipping and unzipping files with Qt

    1. Thank you very much for the video! It is very useful! πŸ˜€

    1. Hi Wemani!

      Just open Qt Creator and in the top bar click on Tools > Options… In the opened window, click on the tab Compilers to know which version you are using.

      Cheers!

  1. Hii, I am using quazip library for zip and unzip files for my Blackberry Device. I am making the project in their momentics ide. I have successfully implemented zip functionality but when i am trying to unzip, the files unzip in the first directory itself. For example: if my folder structure is like docs/abc/qwe/rty/hello.txt and zip folder comes as abc.zip, it means that the abc.zip has structure as abc/qwe/rty/hello.txt and on unzip it should give the same. But on unzip, i am getting it as docs/hello.txt., while I should get it as docs/abc/qwe/rty/hello.txt.
    Any help?
    Many Thanks

  2. Thank you for your article,I am trying to according to the steps.When I doing”cd C:\Qt\zlib-1.2.8″,an error occurred…oh,please forgive my poor English,I mean,I uncompress the file zlib128.zip in my computer and chosen the path D:\Qt\zlib-1.2.8,may be “cd /d d:”?

    1. Hi 郝睿,

      If you uncompressed the zlib128.zip in the path D:\Qt\zlib-1.2.8, you will need to run the following command instead:

      cd D:\Qt\zlib-1.2.8

  3. Hi, i am struggling here to install zlib and quazip, watched and read the entire Internet and nothing seems to be working for me.
    I have followed the exact steps as described above, but came up with errors.
    When
    qmake “CONFIG+=release” “INCLUDEPATH+=C:/Qt/zlib-1.2.8” “LIBS+=-LC:/Qt/zlib-1.2.8 -lz”
    mingw32-make
    is executed, i get the erros shown here.. https://www.dropbox.com/s/f7gt8e9z7h71x15/Quazip%20qmake%20error.png?dl=0

    Please advise.
    Using Qt 5.6 & MinGW 4.9.2 32bit on Windows 10

    1. Hi Babis,

      Try to follow exactly the same steps that I describe in this post and then let me know if you still have the same problem πŸ˜‰

  4. Hello

    When I use this command lines “cd C:\Qt\quazip-0.7
    qmake “CONFIG+=release” “INCLUDEPATH+=C:/Qt/zlib-1.2.8” “LIBS+=-LC:/Qt/zlib-1.2.8 -lz”
    mingw32-make”

    It gave this error :

    (QIODevice::readLineData(char*, long long)’
    collect2.exe: error: ld returned 1 exit status
    Makefile.Release:117: recipe for target ‘release\quazip.dll’ failed
    mingw32-make[2]: *** [release\quazip.dll] Error 1
    mingw32-make[2]: Leaving directory ‘C:/Qt/quazip-0.7/quazip’
    Makefile:34: recipe for target ‘release’ failed
    mingw32-make[1]: *** [release] Error 2
    mingw32-make[1]: Leaving directory ‘C:/Qt/quazip-0.7/quazip’
    Makefile:39: recipe for target ‘sub-quazip-make_first’ failed
    mingw32-make: *** [sub-quazip-make_first] Error 2)

    with many lines like this :(./release\qioapi.o:qioapi.cpp:(.text+0xce): undefined reference to `__imp__ZNK9QIODevice6isOpenEv’)

    I have no idea what’s wrong?! I use windows 10, downloaded TDM-GCC-64 because I coundn’t use mingw32-make, and I did all of tutorial step by step, even in same directory

    1. Hi Farhad,

      I fear I cannot help you, sorry.

      If you are using a different compiler, I cannot tell you why that error is happening, unless I had the same environment as you (Windows 10 + TDM-GCC-64).

Leave a Reply