CZip/CUnzip Classes for Zipping and Unzipping Files | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 2, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.