zlib c++ and extracting files

22,640

Solution 1

Yes, it does it well. (But if ever you don't like C code, you should look at 7-zip SDK that have code in C++ and C#.)

  • All the functions to browse and uncompress the files from a zip archive are in unzip.h
  • All the functions to compress and add files to a zip archive are in zip.h

(look at contrib\minizip\unzip.h and contrib\minizip\zip.h)

For example, decompressing: the unzOpen() functions of your zip file returns a unzFile

then use unzGoToFirstFile() and unzGoToNextFile() on this unzFile to browse through all files in the archive.

then you get the file info for each file with unzGetCurrentFileInfo(), namely for its size,

surely you should call unzOpenCurrentFile() at some moment.

and call unzReadCurrentFile() using the size from file info, retrieving the binary content of the archived file.

optionally, there is an opaque structure you can provide so as to use your own i/o function, but obviously, there is a default win32 implementation for file access, so you could even not worry about that.

PS: and dont forget to call unzCloseCurrentFile().

Solution 2

From: http://www.zlib.net/zlib_faq.html#faq11 : 11. Can zlib handle .zip archives?

Not by itself, no. See the directory contrib/minizip in the zlib distribution.

There's not a tutorial there but the minizip zip.c source is exactly for IO (so presumably compression and decompression) on zip files using zlib.

And still no tutorial BUT http://www.winimage.com/zLibDll/minizip.html gives more details.

Solution 3

I have built a wrapper around minizip adding some features that I needed and making it nicer to use it. Is does use the latest c++11 and is developed using Visual Studio 2013 (should be portable, but I haven't tested it on unix)

There's a full description here: https://github.com/sebastiandev/zipper

you can zip entire folders, streams, vectors, etc. Also a nice feature is doing everything entirely in memory.

Share:
22,640
reza
Author by

reza

Updated on July 18, 2022

Comments

  • reza
    reza almost 2 years

    I have started to use zlib 1.2.5 and I do not see any routine to extract from a zip file? I read about a minizip application, part of the distribution.

    Is that how it is supposed to be done?

  • reza
    reza over 13 years
    Ok, I found the functions and I am using miniunzip to test with. I created a file c:\a.zip with 7zip. It contains one file. I run miniunzip -x c:\a.zip -d c:\reza and and unzOpen2_64 fails. I am running this on Windows. Any thoughts on why this is the case?
  • Wes
    Wes about 8 years
    Is it possible to replace items in a zip folder with your wrapper? I have a case where I need to extract a file from a zip, make some changes and write it back to the zip (overwriting what's there now) without overwriting the entire zip file. Looks like I could use the unzipper class first to extract and modify the file and then use the add function in the zipper class with the same file name to overwrite it.
  • Sebastian
    Sebastian about 8 years
    minizip doesnt have that functionality, not even delete from the zip. I think the zip standard gives the possibility to do that, but it would have to be implemented on minizip first and added later to these wrapper. This was more focused on zipping data, specially in memory for internal use of apps where plain compresion was the goal, but not really oriented or intended to be used to edit and alter zips.
  • kberson
    kberson almost 8 years
    These links are dead.
  • Stephane Rolland
    Stephane Rolland almost 8 years
    @kberson thx for the remark. I have updated links to point to github.com/madler/zlib/blob/master/contrib/minizip