![]() |
Languages »
C / C++ Language »
General
Intermediate
License: The Code Project Open License (CPOL)
XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dllBy Hans DietrichXZip and XUnzip provide non-MFC functions to create a zip, add files to it, and extract files from it - all in two .cpp files |
VC6, VC7Win2K, WinXP, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
Most of the functions are demonstrated in the XZip demo app. Here are the main ones:
////////////////////////////////////////////////////////////////////////////// // // 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() // // 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() // // 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() // // 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() // // 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() // // 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() // // Purpose: Close an open zip archive // // Parameters: hz - handle to an open zip archive // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
To integrate XZip into your app, you first need to add following the files to your project:
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.
The XZipTest.exe demo tests the APIs in XZip and XUnzip. Here is some of the output:
XZip.cpp(2918) : fatal error C1010: unexpected end of file while looking for precompiled header directive. How can I fix this?
LINK : fatal error LNK1104: cannot open file "mfc42u.lib" Error executing link.exe. How can I fix this?
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.
AddFolderContent() contributed by Renaud Deysine. TUnzip::Open() reported by Pete Howells. Open() now returns correct success code. unzReadCurrentFile() reported by Kochise. EnsureDirectory() reported by craigmj. ideflate() suggested by Michael B. Hansen. time_t reported by Ronney. PUTSHORT and PUTBYTE macros and to TZip::write(), suggested by vielheit. 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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Jul 2007 Editor: Sean Ewington |
Copyright 2003 by Hans Dietrich Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |