Click here to Skip to main content
Licence CC (ASA 2.5)
First Posted 7 Jun 2003
Views 250,726
Bookmarked 85 times

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

By | 27 Jul 2003 | Article
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

About the Author

.dan.g.

Software Developer
Maptek
Australia Australia

Member

Follow on Twitter Follow on Twitter
.dan.g. is a former chartered structural engineer from the uk. He's been programming since university and has been developing commercial windows software in Australia since 1998.
 
For all his latest freeware visit AbstractSpoon.
Follow him on Facebook, or on Twitter.

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan I use these codes in commercial software? Pinmemberairekans17:41 2 Nov '11  
AnswerRe: Can I use these codes in commercial software? Pinmember.dan.g.20:37 2 Nov '11  
QuestionBug with UNC paths - also on abstractspoon site code, PinmemberIain Clarke, Warrior Programmer22:21 27 Sep '11  
QuestionUnicode version (sort of) PinmemberRoger Bamforth3:47 8 Aug '11  
GeneralZip file not created [modified] Pinmembercotsjdixon3:55 21 Mar '11  
QuestionError when compiling Pinmembercotsjdixon9:03 9 Mar '11  
AnswerRe: Error when compiling Pinmember.dan.g.16:39 9 Mar '11  
Question?? Pinmemberrerainings0:31 14 Jun '10  
GeneralI have made a vc++ 2005 version Pinmemberxiangzi20:46 1 Mar '10  
GeneralRe: I have made a vc++ 2005 version Pinmember.dan.g.16:41 9 Mar '11  
QuestionGetting a compiler error in unzip.h PinmemberSara Thompson16:51 21 Feb '10  
Generalx64 version of this library Pinmembera.tess3:04 16 Dec '08  
GeneralBug in Zipping PinmemberPresu1:34 30 Oct '08  
GeneralAppend to Existing Zip File with zlib 1.2.3 Pinmembercwdonnelly2:25 8 Jun '08  
GeneralHelp me for unicode PinmemberThuy NH20:11 18 Nov '07  
GeneralRe: Help me for unicode PinmemberThuy NH19:04 19 Nov '07  
QuestionProblem with XP unzipping feature PinmemberStefanFrank2:22 10 Sep '07  
AnswerRe: Problem with XP unzipping feature PinmemberStefanFrank21:48 11 Sep '07  
Generalsetting the file pointer in a zip file Pinmemberpauldowd8:13 3 Apr '07  
GeneralZipper Bug (alphabetical ordering of files and folders) Pinmemberstokos14:23 1 Jan '07  
Hi,
 
Firstly, let me say a big THANK YOU for the code. It is very easy to use and extremely useful, especially considering the ugly raw interface of zlib.
 
Now to business. I am using the latest version of the CZipper class (downloaded from www.abstractspoon.com) and I noticed some weird behaviour.
 
I have a collection of files and folders in the root directory of the zip file and while some programs (e.g. WinZip) can use the resulting zip files without any problems, other programs (e.g. Google Earth) cannot. [In case you are wondering, I use CZipper to create Google Earth KMZ archives]. After a lot of debugging and frustration, I realised that the problem lies with the _alphabetical_ ordering of the files and folders in the zip archive. To be more precise, this works:
root/
|
|-- A/ => Folder A
|-- Z/ => Folder Z
|-- F  => File F
This doesn't:
root/
|
|-- A/ => Folder A
|-- B/ => Folder B
|-- F  => File F
The difference is the name of the file and the alphabeticaly last folder. If the name of the file is alphabetically AFTER the name of the last folder, the archive is not usable by some aplications.
 
Before you rush to accuse Google Earth for this weird behaviour, I also unzipped the file and rezipped it with WinZip, in which case Google Earth was more than happy to load it. This all points to a problem in the CZipper class, but I have been unable to locate the source of the problem.
 
Any help will be greatly appreciated! Many thanks.
 
--
/stokos

QuestionNew version? PinmemberDaniel C.20:59 17 Aug '06  
GeneralProblem with unzipping Pinmemberkezhu16:05 8 May '06  
GeneralRe: Problem with unzipping Pinmemberonlysmooth7:20 12 May '06  
GeneralRe: Problem with unzipping Pinmemberabc_pf0:41 23 Oct '06  
GeneralProblem with Zipping Pinmemberviaduct4:47 3 Apr '06  

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.

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