Click here to Skip to main content
Click here to Skip to main content

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)

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

 
GeneralXZip and XUnzip Pinmembernikolae11-Dec-03 21:47 
GeneralProblem in TUnzip::Get ---> creates sometimes read-only files without plausible reason PinmemberThomas Haase3-Dec-03 3:26 
GeneralCreating multiple archives Pinmemberwayop200026-Oct-03 4:37 
GeneralERROR : XUnzip.cpp PinmemberKochise16-Oct-03 3:21 
GeneralSure? PinmemberThomas Haase5-Dec-03 1:25 
GeneralRe: ERROR : XUnzip.cpp Pinsusscodeproject123413-Mar-04 17:46 
GeneralAnother ERROR : XUnzip.cpp PinmemberKochise6-Jul-05 22:24 
GeneralRe: Another ERROR : XUnzip.cpp PinmemberKochise8-Jul-05 0:02 
And just to stop moaning about how zlib is perfect and well tested, that I'm a moron so far, just read this :
 
http://www.eweek.com/article2/0,1895,1834632,00.asp
 
Reproduced here for archiving :
 
"A serious security flaw has been identified in Zlib, a widely used data compression library. Fixes have begun to appear, but a large number of programs could be affected.
 
Zlib is a data compression library that is used by many third-party programs and is distributed with many operating systems, including many Linux and BSD distributions.
 
Microsoft Corp. and other proprietary software companies also use the library in many programs. These companies can do so because Zlib is licensed under liberal BSD-style license.
 
This isn't the first time that the popular Zlib has been the center of a security concern. In 2002, a problem with how it handled memory allocation became a major concern.
 
This time, the flaw is a buffer overflow in the decompression process. Because the program doesn't properly validate input data, it can be fed bad data, which can lead to a buffer overflow.
 
This, in turn, means that if a user opens a file with a Zlib-enabled application, such as a Web browser or data compression tool, which contains specially malformed compressed data, an attacker could execute arbitrary code as the user. If this user were running as a system administrator the flaw would run at that level as well.
 
Read details here about open-source security tools on view at InfoSec.
 
The flaw was discovered by Tavis Ormandy of the Gentoo Linux Security Audit Team.
 
Since Zlib is so ubiquitous, this represents a serious security concern.
 
It's not clear how many programs are affected, but some operating system distributions are widely exposed. According to one source, numerous key packages in the Fedora Core 3 distribution use Zlib. Symantec Corp. reports that AIX, Debian, FreeBSD, Gentoo, SuSE, Red Hat, Ubuntu and many other operating systems are affected.
 
Some versions of Microsoft's DirectX, FrontPage, Internet Explorer, Office, Visual Studio, Messenger and the Windows InstallShield program, among other programs, also use Zlib and are potentially vulnerable.
 
Microsoft is currently looking into what vulnerabilities may exist in its software because of the Zlib problem.
 
As Ormandy said, "Everything from the Linux kernel to OpenSSH, Mozilla and Internet Explorer makes use of Zlib, and any application that understands PNG [portable network graphics] image [format] is likely to use it."
 
Click here to read about vulnerabilities in two popular open-source projects, phpMyAdmin and phpBB.
 
If exploited, this flaw could lead to DoS (denial of service) attacks on the targeted machine. This buffer overflow could also be used to allow a hacker unaurhorized access to a system.
 
At this time, however, Symantec reports, there are no known exploits.
 
In the open-source operating systems, deploying application fixes for this problem will tend to be straightforward. That's because in these operating systems the Zlib library is usually linked dynamically to applications. Thus, simply updating the operating system with the new library will take care of the problem for most applications. On other systems, however, and even with some open-source applications, each application will need to be patched.
 
"Zlib is statically linked quite often, especially on non-Unix platforms such as Windows; however, on Linux, BSD and [similar operating systems] it's more conventional to use dynamic linking, especially as Zlib is so widely used on these platforms that it reduces lots of unnecessary duplication," explained Ormandy.
 
Activity at the Zlib development site has been sparse for some time, and the main developers seem to have moved on to other projects. We received no response to our attempts to contact the developers in time for this story.
 
However, Ormandy said, "Zlib is very mature and stable, so development is sporadic, but it's certainly not dead. Mark Adler [a Zlib co-author] responded to my report with a patch and an in-depth investigation and explanation within 24 hours, and I believe he expects to release a new version of Zlib very soon."
 
In the meantime, many open-source operating systems already have patches for the buffer problem. These include Debian, FreeBSD, Gentoo, SuSE and Ubuntu.
 
Check out eWEEK.com's Security Center for the latest security news, reviews and analysis. And for insights on security coverage around the Web, take a look at eWEEK.com Security Center Editor Larry Seltzer's Weblog."
 
Kochise
 
In Code we trust !
Generalreading uninitialized memory PinmemberWarren Stevens15-Oct-03 6:36 
GeneralRe: reading uninitialized memory PinmemberHans Dietrich15-Oct-03 10:01 
GeneralRe: reading uninitialized memory PinmemberWarren Stevens16-Oct-03 5:46 
Questioncross-platform... or not? Pinmemberphaedrus17-Sep-03 15:11 
AnswerRe: cross-platform... or not? PinmemberHans Dietrich15-Oct-03 10:09 
GeneralBuffer Overflow Vulnerability PinsussLuca Ercoli7-Sep-03 14:18 
GeneralTUnzip::Open() problem PinsussPete Howells7-Sep-03 11:10 
GeneralPassword Pinsussfuzzycontrole4-Sep-03 22:27 
GeneralNice.. PinmemberAndreas Weller28-Aug-03 5:04 
QuestionProgress Bar ?!? PinmemberRisto Avila14-Aug-03 5:57 
Question.NET assembly ? PinmemberBernhard Hofmann21-Jul-03 4:20 
AnswerRe: .NET assembly ? Pinmemberapferreira21-Jul-03 4:34 
GeneralI want a progress show during compression Pinmemberrichard_lee3-Jul-03 5:16 
QuestionNo on Linux? Pinmemberadityad13-Jun-03 10:31 
GeneralWill it work on Linux Pinmemberadityad12-Jun-03 11:18 
Generalcompression level PinmemberMighty T12-Jun-03 4:41 
GeneralRe: compression level PinmemberHans Dietrich12-Jun-03 8:48 

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
Web03 | 2.8.140709.1 | Last Updated 18 Jul 2007
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid