Click here to Skip to main content
Click here to Skip to main content

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

By , 27 Jul 2003
 

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

About the Author

.dan.g.
Software Developer Maptek
Australia Australia
Member
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.
 
For all his latest freeware visit AbstractSpoon.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThank youmembermanucng11 Feb '13 - 23:06 
AnswerRe: Thank youmember.dan.g.12 Feb '13 - 12:50 
QuestionCan I use these codes in commercial software?memberairekans2 Nov '11 - 17:41 
AnswerRe: Can I use these codes in commercial software?member.dan.g.2 Nov '11 - 20:37 
QuestionBug with UNC paths - also on abstractspoon site code,memberIain Clarke, Warrior Programmer27 Sep '11 - 22:21 
QuestionUnicode version (sort of)memberRoger Bamforth8 Aug '11 - 3:47 
GeneralZip file not created [modified]membercotsjdixon21 Mar '11 - 3:55 
QuestionError when compilingmembercotsjdixon9 Mar '11 - 9:03 
AnswerRe: Error when compilingmember.dan.g.9 Mar '11 - 16:39 
Question??memberrerainings14 Jun '10 - 0:31 
GeneralI have made a vc++ 2005 versionmemberxiangzi1 Mar '10 - 20:46 
GeneralRe: I have made a vc++ 2005 versionmember.dan.g.9 Mar '11 - 16:41 
QuestionGetting a compiler error in unzip.hmemberSara Thompson21 Feb '10 - 16:51 
Generalx64 version of this librarymembera.tess16 Dec '08 - 3:04 
GeneralBug in ZippingmemberPresu30 Oct '08 - 1:34 
GeneralAppend to Existing Zip File with zlib 1.2.3membercwdonnelly8 Jun '08 - 2:25 
GeneralHelp me for unicodememberThuy NH18 Nov '07 - 20:11 
GeneralRe: Help me for unicodememberThuy NH19 Nov '07 - 19:04 
QuestionProblem with XP unzipping featurememberStefanFrank10 Sep '07 - 2:22 
AnswerRe: Problem with XP unzipping featurememberStefanFrank11 Sep '07 - 21:48 
Generalsetting the file pointer in a zip filememberpauldowd3 Apr '07 - 8:13 
GeneralZipper Bug (alphabetical ordering of files and folders)memberstokos1 Jan '07 - 14:23 
QuestionNew version?memberDaniel C.17 Aug '06 - 20:59 
GeneralProblem with unzippingmemberkezhu8 May '06 - 16:05 
GeneralRe: Problem with unzippingmemberonlysmooth12 May '06 - 7:20 
GeneralRe: Problem with unzippingmemberabc_pf23 Oct '06 - 0:41 
GeneralProblem with Zippingmemberviaduct3 Apr '06 - 4:47 
GeneralRe: Problem with ZippingmemberDefenestration27 Apr '06 - 13:03 
GeneralRe: Problem with Zippingmemberviaduct27 Apr '06 - 21:43 
AnswerRe: Problem with Zipping [modified]memberphilzzh31 Jul '07 - 23:01 
Generalfiles here are outdated & buggymemberMax Meier11 Nov '05 - 6:54 
Generalerror LNK2005memberMichael Klim21 Jul '05 - 13:01 
Questionhow do I zip folders?sussy2j15022 Jun '05 - 18:06 
GeneralUnzipping across foldersmembermklabs16 Dec '04 - 2:06 
QuestionSub-Folder in Zip?sussAnonymous13 Dec '04 - 7:17 
Questionzip larger than 2Gig?memberjohnd1261 Oct '04 - 5:22 
GeneralAppend a Zipmemberjeccull16 Sep '04 - 9:06 
QuestionDestination Folder for Zip?memberjeccull10 Sep '04 - 7:07 
AnswerRe: Destination Folder for Zip?member.dan.g.10 Sep '04 - 21:08 
GeneralDoes not work with UNC pathsussAnonymous12 May '04 - 8:45 
GeneralDelete filessussAnonymous27 Apr '04 - 7:42 
GeneralError in link ?!memberDeian25 Jan '04 - 3:51 
GeneralRe: Error in link ?!memberSOFBE8 Jul '04 - 21:54 
QuestionDoes it support subtracting?memberzeon7720 Dec '03 - 8:27 
Questionis this portable to Linux?memberd56bb7 Nov '03 - 10:13 
AnswerRe: is this portable to Linux?memberdang!9 Nov '03 - 12:27 
QuestionHow Can I Unzip without an DLLs inVB6?sussWileySiler29 Oct '03 - 20:55 
AnswerRe: How Can I Unzip without an DLLs inVB6?memberdang!30 Oct '03 - 11:30 
Generalfiles attributes ..memberbebert6 Oct '03 - 1:07 
QuestionUNICODE version not supported ??memberbebert5 Oct '03 - 22:50 

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

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