Click here to Skip to main content
11,414,234 members (69,817 online)
Click here to Skip to main content

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

, 27 Jul 2003 CC (ASA 2.5)
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

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

For all his latest freeware visit AbstractSpoon.

Comments and Discussions

 
QuestionWhy Not Support X64? Please~ ( zlibstatX64.lib ) Pin
chunbogloved, 4-Feb-15 22:55
memberchunbogloved4-Feb-15 22:55 
Questiondevcpp Pin
Member 11093005, 23-Sep-14 9:24
memberMember 1109300523-Sep-14 9:24 
BugUnzip bug Pin
Member 10734440, 11-Apr-14 9:53
memberMember 1073444011-Apr-14 9:53 
GeneralRe: Unzip bug Pin
chineseghost, 13-Jul-14 5:36
memberchineseghost13-Jul-14 5:36 
QuestionMinizip: Zip and UnZip library has moved Pin
merano, 4-Oct-13 9:46
membermerano4-Oct-13 9:46 
QuestionThank you Pin
manucng, 12-Feb-13 0:06
membermanucng12-Feb-13 0:06 
AnswerRe: Thank you Pin
.dan.g., 12-Feb-13 13:50
member.dan.g.12-Feb-13 13:50 
QuestionCan I use these codes in commercial software? Pin
airekans, 2-Nov-11 18:41
memberairekans2-Nov-11 18:41 
Hi, all.
I am not quite familiar with the "Creative Common Attribution-ShareAlike 2.5 Generic" license, so I don't know whether it's OK to use this wrapper in commercial software.
Is it right that if I use it in commercial software, I must not change the code in it?

Thanks.
AnswerRe: Can I use these codes in commercial software? Pin
.dan.g., 2-Nov-11 21:37
member.dan.g.2-Nov-11 21:37 
QuestionBug with UNC paths - also on abstractspoon site code, Pin
Iain Clarke, Warrior Programmer, 27-Sep-11 23:21
memberIain Clarke, Warrior Programmer27-Sep-11 23:21 
QuestionUnicode version (sort of) Pin
Roger Bamforth, 8-Aug-11 4:47
memberRoger Bamforth8-Aug-11 4:47 
GeneralZip file not created [modified] Pin
cotsjdixon, 21-Mar-11 4:55
membercotsjdixon21-Mar-11 4:55 
QuestionError when compiling Pin
cotsjdixon, 9-Mar-11 10:03
membercotsjdixon9-Mar-11 10:03 
AnswerRe: Error when compiling Pin
.dan.g., 9-Mar-11 17:39
member.dan.g.9-Mar-11 17:39 
Question?? Pin
rerainings, 14-Jun-10 1:31
memberrerainings14-Jun-10 1:31 
GeneralI have made a vc++ 2005 version Pin
xiangzi, 1-Mar-10 21:46
memberxiangzi1-Mar-10 21:46 
GeneralRe: I have made a vc++ 2005 version Pin
.dan.g., 9-Mar-11 17:41
member.dan.g.9-Mar-11 17:41 
QuestionGetting a compiler error in unzip.h Pin
Sara Thompson, 21-Feb-10 17:51
memberSara Thompson21-Feb-10 17:51 
Generalx64 version of this library Pin
a.tess, 16-Dec-08 4:04
membera.tess16-Dec-08 4:04 
GeneralRe: x64 version of this library Pin
chunbogloved, 4-Feb-15 22:36
memberchunbogloved4-Feb-15 22:36 
GeneralBug in Zipping Pin
Presu, 30-Oct-08 2:34
memberPresu30-Oct-08 2:34 
GeneralAppend to Existing Zip File with zlib 1.2.3 Pin
cwdonnelly, 8-Jun-08 3:25
membercwdonnelly8-Jun-08 3:25 
GeneralHelp me for unicode Pin
Thuy NH, 18-Nov-07 21:11
memberThuy NH18-Nov-07 21:11 
GeneralRe: Help me for unicode Pin
Thuy NH, 19-Nov-07 20:04
memberThuy NH19-Nov-07 20:04 
QuestionProblem with XP unzipping feature Pin
StefanFrank, 10-Sep-07 3:22
memberStefanFrank10-Sep-07 3:22 
AnswerRe: Problem with XP unzipping feature Pin
StefanFrank, 11-Sep-07 22:48
memberStefanFrank11-Sep-07 22:48 
Generalsetting the file pointer in a zip file Pin
pauldowd, 3-Apr-07 9:13
memberpauldowd3-Apr-07 9:13 
GeneralZipper Bug (alphabetical ordering of files and folders) Pin
stokos, 1-Jan-07 15:23
memberstokos1-Jan-07 15:23 
QuestionNew version? Pin
Daniel C., 17-Aug-06 21:59
memberDaniel C.17-Aug-06 21:59 
GeneralProblem with unzipping Pin
kezhu, 8-May-06 17:05
memberkezhu8-May-06 17:05 
GeneralRe: Problem with unzipping Pin
onlysmooth, 12-May-06 8:20
memberonlysmooth12-May-06 8:20 
GeneralRe: Problem with unzipping Pin
abc_pf, 23-Oct-06 1:41
memberabc_pf23-Oct-06 1:41 
GeneralProblem with Zipping Pin
viaduct, 3-Apr-06 5:47
memberviaduct3-Apr-06 5:47 
GeneralRe: Problem with Zipping Pin
Defenestration, 27-Apr-06 14:03
memberDefenestration27-Apr-06 14:03 
GeneralRe: Problem with Zipping Pin
viaduct, 27-Apr-06 22:43
memberviaduct27-Apr-06 22:43 
AnswerRe: Problem with Zipping [modified] Pin
philzzh, 1-Aug-07 0:01
memberphilzzh1-Aug-07 0:01 
Generalfiles here are outdated & buggy Pin
Max Meier, 11-Nov-05 7:54
memberMax Meier11-Nov-05 7:54 
Generalerror LNK2005 Pin
Michael Klim, 21-Jul-05 14:01
memberMichael Klim21-Jul-05 14:01 
Questionhow do I zip folders? Pin
y2j150, 22-Jun-05 19:06
sussy2j15022-Jun-05 19:06 
GeneralUnzipping across folders Pin
mklabs, 16-Dec-04 3:06
membermklabs16-Dec-04 3:06 
QuestionSub-Folder in Zip? Pin
Anonymous, 13-Dec-04 8:17
sussAnonymous13-Dec-04 8:17 
Questionzip larger than 2Gig? Pin
johnd126, 1-Oct-04 6:22
memberjohnd1261-Oct-04 6:22 
GeneralAppend a Zip Pin
jeccull, 16-Sep-04 10:06
memberjeccull16-Sep-04 10:06 
QuestionDestination Folder for Zip? Pin
jeccull, 10-Sep-04 8:07
memberjeccull10-Sep-04 8:07 
AnswerRe: Destination Folder for Zip? Pin
.dan.g., 10-Sep-04 22:08
member.dan.g.10-Sep-04 22:08 
GeneralDoes not work with UNC path Pin
Anonymous, 12-May-04 9:45
sussAnonymous12-May-04 9:45 
GeneralDelete files Pin
Anonymous, 27-Apr-04 8:42
sussAnonymous27-Apr-04 8:42 
GeneralError in link ?! Pin
Deian, 25-Jan-04 4:51
memberDeian25-Jan-04 4:51 
GeneralRe: Error in link ?! Pin
SOFBE, 8-Jul-04 22:54
memberSOFBE8-Jul-04 22:54 
QuestionDoes it support subtracting? Pin
zeon77, 20-Dec-03 9:27
memberzeon7720-Dec-03 9:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.2 | Last Updated 28 Jul 2003
Article Copyright 2003 by .dan.g.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid