5,693,062 members and growing! (18,349 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate License: The Creative Commons Attribution-ShareAlike 2.5 License

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

By .dan.g.

High level wrapping of the zlib library to make easy work of zipping and unzipping files and folders
VC6, VC7, VC7.1, C++Windows, Win2K, WinXP, Win2003VS6, Visual Studio, Dev

Posted: 7 Jun 2003
Updated: 27 Jul 2003
Views: 148,762
Bookmarked: 55 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 6.57 Rating: 4.45 out of 5
4 votes, 13.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 10.0%
4
23 votes, 76.7%
5

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.


.dan.g. is a former chartered structural engineer from the uk. He's been programming for 25 years since university and has been developing commercial windows software in Australia since 1998. He has a shaved head and assorted whiskers should you want to recognize him. [update] Dan has now grown his hair and removed his whiskers in an attempt to appear more normal. However he can still be recognized (in the summer months) by the aqua nail-polish he uses on his big toes. [/update]

For all his latest freeware visit http://www.abstractspoon.com/
Occupation: Software Developer (Senior)
Location: Australia Australia

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 103 (Total in Forum: 103) (Refresh)FirstPrevNext
GeneralBug in ZippingmemberPresu2:34 30 Oct '08  
GeneralAppend to Existing Zip File with zlib 1.2.3membercwdonnelly3:25 8 Jun '08  
GeneralHelp me for unicodememberThuy NH21:11 18 Nov '07  
GeneralRe: Help me for unicodememberThuy NH20:04 19 Nov '07  
QuestionProblem with XP unzipping featurememberStefanFrank3:22 10 Sep '07  
AnswerRe: Problem with XP unzipping featurememberStefanFrank22:48 11 Sep '07  
Generalsetting the file pointer in a zip filememberpauldowd9:13 3 Apr '07  
GeneralZipper Bug (alphabetical ordering of files and folders)memberstokos15:23 1 Jan '07  
GeneralNew version?memberDaniel C.21:59 17 Aug '06  
GeneralProblem with unzippingmemberkezhu17:05 8 May '06  
GeneralRe: Problem with unzippingmemberonlysmooth8:20 12 May '06  
GeneralRe: Problem with unzippingmemberabc_pf1:41 23 Oct '06  
GeneralProblem with Zippingmemberviaduct5:47 3 Apr '06  
GeneralRe: Problem with ZippingmemberDefenestration14:03 27 Apr '06  
GeneralRe: Problem with Zippingmemberviaduct22:43 27 Apr '06  
AnswerRe: Problem with Zipping [modified]memberphilzzh0:01 1 Aug '07  
Generalfiles here are outdated & buggymemberMax Meier7:54 11 Nov '05  
Generalerror LNK2005memberMichael Klim14:01 21 Jul '05  
Generalhow do I zip folders?sussy2j15019:06 22 Jun '05  
GeneralUnzipping across foldersmembermklabs3:06 16 Dec '04  
GeneralSub-Folder in Zip?sussAnonymous8:17 13 Dec '04  
Generalzip larger than 2Gig?memberjohnd1266:22 1 Oct '04  
GeneralAppend a Zipmemberjeccull10:06 16 Sep '04  
GeneralDestination Folder for Zip?memberjeccull8:07 10 Sep '04  
GeneralRe: Destination Folder for Zip?member.dan.g.22:08 10 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jul 2003
Editor: Chris Maunder
Copyright 2003 by .dan.g.
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project