Click here to Skip to main content
14,032,238 members
Click here to Skip to main content
Add your own
alternative version


281 bookmarked
Posted 15 Jun 2000
Licenced CPOL

Zip and Unzip in the MFC way

, 12 Oct 2011
Rate this:
Please Sign up or sign in to vote.
A library to create, modify, and extract Zip archives.

Sample Image


This library allows creating, modifying, and extracting zip archives in a compatible way with PKZIP (2.5 and higher) and WinZip. Supported are all possible operations on the zip archive: creating, extracting, adding, deleting files from the archive, modifications of the existing archive. There is also support for creating and extracting multiple disk archives (on non-removable devices as well) and for password encryption and decryption. This module uses compression and decompression functions from the zlib library by Jean-loup Gailly and Mark Adler.

How to integrate with the project

Zip is a static library and statically links to the compiled zlib.lib (version 1.13 nowadays). The zlib library can be replaced with a newer version, providing you also replace the files "zlib.h" and "zconf.h" in the Zip project. The Zip library uses MFC in a shared library as a Release and Debug configuration. Your project must use MFC in the same way in the appropriate project configuration. You may need to adapt this to your needs. To add Zip library functionality to your project, you need to link the library to the project. You can do this in at least two ways (in both cases, you need to include the ZipArchive.h header in your sources like this: #include "ZipArchive.h"):

Method 1

Add "..\Zip\debug(release)\ZipArchive.lib" to Project Settings->Link->Input->Object/library modules and add the Zip directory to the preprocessor searches (Project Settings -> C++ -> Preprocessor -> Additional include directories).

Method 2 (simpler)

Insert the Zip project into the workspace and set the project dependencies (your project dependent on the Zip project).

How to use

The details about using this library are in the sources. The example available for download at the bottom of the page is an almost complete compression\decompression program. There are only the main issues mentioned below about using this library. If you have a question about using the library and you can't find the answer here, don't hesitate to ask.

Compression and decompression

There are some functions defined for fast operations on archives; among others: AddNewFile(), ExtractFile(), DeleteFile(). You only need to call the functions Open() - before, and Close() - after using them. Remember to call the Close() function when you finish working with the CZipArchive class.

Multi-disk archives

This library supports two kinds of multi-disk archives:

  1. Disk spanning performed in the compatible way with all other main zip programs. It means that the archive can only be created on a removable device, the size of the volume is auto-detected, and the label is written to the disk. To use this kind of disk spanning, you need to define a static callback function for changing disks and set it with the SetSpanCallback() function.
  2. Disk spanning performed in the internal mode, called in the sources TD span mode. This allows creating multi disk archives also on non-removable devices and with user-defined volume size. There is no need to set the callback function in this mode.

These two disk spanning modes create volumes with compatible internal structure. It means that you can easily convert the volumes created in one mode to the other one by renaming the files (in TD mode, each volume but last has a number as an extension). To convert the archive from TD to PKZIP compatible archive, copy each file to the removable media, giving the extension ".zip". You should also label each disk with the appropriate label starting from "pkback# 001".

There is a limited function set available when working with multi-disk archives. Only adding is allowed when creating an archive and only extracting and testing when opening an existing one. Deleting files from these archives isn't allowed in any of these cases.

The class CZipArchive uses a write buffer to make write operations extremely fast. You can change its size with the SetAdvanced() function. While creating a multi-disk archive, set the size of the buffer to the maximum size of the volume for the best performance.

Popular archivers such as PKZIP and WinZip cannot operate on an archive in TD span mode. You need to convert them to PKZIP span mode (have a look above). Remember about copying the files to the removable media (it does not comply with WinZip, which can extract a multi-disk archive from any media but only from a fixed location on the drive).

Password encryption and decryption

This library supports creating and extracting of password protected archives. There are several issues you should be aware of when using this feature. To set the password for the file to be added or extracted, call the function SetPassword() with the password as the argument. To clear the password, call this function without arguments or with an empty string argument. The function has no effect on a closed archive and on the currently opened file (whether new or existing) inside the archive.

During opening an archive, the password is cleared and it is not changed if the file inside the archive is opened. You can set different passwords for different files inside the same archive, but remember to set it before opening the file. You cannot use ASCII characters with codes above 127 in a password; if you do so, the function SetPassword() returns false and the password is cleared. If the password is cleared, no encryption or decryption takes place.

You can find out what files are password encrypted by calling CZipArchive::GetFileInfo() which fills the structure CZipFileHeader with data, and then the method IsEncrypted() of this structure. If it returns true, the file needs a password to extract.

Successful extraction of the encrypted file doesn't always mean that the password is correct. CZipArchive doesn't check for a CRC if m_info.m_uUncomprLeft is not zero in the function CZipArchive::CloseFile(). In some cases, a bad password causes that this value is not zero, so you have to also check for the return value of this function (it returns -1 in this case). You can also check the size of the extracted file since it will be smaller than it should be.

Self extract support

The library automatically detects self-extracting archives. This is the simplest self-extract code:

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
    CZipArchive zip;

    // get path of executable
    TCHAR szBuff[_MAX_PATH];
    if (!::GetModuleFileName(hInstance, szBuff, _MAX_PATH))

        return -1;

    CString szDest;
    // ...
    // add the code here to get the destination directory from the user 
    // for example:
    /* CBrowseForFolder bf;
       bf.strTitle = _T("Select directory to extract files");
       if (!bf.GetFolder(szDest))
           return -1;
    // class CBrowseForFolder is included in the example project
    // remember about including the header!

    zip.Open(szBuff, CZipArchive::openReadOnly); 
    // openReadOnly mode is necessary for self extract archives
    for (WORD i = 0; i < zip.GetNoEntries(); i++)
        zip.ExtractFile(i, szDest);

    return 0;
    // this code will not work for the encrypted archives since it is needed
    // to get the password from the user ( a small addition to the 
    // existing code I suppose )

After compiling it and appending a zip archive to it (e.g., with the DOS command: copy /b SelfExtract.exe + FinalFile.exe), we have a self extracting archive.

Testing the integrity of the archive

The new functions have been provided to allow the testing of the integrity of the archive. The first one is CZipArchive::TestFile which is a comprehensive testing function, but if you need a different functionality, you can make your own by taking advantage of the second function provided: CZipArchive::CloseFileAfterTestFailed. The detailed description and the example of use are provided in the sources and in the example project.


The library throws the following exceptions: CMemoryException, CFileExeption, and CZipException. The first two don't need an explanation. The last is thrown when some internal error occurs. Handling them may be done in the following way:

    // ...
    // some operations on Zip library
catch (CException* e)
    if (e->IsKindOf( RUNTIME_CLASS( CZipException )))
        CZipException* p = (CZipException*) e;
        //... and so on 
    else if (e->IsKindOf( RUNTIME_CLASS( CFileException )))
        CFileException* p = (CFileException*) e;
        //... and so on 
        // the only possibility is a memory exception I suppose
        //... and so on 

Handling the extra field

The extra field of the file can be different in the local and central headers. To set the extra filed in the local header, set it in the header parameter of the CZipArchive::OpenNewFile function. After that the extra field is written to the local header and cleared. You should call the CZipArchive::SetExtraField function anytime after opening the new file (but before closing it) to set the file extra field in the central directory.

Sample application

To run the example, integrate the first Zip library into it (the Zip library is not included in the example, you must download it separately); you should be asked at the beginning about the location of the Zip project, if not, use one of the integration methods described above. If you don't put the Zip library project at the same directory level that the sample application is, you also have to change the path pointing to ZipArchive.h in the file testzipdlgdlg.h.

Linking errors

When you experience linking errors (mostly LNK2005), you need to make sure that the library and your program are both using the single-threaded or both multithreaded library. The option "Project->Settings-> c/c++ ->Code Generation->Use run-time library" should be set to the same value in the ZipArchive library and the program project options.

If it doesn't help, try recompiling the zlib library (zlib.lib provided with the project is compiled from a release configuration and uses the multithreaded DLL run-time library). You can download the zlib library sources from Use the zlibstat project. Set the "Use run-time library" option to the same value as you set it in the ZipArchve library and your program configurations. Compile the Release configuration of the zlib library and replace it with the file zlib.lib in the ZipArchive folder. You may however experience linking warnings or errors while compiling the Debug configuration. To eliminate them, do as follows:

  • Compile the Debug and Release configurations of the zlib library.
  • Copy zlib_d.lib from Debug and zlib.lib from the Release directory to the ZipArchive directory.
  • Add these files to the ZipArchive project (Project -> Add To Project -> Files...).
  • Go to the project settings dialog (Project -> Settings).
  • Select debug configurations (Setting For -> Multiple Configurations) such as Debug and Unicode Debug.
  • Select the zlib.lib file, then the General tab, and check the box "Exclude file from build".
  • Select release configurations.
  • Select zlib_.lib and exclude it from these builds.

To-do and updates

I am currently working on the non-MFC version of the library and multi-platform support. Make sure to check out the site which is more likely to have an updated version.



  • When the central directory is not located, the library throws CZipException::cdirNotFound, which allows distinguishing from other exceptions (useful when we want to keep prompting the user to insert the last disk in a multi-disk spanning archive).


Features added:

  • Ability to reuse the archive after an exception thrown during extraction.
  • Added progress control possibilities to CZipArchive::AddNewFile, CZipArchive::ExtractFile, CZipArchive::TestFile.


  • CZipArchive::FindFile operation boosted (thanks to Darin Warling for the idea).
  • Library name changed to ZipArchive.

Bugs fixed:

  • Removed bug during extracting an encrypted file with 0 size.
  • Fixed bug when extracting a file with an extra field in a local file header (CZipFileHeader::ReadLocal).


  • Disk numbering in a disk spanning archive begins now from PKBACK# 001, not PKBACK# 000.
  • Adding and extracting without a full path possible in CZipArchive::AddNewFile and CZipArchive::ExtractFile.
  • Several minor changes and enhancements.
  • Changed names for several classes.


  • Added support for password encryption and decryption. The encryption used in PKZIP was generously supplied by Roger Schlafly.
  • Testing the archive made easier.
  • Unicode support added.


  • Bugs fixed.


The code has been completely rewritten since the very beginning; the main improvements are:

  • Multi-disk archive support.
  • Creation of disk spanning archives with the user-defined volume size.
  • Ability to modify existing archives (add, delete files).
  • Modification of self-extracting archives.
  • Write buffer used for faster disk write operations.
  • One class for zip and unzip functions.
  • Fast adding, deleting, and extracting files with a single function call.


  • International characters in filenames inside archive are now converted in a compatible way with other archiving programs (they are stored converted to OEM inside archive).


First version; it is just modified code from zip.c and unzip.c files written by Gilles Vollant and distributed with the zlib library; the modifications are as follows:

  • Added class wrappers.
  • Several bugs fixed.
  • Several enhancements added.
  • MFC support added.
  • Memory leaks eliminated when write error occurs.
  • Automatically frees used memory on destruction or exception.
  • Modern error notification using exceptions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tadeusz Dracz
Web Developer
Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionDoes Not Compile & Does Not Add Files Pin
Member 100929214-Jun-13 17:20
memberMember 100929214-Jun-13 17:20 
AnswerRe: Does Not Compile & Does Not Add Files Pin
Sergej8916-Feb-15 20:12
memberSergej8916-Feb-15 20:12 
AnswerRe: Does Not Compile & Does Not Add Files Pin
Tadeusz Dracz11-Jun-15 1:29
professionalTadeusz Dracz11-Jun-15 1:29 
GeneralMy vote of 5 Pin
gndnet7-Nov-12 7:14
membergndnet7-Nov-12 7:14 
BugCZipStorage::Close and CMemFile Pin
voyk10-Mar-12 12:21
membervoyk10-Mar-12 12:21 
GeneralRe: CZipStorage::Close and CMemFile Pin
Tadeusz Dracz11-Mar-12 10:17
professionalTadeusz Dracz11-Mar-12 10:17 
QuestionCannot statically link to MFC in release mode Pin
masteryoda2128-Oct-11 12:16
membermasteryoda2128-Oct-11 12:16 
AnswerRe: Cannot statically link to MFC in release mode Pin
Tadeusz Dracz28-Oct-11 12:28
professionalTadeusz Dracz28-Oct-11 12:28 
GeneralRe: Cannot statically link to MFC in release mode Pin
masteryoda2128-Oct-11 13:44
membermasteryoda2128-Oct-11 13:44 
GeneralRe: Cannot statically link to MFC in release mode Pin
Tadeusz Dracz28-Oct-11 22:58
professionalTadeusz Dracz28-Oct-11 22:58 
AnswerRe: Cannot statically link to MFC in release mode Pin
kezhu27-Jan-16 17:35
memberkezhu27-Jan-16 17:35 
QuestionDid I miss something or? Pin
Abin9-Oct-11 21:19
memberAbin9-Oct-11 21:19 
AnswerRe: Did I miss something or? Pin
Tadeusz Dracz9-Oct-11 23:00
professionalTadeusz Dracz9-Oct-11 23:00 
GeneralRe: Did I miss something or? Pin
Abin10-Oct-11 15:17
memberAbin10-Oct-11 15:17 
GeneralRe: Did I miss something or? Pin
Tadeusz Dracz11-Oct-11 11:54
professionalTadeusz Dracz11-Oct-11 11:54 
QuestionSupport for > 65,535 files [modified] Pin
masteryoda2112-Jul-11 7:58
membermasteryoda2112-Jul-11 7:58 
AnswerRe: Support for > 65,536 files Pin
Tadeusz Dracz12-Jul-11 8:02
professionalTadeusz Dracz12-Jul-11 8:02 
GeneralUse for Borland C++ Pin
feihao8810-Jun-10 21:07
memberfeihao8810-Jun-10 21:07 
QuestionBug in CZipArchive::ExtractFile() ? Pin
Sie Raybould14-Mar-10 11:31
memberSie Raybould14-Mar-10 11:31 
AnswerRe: Bug in CZipArchive::ExtractFile() ? Pin
Tadeusz Dracz14-Mar-10 23:37
professionalTadeusz Dracz14-Mar-10 23:37 
GeneralCompilation errors Pin
Jiljith5-Mar-10 0:19
memberJiljith5-Mar-10 0:19 
GeneralRe: Compilation errors Pin
Tadeusz Dracz5-Mar-10 5:28
professionalTadeusz Dracz5-Mar-10 5:28 
QuestionServeral Questions Pin
Hong Joon Kim1-Dec-09 20:46
memberHong Joon Kim1-Dec-09 20:46 
AnswerRe: Serveral Questions Pin
Hong Joon Kim1-Dec-09 23:23
memberHong Joon Kim1-Dec-09 23:23 
GeneralRe: Serveral Questions Pin
Tadeusz Dracz2-Dec-09 21:46
professionalTadeusz Dracz2-Dec-09 21:46 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 12 Oct 2011
Article Copyright 2000 by Tadeusz Dracz
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid