Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I zip a directory of files using C++?

I'm working on a project using C++, Boost, and Qt. I understand how to compress single files and bytestreams using, for example, the qCompress() function in Qt.

How do I zip a directory of multiple files, including subdirectories? I am looking for a cross-platform (Mac, Win, Linux) solution; I'd prefer not to fire off a bunch of new processes.

Is there a standard way to combine bytestreams from multiple files into a zipped archive, or maybe there is a convenience function or method that would be available in the Boost iostream library?

Many thanks for the assistance.

Update: The QuaZip library looks really great. There is an example in the download package (in the "tests" dir) that shows very clearly how to zip up a directory of files.

Update 2: After completing this task on my Linux build environment, I discovered that QuaZip doesn't work at all with the Visual Studio compiler. It may be possible to tackle all those compiler errors, but a word of caution to anyone looking down this path.

like image 273
martian Avatar asked Feb 03 '09 02:02

martian


3 Answers

I have found the following two libraries:

  • ZipIOS++. Seems to be "pure" C++. They don't list Windows explicitly as a supported platform. So i think you should try your luck yourself.
  • QuaZIP. Based on Qt4. Actually looks nice. They list Windows explicitly (Using mingw). Apparently, it is a C++ wrapper for [this] library.

Ah, and of course, i have ripped those sites from this Qt Mailinglist question about Zipping/Unzipping of directories :)

like image 99
Johannes Schaub - litb Avatar answered Nov 02 '22 19:11

Johannes Schaub - litb


http://www.zlib.net/

like image 32
Dustin Getz Avatar answered Nov 02 '22 19:11

Dustin Getz


Just for the record...

Today, I needed to do something very similar in Visual C++. (Though wanted to maintain the possibility to compile the project on other platforms; however preferred not to adopt Qt just for this purpose.)

Ended up using the Minizip library. It is written in plain C, but devising a simple C++ wrapper around it was a breeze, and the end result works great, at least for my purposes.

like image 45
Reunanen Avatar answered Nov 02 '22 18:11

Reunanen