Click here to Skip to main content
Click here to Skip to main content
Go to top

XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dll

, 18 Jul 2007
Rate this:
Please Sign up or sign in to vote.
XZip and XUnzip provide non-MFC functions to create a zip, add files to it, and extract files from it - all in two .cpp files

Introduction

I have already introduced XZip in a previous article. This article presents XZip and also XUnzip, which together allow you to add zip and unzip to your application without using any .lib or .dll.

First, let me acknowledge the work of Lucian Wischik, who took the many .c and .h files from Info-ZIP and produced the .cpp and .h files that XZip is based on.

XZip and XUnzip Features

Most of the functions are demonstrated in the XZip demo app. Here are the main ones:

  • CreateZip() - Create a zip archive file.
//////////////////////////////////////////////////////////////////////////////
//
// CreateZip()
//
// Purpose:     Create a zip archive file
//
// Parameters:  z     - archive file name if flags is ZIP_FILENAME;  for other
//                       uses see below
//              len   - for memory (ZIP_MEMORY) should be the buffer size;
//                       for other uses, should be 0
//              flags - indicates usage, see below;  for files, this will be
//                       ZIP_FILENAME
//
// Returns:     HZIP  - non-zero if zip archive created ok, otherwise 0
//
  • ZipAdd() - Add a file to a zip archive.
//////////////////////////////////////////////////////////////////////////////
//
// ZipAdd()
//
// Purpose:     Add a file to a zip archive
//
// Parameters: hz      - handle to an open zip archive
//             dstzn   - name used inside the zip archive to identify the file
//             src     - for a file (ZIP_FILENAME) this specifies the filename
//                        to be added to the archive;  for other uses, see 
//                        below
//             len     - for memory (ZIP_MEMORY) this specifies the buffer
//                        length;  for other uses, this should be 0
//             flags   - indicates usage, see below;  for files, this will be
//                        ZIP_FILENAME
//
// Returns:    ZRESULT - ZR_OK if success, otherwise some other value
//
  • OpenZip() - Open an existing zip archive file.
//////////////////////////////////////////////////////////////////////////////
//
// OpenZip()
//
// Purpose:     Open an existing zip archive file
//
// Parameters:  z      - archive file name if flags is ZIP_FILENAME;  for 
//                        other uses see below
//              len    - for memory (ZIP_MEMORY) should be the buffer size;
//                       for other uses, should be 0
//              flags  - indicates usage, see below;  for files, this will be
//                       ZIP_FILENAME
//
// Returns:     HZIP   - non-zero if zip archive opened ok, otherwise 0
//
  • GetZipItem() - Get information about an item in an open zip archive.
//////////////////////////////////////////////////////////////////////////////
//
// GetZipItem()
//
// Purpose:     Get information about an item in an open zip archive
//
// Parameters:  hz      - handle of open zip archive
//              index   - index number (0 based) of item in zip
//              ze      - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
//                        (if Unicode)
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//
  • FindZipItem() - Find item by name and return information about it.
//////////////////////////////////////////////////////////////////////////////
//
// FindZipItem()
//
// Purpose:     Find item by name and return information about it
//
// Parameters:  hz      - handle of open zip archive
//              name    - name of file to look for inside zip archive
//              ic      - TRUE = case insensitive
//              index   - pointer to index number returned, or -1
//              ze      - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
//                        (if Unicode)
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//
  • UnzipItem() - Find item by index and unzip it.
//////////////////////////////////////////////////////////////////////////////
//
// UnzipItem()
//
// Purpose:     Find item by index and unzip it
//
// Parameters:  hz      - handle of open zip archive
//              index   - index number of file to unzip
//              dst     - target file name of unzipped file
//              len     - for memory (ZIP_MEMORY. length of buffer;
//                        otherwise 0
//              flags   - indicates usage, see below;  for files, this will be
//                        ZIP_FILENAME
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//
  • CloseZip() - Close an open zip archive.
//////////////////////////////////////////////////////////////////////////////
//
// CloseZip()
//
// Purpose:     Close an open zip archive
//
// Parameters:  hz      - handle to an open zip archive
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//

How To Use

To integrate XZip into your app, you first need to add following the files to your project:

  • XZip.cpp
  • XZip.h
  • XUnzip.cpp
  • XUnzip.h

If you include XZip in a project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XZip.cpp and XUnzip.cpp.

Next, include the header files XZip.h and XUnzip.h in appropriate project files. Now you are ready to start using XZip. There are many notes concerning usage of various functions in XZip.h and XUnzip.h. Please read all function headers for each function you wish to use.

Known Limitations

XZip and XUnzip have been tested only with files.

Demo App

The XZipTest.exe demo tests the APIs in XZip and XUnzip. Here is some of the output:

screenshot

Frequently Asked Questions

  1. Can I use XZip in non-MFC apps?
    Yes. It has been implemented to compile with any Win32 program.
  2. When I try to include XZip.cpp in my MFC project, I get the compiler error XZip.cpp(2918) : fatal error C1010: unexpected end of file while looking for precompiled header directive. How can I fix this?
    When using XZip in project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XZip.cpp and XUnzip.cpp. Be sure to do this for All Configurations.

screenshot


  1. When I try to build the demo app, I get the linker error LINK : fatal error LNK1104: cannot open file "mfc42u.lib" Error executing link.exe. How can I fix this?
    The default installation options of Visual C++ v6.0 don't install the Unicode libraries of MFC, so you might get an error that mfc42u.lib or mfc42ud.lib cannot be found. You can fix this either by installing the Unicode libs from the VC++ install CD, or by going to Build | Set Active Configuration and selecting one of the non-Unicode configurations.

    screenshot

    You can configure the Visual Studio toolbars to include the Select Active Configuration combobox. This allows you to see at a glance what configuration you are working with.

  2. I don't need the Zip/Unzip functions. Can I exclude XZip.cpp/XUnzip.cpp?
    Yes. You only need to include the .h/.cpp pair that you need.
  3. Can we use XZip in our (shareware/commercial) app?
    Yes, you can use XZip without charge or license fee, providing you follow the Info-ZIP restrictions as defined in XZip.cpp.
  4. Does XZip handle pipes? in-memory zipping?
    XZip has not been tested with anything other than files.
  5. Can I use XZip in a VS2005 project?
    Yes. There is a sample VS2005 project included in the download.
  6. Does XZip work on Vista?
    Yes.

Acknowledgments

Revision History

Version 1.3 - 2007 July 18

  • Fixed problem with file size that is multiple of 16384, reported by Mathias Svensson.
  • Fixed XZip to save file time in local time, suggested by Damir Valiulin.

Version 1.2 - 2007 June 30

  • Added project for VS2005.
  • Added AddFolderContent() contributed by Renaud Deysine.
  • Fixed problem with TUnzip::Open() reported by Pete Howells. Open() now returns correct success code.
  • Fixed several bugs reported by Warren Stevens.
  • Fixed a problem in unzReadCurrentFile() reported by Kochise.
  • Fixed bug in EnsureDirectory() reported by craigmj.
  • Changed ideflate() suggested by Michael B. Hansen.
  • Fixed problem with time_t reported by Ronney.
  • Fixed problems found by Boundschecker as reported by Warren Stevens.
  • Made changes to PUTSHORT and PUTBYTE macros and to TZip::write(), suggested by vielheit.

Version 1.1 - 2003 May 7

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
Bugtwo bugs. Pinmemberjcyangzh10-Aug-14 20:33 
QuestionHow to display the Info-Zip Licenee PinmemberSeverin Friede25-Mar-14 22:40 
QuestionEnsureDirectory leaves temp directories [modified] PinmemberMarkus Bst13-Mar-13 2:24 
GeneralReally useful job Pinmemberzirandeai25-Feb-13 18:53 
QuestionCString with path and file name [modified] PinmemberMike Gaskey21-Jan-13 7:11 
QuestionBugs found and fixed. Pinmemberluuxuanduan8-Nov-12 23:12 
Hi guys,

Thanks for the code. I've tried it and has found and fixed 2 bugs:
1. In BOOL AddFolderContent(HZIP hZip, TCHAR* AbsolutePath, TCHAR* DirToAdd) (it didn't work):

I've fixed as follows:
 
//Duan fixed bug
CString stPath;
stPath = AbsolutePath;
stPath += RelativePathNewFileFound;
if (ZipAdd(hZip, RelativePathNewFileFound, /*RelativePathNewFileFound*/(TCHAR*)((LPCTSTR)stPath), 0, ZIP_FILENAME) != ZR_OK)
{
return FALSE;
}
 

2. Bug for Unicode mode for adding a folder in a zip: In ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags)

I've fixed as follows:
 
//Duan added || flags == ZIP_FOLDER
if (flags == ZIP_FILENAME || flags == ZIP_FOLDER)
{
char szDest[MAX_PATH*2];
memset(szDest, 0, sizeof(szDest));

#ifdef _UNICODE
// need to convert Unicode dest to ANSI
int nActualChars = WideCharToMultiByte(CP_ACP, // code page
0, // performance and mapping flags
(LPCWSTR) dstzn, // wide-character string
-1, // number of chars in string
szDest, // buffer for new string
MAX_PATH*2-2, // size of buffer
NULL, // default for unmappable chars
NULL); // set when default char used
if (nActualChars == 0)
return ZR_ARGS;
#else
strcpy(szDest, dstzn);
#endif

lasterrorZ = zip->Add(szDest, src, len, flags);
}
else
{
lasterrorZ = zip->Add((char *)dstzn, src, len, flags);
}
 

Best Regards,
Duan.
SuggestionFindZipItem support Windows RelativePath [modified] Pinmemberdaviyang9-Apr-12 0:56 
BugWrong file time set in decompressed file in some cases - fix PinmemberFrank Kobs15-Dec-11 0:06 
BugBug in TUnzip::Get lets UnzipItem create read-only files PinmemberThomas Haase1-Dec-11 0:45 
Question_stricmp / _tzset instead of #pragma warning(disable : 4996) PinmemberThomas Haase30-Nov-11 23:24 
QuestionUpdated version available? PinmemberDale Fugier25-Nov-11 12:05 
AnswerRe: Updated version available? PinmemberThomas Haase30-Nov-11 22:31 
BugZipAdd(..., ZIP_MEMORY) With Unicode FileName Bug And Solution [modified] PinmemberMember 833035718-Oct-11 16:21 
Questionflag ZIP_FOLDER for CreateZip not working Pinmemberkezhu10-Aug-11 13:40 
AnswerRe: flag ZIP_FOLDER for CreateZip not working [modified] PinmemberSteven L Christy23-Aug-11 13:55 
GeneralUnzip Larger Than 2GB zip Files (solution) PinmemberPeter V.24-May-11 2:59 
AnswerRe: Unzip Larger Than 2GB zip Files (solution) PinmentorHans Dietrich24-May-11 3:04 
GeneralRe: Unzip Larger Than 2GB zip Files (solution) Pinmembertroichet26-Jun-12 17:50 
GeneralSmall Unzip Usage Modification Pinmemberxpmule10-Apr-11 21:12 
AnswerRe: Small Unzip Usage Modification PinmentorHans Dietrich10-Apr-11 21:21 
GeneralRe: Small Unzip Usage Modification Pinmemberxpmule21-Apr-11 17:59 
GeneralBetter ver of AddFolderContent() Pinmemberxpmule1-Mar-11 19:44 
GeneralRe: Better ver of AddFolderContent() PinmentorHans Dietrich1-Mar-11 20:08 
GeneralRe: Better ver of AddFolderContent() Pinmemberxpmule2-Mar-11 10:34 
GeneralRe: Better ver of AddFolderContent() Pinmemberxpmule2-Mar-11 13:42 
GeneralUnzip bug fix Pinmemberxpmule27-Feb-11 10:56 
GeneralRe: Unzip bug fix Pinmemberxpmule10-Apr-11 11:21 
AnswerRe: Unzip bug fix PinmentorHans Dietrich10-Apr-11 17:21 
GeneralBug+solution: Non ANSI filenames in UNICODE build Pinmembertbrammer15-Jul-10 4:09 
GeneralRe: Bug+solution: Non ANSI filenames in UNICODE build Pinmemberchaa29-Sep-10 22:49 
AnswerRe: Bug+solution: Non ANSI filenames in UNICODE build PinmentorHans Dietrich10-Apr-11 17:22 
AnswerIncorrect return value from unzReadCurrentFile PinmemberTiL_MtL28-Mar-10 17:14 
GeneralQuestion about usage with Qt, MinGw + gcc (cross-plattform code) PinmemberMember 9537186-May-09 23:29 
GeneralShowing progress of zip operation Pinmember Aljechin 5-May-09 1:00 
AnswerRe: Showing progress of zip operation PinmentorHans Dietrich10-Apr-11 17:23 
GeneralBug for time convert Pinmemberbabiq11-Feb-09 22:29 
GeneralFew winapi calls away from WM compatibility PinmemberVelja Radenkovic6-Feb-09 6:22 
General64-bit build issue (_USE_32BIT_TIME_T) PinmemberDale Fugier19-Jan-09 8:53 
GeneralRe: 64-bit build issue (_USE_32BIT_TIME_T) Pinmemberehsiung1-Mar-11 3:55 
GeneralRe: 64-bit build issue (_USE_32BIT_TIME_T) --> solution Pinmembertbrammer20-Oct-11 22:11 
GeneralRe: 64-bit build issue (_USE_32BIT_TIME_T) --> solution PinmemberThomas Haase30-Nov-11 22:21 
Generalfilesize over 4gb PinmemberMember 266531411-Jan-09 3:08 
GeneralBug for UNICODE-build in ZipAdd() causes AddFolderContent() to fail Pinmembertbrammer19-Nov-08 23:54 
QuestionHow to show the progress? PinmemberMember 176444817-Nov-08 21:28 
QuestionCan't Open ZIP File (Handle is NULL) PinmemberHumanJHawkins17-Oct-08 13:50 
AnswerRe: Can't Open ZIP File (Handle is NULL) PinmemberHumanJHawkins20-Oct-08 6:40 
GeneralAddFolderContent bug... [modified] PinmemberMario M.17-Oct-08 0:51 
QuestionWhen Pulling One File from a .Zip, is whole .zip in memory? PinmemberHumanJHawkins16-Oct-08 13:49 
QuestionHow to retrieve the whole ZIP size in MEMORY mode? PinmemberFrank Isensee8-Oct-08 22:28 
AnswerSolved! PinmemberFrank Isensee8-Oct-08 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 18 Jul 2007
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid