Creating zip-archives in C++
published at 22.11.2013 17:46 by Jens Weller
Save to Instapaper Pocket
Today I had to do some research into how to create archives in C++, as this is my windows machine, a windows or better platformindependent solution is needed. My plan is to write a small backup tool, which simply copies folders and files (maybe with filtersupport) into an archive. Of course there is the zlib as a solution, but I'm looking for an easy way to do things, and not an C interface. So, I started my search for a solution...
Zip archives with Qt
As I usually use Qt for UI, I started to look for a solution that would either work with Qt or is pure C++. Zipios++ is a very nice C++ solution. And its not for iOS, as the name might imply. But there is no real windows support as it seems, as I work with MinGW, I'm sure I could build the library, but not sure if it would work. Also after looking a little bit through its documentation, its clear, that it seems to only be able to read zip files, but not create new entries, which actually is what I'd like to do.
Qt it self does not support the creation of archives out of the box. It comes with zlib compiled into the core lib, so it provides the basic support of creating zip data. qCompress/qUncompress use this, and I previously already did use the zlib interface to "unzip" data coming over HTTP. But there is a library on sourceforge too for this: QuaZIP. Same license as Qt, QuaZIP is able to read and write Zip archive files. As I only want to write them, it seems to do what I need. Obtaining the files to export is also quite easy, a possible filter could be easily applied with remove_if from the STL. Building QuaZIP is also quite easy, simply open the .pro file of the included project, and build the library. There will be an error that the linker doesn't find the zlib, but when you link an executable with Qt, the symbols should be found in the core lib of Qt.
Last but not least there is also a GPL/commercial alternative, but I'm not sure how the Qt5 support is. Also there seems to be an officially not in Qt API documented/supported QZip(Writer/Reader) class in Qt4, which might not have made it into Qt5. Also KDE has a library to handle this, and OSDab is a different solution to handle zipfiles in Qt4.
Using wxWidgets
I used wxWidgets for many years, so while I was ready to go with Qt, I thought to do a quick search if any libraries for wxWidgets and creating zip files existed. As I do want to take a closer look at wxWidgets 3.0. So, I found with zip archives something, that wxWidgets is able to do, but Qt can't (usual its the other way around (GraphisView, QML, WebKit integration etc.)). wxWidgets has archive support for zip and tar files. There is a good documentation on how to work with archives in wxWidgets.
Now, I have to choose which approach I'd like to use. And as wxWidgets3.0 offers better STL support and build in archive support (switching to tar is a nice alternative), I think I'll go with wxWidgets. This also, as I was looking for a small project to make use of wxWidgets, and it seems to fit quite well.
Join the Meeting C++ patreon community!
This and other posts on Meeting C++ are enabled by my supporters on patreon!