【转】XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dll
2013-12-18 10:58
627 查看
原文:http://www.codeproject.com/Articles/4135/XZip-and-XUnzip-Add-zip-and-or-unzip-to-your-app-w
Download source files - 154.3 KB
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.
CreateZip() - Create a zip archive file.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
ZipAdd() - Add a file to a zip archive.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
OpenZip() - Open an existing zip archive file.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
GetZipItem() - Get information about an item in an open zip archive.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
FindZipItem() - Find item by name and return information about it.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
UnzipItem() - Find item by index and unzip it.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
CloseZip() - Close an open zip archive.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
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 Headerssettings 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.
![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip1.png)
Yes. It has been implemented to compile with any Win32 program.
When I try to include XZip.cpp in my MFC project, I get the compiler error
When using XZip in project that uses precompiled headers, you must change C/C++ Precompiled Headerssettings to Not using precompiled headers for XZip.cpp and XUnzip.cpp. Be sure to do this for All Configurations.
![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip2.png)
When I try to build the demo app, I get the linker error
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.
![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip3.png)
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.
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.
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 inXZip.cpp.
Does XZip handle pipes? in-memory zipping?
XZip has not been tested with anything other than files.
Can I use XZip in a VS2005 project?
Yes. There is a sample VS2005 project included in the download.
Does XZip work on Vista?
Yes.
The CXListBox class is taken from my own article, XListBox - Owner-draw CListBox with selectable text and background colors.
Fixed XZip to save file time in local time, suggested by Damir Valiulin.
Added
Fixed problem with
Fixed several bugs reported by Warren Stevens.
Fixed a problem in
Fixed bug in
Changed
Fixed problem with
Fixed problems found by Boundschecker as reported by Warren Stevens.
Made changes to
Download source files - 154.3 KB
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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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.
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy Code
////////////////////////////////////////////////////////////////////////////// //// 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 Headerssettings 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:![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip1.png)
Frequently Asked Questions
Can I use XZip in non-MFC apps?Yes. It has been implemented to compile with any Win32 program.
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 Headerssettings to Not using precompiled headers for XZip.cpp and XUnzip.cpp. Be sure to do this for All Configurations.
![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip2.png)
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.
![](http://www.codeproject.com/KB/cpp/xzipunzip/XZip3.png)
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.
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.
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 inXZip.cpp.
Does XZip handle pipes? in-memory zipping?
XZip has not been tested with anything other than files.
Can I use XZip in a VS2005 project?
Yes. There is a sample VS2005 project included in the download.
Does XZip work on Vista?
Yes.
Acknowledgments
The code in XZip.cpp and XUnzip.cpp is included with the kind permission of Lucian Wischik. It is based on code from Info-ZIP. Please see XZip.cpp for Info-ZIP's copyright and restrictions.The CXListBox class is taken from my own article, XListBox - Owner-draw CListBox with selectable text and background colors.
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_treported by Ronney.
Fixed problems found by Boundschecker as reported by Warren Stevens.
Made changes to
PUTSHORTand
PUTBYTEmacros and to
TZip::write(), suggested by vielheit.
Version 1.1 - 2003 May 7
Initial public releaseUsage
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)相关文章推荐
- Your app, extension, and/or linked framework appears to contain code designed explicitly with the ca
- eclipse报 No XML content. Please add a root view or layout to your document. 错误的解决办法
- PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath s
- Add Languages to Your Xamarin Apps with Multilingual App Toolkit
- How To Save Your App Data With NSCoding and NSFileManager
- How to Add Route to TCP/IP Routing Table With Windows Routing And Remote Access Console or DOS Promp
- [Resolved] TF254006: No usable accounts are present in your configuration database. Use TfsConfig accounts /add to add an account and return to this wizard.
- AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set
- his bundle is invalid . new apps and app updates submitted to the app store must be built with publi
- How to get started with WebRTC and iOS without wasting 10 hours of your life
- Android--You need to use a Theme.AppCompat theme (or descendant) with this activity解决方案
- 【Android】You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- libtest.a: could not read symbols: Archive has no index; run ranlib to add one的原因和解决方案
- system call filters failed to install; check the logs and fix your configuration or disable system c
- 信鸽errCode:10004,msg:please add libTpnsSecurity.so to your project
- You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- 10. You need to use a Theme.AppCompat theme (or descendant) with this activity
- Networking with Microsoft Windows Vista: Your Guide to Easy and, Secure Windows Vista Networking (Paperback) Dec.2007.eBook-BBL
- 【摘要】Advanced Serialization: Format Your Way to Success with the .NET Framework Versions 1.1 and 2.0
- Android问题集锦之二十八:You need to use a Theme.AppCompat theme (or descendant) with this activity.