views:

1493

answers:

4

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.

A: 
system("zip myarchive.zip *");
Adam Pierce
He said he doesn't want to fire off new processes.
PolyThinker
This is only one extra process and its a helluva lot quicker to implement than using a 3rd party library. But, I don't know the reasons for not wanting to launch another process, maybe the OP has very limited memory or wants more control over ZIP as it is running but this is how I'd do it.
Adam Pierce
it's not a question of spawning new processes. this answer is just plain ignorant. what if "zip" is not installed, what about other platforms then unix?
Jacek Ławrynowicz
+3  A: 

http://www.zlib.net/

Dustin Getz
As far as I am aware, zlib only supports .gz files and not .zip files.
Adam Pierce
Yeah, I'm looking for .zip. The minizip library linked from the zlib.net site might be able to help, but I need to dig into it a little more... If anyone knows more about this library I'd appreciate hearing about it.
martian
Can you actually compress whole directories with zlib? Or just single files?
Zlib only does single files - you need another package format to handle the files and directories
Martin Beckett
You can compress whole directories with minizip (can be found in zlib/contrib/minizip) but you will have to iterate over directory's contents and add the files one by one (boost::filesystem can help).
error.exit
+3  A: 

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 :)

Johannes Schaub - litb
I second QuaZIP.
Nick Presta
QuaZip looks like the way to go. Thanks!
martian
ZipIOS++ does have MSVC 6 project files. However, it borrows some code from Boost.Filesystem (as of 2001!), which doesn't really compile out of the box, at least in my VS2008 environment. So I guess I'll also have to look at QuaZIP, or plain zlib...
Pukku
+1  A: 

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.

Pukku