Click here to Skip to main content
15,867,453 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 460.9K   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

 
GeneralRe: UnzipTo() Problem Pin
.dan.g.27-Jul-03 14:31
professional.dan.g.27-Jul-03 14:31 
GeneralRe: UnzipTo() Problem Pin
.dan.g.28-Jul-03 0:49
professional.dan.g.28-Jul-03 0:49 
GeneralRe: UnzipTo() Problem Pin
.dan.g.28-Jul-03 1:09
professional.dan.g.28-Jul-03 1:09 
GeneralRe: UnzipTo() Problem Pin
ClickHeRe28-Jul-03 1:40
ClickHeRe28-Jul-03 1:40 
GeneralRe: UnzipTo() Problem Pin
.dan.g.28-Jul-03 14:28
professional.dan.g.28-Jul-03 14:28 
GeneralRe: UnzipTo() Problem Pin
.dan.g.7-Aug-03 21:07
professional.dan.g.7-Aug-03 21:07 
GeneralRe: UnzipTo() Problem Pin
ClickHeRe28-Jul-03 1:48
ClickHeRe28-Jul-03 1:48 
GeneralRe: UnzipTo() Problem Pin
.dan.g.28-Jul-03 14:27
professional.dan.g.28-Jul-03 14:27 
GeneralAnother Issue Pin
ClickHeRe28-Jul-03 17:00
ClickHeRe28-Jul-03 17:00 
GeneralRe: Another Issue Pin
.dan.g.28-Jul-03 17:16
professional.dan.g.28-Jul-03 17:16 
GeneralRe: Another Issue Pin
ClickHeRe6-Aug-03 7:59
ClickHeRe6-Aug-03 7:59 
GeneralRe: Another Issue Pin
.dan.g.6-Aug-03 12:48
professional.dan.g.6-Aug-03 12:48 
GeneralRe: UnzipTo() Problem Pin
.dan.g.28-Jul-03 22:48
professional.dan.g.28-Jul-03 22:48 
GeneralRe: UnzipTo() Problem Pin
ClickHeRe29-Jul-03 1:34
ClickHeRe29-Jul-03 1:34 
GeneralLNK2001 Problem Pin
ClickHeRe25-Jul-03 7:40
ClickHeRe25-Jul-03 7:40 
GeneralRe: LNK2001 Problem Pin
ClickHeRe25-Jul-03 14:58
ClickHeRe25-Jul-03 14:58 
GeneralRe: LNK2001 Problem Pin
hungrytom20-Aug-07 22:50
hungrytom20-Aug-07 22:50 
GeneralRe: LNK2001 Problem Pin
hungrytom21-Aug-07 0:19
hungrytom21-Aug-07 0:19 
GeneralSome bugs exist! Pin
AlvaChin23-Jul-03 0:20
AlvaChin23-Jul-03 0:20 
GeneralRe: Some bugs exist! Pin
.dan.g.23-Jul-03 14:10
professional.dan.g.23-Jul-03 14:10 
GeneralRe: Some bugs exist! Pin
.dan.g.24-Jul-03 1:24
professional.dan.g.24-Jul-03 1:24 
GeneralRe: Some bugs exist! Pin
AlvaChin24-Jul-03 15:19
AlvaChin24-Jul-03 15:19 
GeneralUse it in Managed C++ Pin
Lang Deng21-Jul-03 11:02
Lang Deng21-Jul-03 11:02 
GeneralRe: Use it in Managed C++ Pin
.dan.g.21-Jul-03 16:14
professional.dan.g.21-Jul-03 16:14 
QuestionBug?? Pin
AlphaDog20-Jul-03 11:24
AlphaDog20-Jul-03 11:24 

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.