CZip/CUnzip Classes for Zipping and Unzipping Files

Environment: Win98, VC 5.0

This article describes classes to zip and unzip files.  The classes use the gzip GNU
source code (gzip-1.2.4a).  This is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License.  See the file COPYING
included in the source code.

The target DLL exports two classes:

  • CZip (for zipping files)
  • CUnzip (for unzipping files)

Description:

A CZip or CUnzip
object is built with a file name.  Then, the SwapSize()
method must be called on that object (with one argument which is the target file name) in order to
zip/unzip the file.  SwapSize() may throw a
CZipException exception.  A message bound to the
exception can be retrieved using the GetString() method. 
Exceptions are thrown when:

  • the source file does not exists or cannot be opened for reading
  • the target file cannot be created (for example, this file already exists)
  • the gzip code is returns an error;  in this case, the message bound to the exception
    contains the message returned by the gzip source code via stderr

To use CZip or CUnzip, do the following:

  1. include czip.h in your project
  2. link your project with zipdll.dll using
    zipdll.lib

  3. move zipdll.dll to the same directory as your executable

    Note:  You can also dynamically link to zipdll.dll
    (I haven’t tested that, though).

Here’s an example of how to zip a file:

  try
{
CZip myFileToZip (“file_to_zip_name”);
myFileToZip.SwapSize (“file_zipped_name”);
}
catch (CZipException e)
{
cout << "problem while zipping : " << e.GetString() << endl; delete pExcep; }

Notes:

  1. The CZipped files can be unzipped using WinZip if its name doesn’t have a “.zip” extension. 
    If the zipped file has a “.zip” extension, WinZip won’t be able to open the file.

  2. An archive constructed with WinZip can be extracted using a CUnzip object if it contains only
    a single file.

  3. In order to know the error messages written by gzip code on stderr, a temp file is built and
    attached to stderr.  If this temp file cannot be built (no way to write to the current
    directory for example), the error messages from gzip code (if any) will not be known, but
    the zip/unzip operations can still be achieved.

Date Updated: 2 March 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read