Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C++
Article

Win32 Wrapper classes for Gilles Volant's Zip/Unzip API

Rate me:
Please Sign up or sign in to vote.
4.87/5 (33 votes)
27 Jul 2003CC (ASA 2.5)3 min read 464.3K   17.6K   102   130
High level wrapping of the zlib library to make easy work of zipping and unzipping files and folders

Introduction

This article is an addendum to my previous article "C++ wrapper for Gilles Vollant's Unzip API".

That article provided a Win32 C++ class (CUnzipper) for simple querying and unzipping of single and multi-file zip files. What I've done now is to complete the work by adding an equivalent class (CZipper) for zipping files and folders.

The code link above contains the source both for zipping and unzipping.

Implementation

The CZipper class interface provides two approaches to zipping:

  1. For simple file and folder zipping, you can use the static ZipFile(...) or ZipFolder(...) methods.
  2. For more complex zipping requirements, you can alternatively instantiate a CZipper object, then use OpenZip(...), followed by one or more AddFileToZip(...) or AddFolderToZip(...) calls, before ending with CloseZip().

The following features are supported:

  • By default, the folder where the zip is saved, is assumed to be the common root of all the files/folders to be added (which is then removed before the files are added to the zip).
  • However, you can supply an alternative root if you wish to save the zip to a location other than the common root.
  • Files/folders can be added by relative or absolute path, although ..\ is not acceptable as the resulting full path would not be within the common root folder.
  • For reasons that I can't immediately think of, you can also elect to have all path information removed, prior to adding to the zip. i.e., all the files appear as if in one folder regardless of their actual relationship on your hard drive.

Notes

  • In addition to the new code, I have also modified the unzip code to take on board a number of comments made on the original article.

    Principally, these relate to the use of _splitpath() and its companion _makepath() for parsing and building valid pathnames.

    Having been through the process, I can confirm that the days of reverse searching for '\' and '.' and well and truly behind me.

  • To use the code in your own project:
    1. Add zipper.h/.cpp and/or unzipper.h/.cpp to the project depending on your needs
    2. And the zlib folder in the same relationship as it is to these files in the sample project (this contains the header files to Gilles Volant's API)
    3. Add zlibstat.lib to the project also (this contains Gilles Volant's API code and the ZLib library)
    4. Build the project.

Sample project

The sample project allows you to zip and unzip, and is intended as a simple demonstration that the code works correctly.

It's the same code that I used for verifying the wrapper classes.

Copyright

The code is supplied here for you to use and abuse without restriction (excepting the copyright restrictions imposed by those referenced in the Credits section), except that you may not modify it and pass it off as your own.

Credits

  • 1.0 Initial Release
  • 1.1 Support for adding folders by relative path (thanks to YourArmsOff)
  • 1.2 File date/times correctly saved with the zip (thanks to IJamil)
  • 1.3
    • correctly zips empty folders (thanks to voland2)
    • handles zip files opened by relative path (thanks to AlphaDog)
    • fixed some bugs relating to how folders are stored (thanks to AlvaChin)

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions

 
Questionproblem by building my own proejct using these lib Pin
Mohammad Nasser27-Aug-18 22:17
professionalMohammad Nasser27-Aug-18 22:17 
QuestionHelp me to build and run application on VS2013 with UNICODE support Pin
gawadepd10-Jul-18 3:16
gawadepd10-Jul-18 3:16 
QuestionMFC Components missing Pin
Member 1325711013-Jun-17 4:19
Member 1325711013-Jun-17 4:19 
QuestionThanks for the wrapper Pin
Singh, Yashpal19-May-15 23:14
Singh, Yashpal19-May-15 23:14 
QuestionWhy Not Support X64? Please~ ( zlibstatX64.lib ) Pin
chunbogloved4-Feb-15 21:55
chunbogloved4-Feb-15 21:55 
Questiondevcpp Pin
Member 1109300523-Sep-14 8:24
Member 1109300523-Sep-14 8:24 
BugUnzip bug Pin
Member 1073444011-Apr-14 8:53
Member 1073444011-Apr-14 8:53 
GeneralRe: Unzip bug Pin
chineseghost13-Jul-14 4:36
chineseghost13-Jul-14 4:36 
QuestionMinizip: Zip and UnZip library has moved Pin
merano4-Oct-13 8:46
merano4-Oct-13 8:46 
QuestionThank you Pin
manucng11-Feb-13 23:06
manucng11-Feb-13 23:06 
AnswerRe: Thank you Pin
.dan.g.12-Feb-13 12:50
professional.dan.g.12-Feb-13 12:50 
QuestionCan I use these codes in commercial software? Pin
airekans2-Nov-11 17:41
airekans2-Nov-11 17:41 
AnswerRe: Can I use these codes in commercial software? Pin
.dan.g.2-Nov-11 20:37
professional.dan.g.2-Nov-11 20:37 
QuestionBug with UNC paths - also on abstractspoon site code, Pin
Iain Clarke, Warrior Programmer27-Sep-11 22:21
Iain Clarke, Warrior Programmer27-Sep-11 22:21 
QuestionUnicode version (sort of) Pin
Roger Bamforth8-Aug-11 3:47
Roger Bamforth8-Aug-11 3:47 
This may not be the best way of fixing this but I solved the problem of making it all work in a Unicode app. by making the problem go away. Smile | :)

First of all, we had previously taken the Zipper/Unzipper and ZLib code and built it into a static library, so we're not quite starting from the same place. I am still building this as a MultiByte character set library and just using it in my Unicode app.

The problem then is that when the headers are included in your Unicode app. all the LPTSTR declarations change into LPWSTR and so on. Then your app. won't link because it can't find the LPWSTR versions of the functions in the library (as they don't exist).

So, all I've done is change Zipper and Unzipper so that all the LPCTSTR function parameters are LPCSTR and all the LPTSTR ones are LPSTR and rebuilt the library (still using the Multibyte character set). Now that these parameters are all explicitly chars, not TCHARs, everything works fine so long as when you call the functions in your Unicode app. you are sure to pass char strings to the functions and not TCHARs.

The important thing to be aware of is that there is no need to convert everything to Unicode in a Unicode app. If you explicitly use char, LPSTR, LPCSTR and CStringA and don't use _T() macros then everything is exactly as it is in an MBCS build, even if you are building for Unicode.

Also, this approach is, of course, no use to you if you need to add files with Unicode names into your zip files, or save the zip file with a Unicode name. But it's good enough for my purposes.
Regards

- Roger

GeneralZip file not created [modified] Pin
cotsjdixon21-Mar-11 3:55
cotsjdixon21-Mar-11 3:55 
QuestionError when compiling Pin
cotsjdixon9-Mar-11 9:03
cotsjdixon9-Mar-11 9:03 
AnswerRe: Error when compiling Pin
.dan.g.9-Mar-11 16:39
professional.dan.g.9-Mar-11 16:39 
Question?? Pin
rerainings14-Jun-10 0:31
rerainings14-Jun-10 0:31 
GeneralI have made a vc++ 2005 version Pin
xiangzi1-Mar-10 20:46
xiangzi1-Mar-10 20:46 
GeneralRe: I have made a vc++ 2005 version Pin
.dan.g.9-Mar-11 16:41
professional.dan.g.9-Mar-11 16:41 
QuestionGetting a compiler error in unzip.h Pin
Sara Thompson21-Feb-10 16:51
Sara Thompson21-Feb-10 16:51 
Generalx64 version of this library Pin
a.tess16-Dec-08 3:04
a.tess16-Dec-08 3:04 
GeneralRe: x64 version of this library Pin
chunbogloved4-Feb-15 21:36
chunbogloved4-Feb-15 21:36 
GeneralBug in Zipping Pin
Prasadaknair30-Oct-08 1:34
Prasadaknair30-Oct-08 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.