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



Downloads

Comments

  • UNZIP to a buffer or memory

    Posted by Legacy on 10/10/2003 12:00am

    Originally posted by: Rajesh Sonti

    Using c or c++ program while uncopressing encountered zip file inside a zip file,how to read the contents of the inner zip file without extracting it to hard disk .

    I could read the metadata(File name,size,creation time etc.,) for the outer zip contents since it is on hard disk and absolute path for its contents are known.But the inner zip file does not have any absolute path to read metadata for its contents, moreover i do not want it to extract to temp folder to read the metadata instead i want it to explode in memory and read the metadata of its contents.How I can do that using c or c++.

    It is as good as uncompressing the inner zip file to a buffer.
    please provide me code or info regarding this..

    thanks,
    Rajesh Sonti

    Reply
  • Compression Level?

    Posted by Legacy on 09/15/2003 12:00am

    Originally posted by: MindBlaster

    Just wondering about the compression level.
    Is it set to default or maximum?

    If it is changeable, then where?

    Thanks for a superb source! :)

    // MindBlaster

    Reply
  • very good..!!

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Seo won

    thanks very much!!
    your class is very helpful to me..!!

    have a nice day~

    Reply
  • How to zip a folder

    Posted by Legacy on 02/24/2003 12:00am

    Originally posted by: Silvere

    Any code to zip a complete folder. Is there a better solution instead of using CInfoZip class.

    Reply
  • Need C++based code(.dll)

    Posted by Legacy on 10/09/2002 12:00am

    Originally posted by: rani

    Its is completly c based code ,Can u provide me c++ based code!

    Reply
  • you can open the zipped file with winzip when it has a .gz extension

    Posted by Legacy on 05/20/2002 12:00am

    Originally posted by: icestar

    you can open the zipped file with winzip when it has a .gz extension,for it only be deflated,while not been archived!

    Reply
  • How to zip directory

    Posted by Legacy on 12/09/2001 12:00am

    Originally posted by: Kurt

    Hi

    Does anybody know how to zip files in directory in one zip file with CZip

    Reply
  • What about a static library?

    Posted by Legacy on 10/16/2001 12:00am

    Originally posted by: Rimantas

    Hello, all,

    Is there a static library, or could anybody help making one?

    Thanks for help
    Rimantas

    Reply
  • How can I uncompress a memeroy data in "deflate" format? Who can help me?

    Posted by Legacy on 03/27/2001 12:00am

    Originally posted by: Bright

    I have got the compressed data block in deflate format,such as "0x2b,0x49,0x2d,0x2e,0x51,0xa8,0xca,0x2c,0x50,0x28,0x49,0xad,0x28,0x29,0x31,0x42,0x03,0x00". Who can help me uncompress it?

    Reply
  • How to compressing memory with CZip?

    Posted by Legacy on 12/01/2000 12:00am

    Originally posted by: Long Nguyen

    I was download the Czip source, it good to compress a file, but i want to compress memory but i coundn't do? You can help me? Thanks!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprises today must focus on digital transformation to remain competitive or disrupt their industries. The foundation for successful transformation is the adoption of a cloud-first mindset. However, IT organizations must first address legacy infrastructure and fragmented management tools that were not designed for the speed and flexibility of the cloud and digital era. Read this IDC Technology Spotlight paper to explore: Why digital transformation is driving a shift to a cloud-centric enterprise Key …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date