原文:http://www.codeproject.com/Articles/4135/XZip-and-XUnzip-Add-zip-and-or-unzip-to-your-app-w

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

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:

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 Headerssettings to Not using precompiled headers for XZip.cpp and XUnzip.cpp. Be sure to do this for All Configurations.

  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.

    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 inXZip.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)

【转】XZip and XUnzip - Add zip and/or unzip to your app with no extra .lib or .dll的更多相关文章

  1. Linux中zip压缩和unzip解压缩命令详解

    文章转自:http://www.jb51.net/LINUXjishu/105916.html 1.把/home目录下面的mydata目录压缩为mydata.zipzip -r mydata.zip ...

  2. 每天一个linux命令(63):Linux中zip压缩和unzip解压缩命令详解

    文章转自:http://www.jb51.net/LINUXjishu/105916.html 1.把/home目录下面的mydata目录压缩为mydata.zipzip -r mydata.zip ...

  3. CentOS中zip压缩和unzip解压缩命令详解

    以下命令均在/home目录下操作cd /home #进入/home目录1.把/home目录下面的mydata目录压缩为mydata.zipzip -r mydata.zip mydata #压缩myd ...

  4. CentOS7中zip压缩和unzip解压缩命令详解

    安装zip.unzip应用 yum install zip unzip 以下命令均在/home目录下操作cd /home #进入/home目录1.把/home目录下面的mydata目录压缩为mydat ...

  5. 使用gunzip、tar、rar、(zip压缩和unzip解压缩)

    ---------------------20171119------------------------------ 解压gz后缀 使用gunzip filename.gz ------------ ...

  6. CentOS Linux中zip压缩和unzip解压缩命令详解

    以下命令均在/home目录下操作cd /home  #进入/home目录1.把/home目录下面的mydata目录压缩为mydata.zip     zip -r  mydata.zip   myda ...

  7. zip压缩工具,unzip解压缩工具

    zip压缩工具,unzip解压缩工具=================== [root@aminglinux tmp]# yum install -y zip[root@aminglinux tmp] ...

  8. zip 压缩文件 unzip查看zip压缩包内的内容

    [root@GitLab tmp]# zip -r new.zip ./*  adding: gitlab_key_file20161001-2668-1eu44mv (deflated 15%)  ...

  9. Centos中压缩(zip)和解压(unzip)命令

    摘自:http://liuzhichao.com/p/681.html 1.我下载了一个yasuo.zip文件,想解压缩: # unzip yasuo.zip 2.我当前目录下有abc1.zip,ab ...

随机推荐

  1. 标准webservice调用

    现代定义的webservice一般都倾向于restfull风格的http请求,但工作中还是会遇到前辈们写的时代代码. 我们更倾向于封装代码来调用,而不是服务引用.请看: Service.asmx服务的 ...

  2. vue中echarts引入中国地图

    <div id="myChartChina" :style="{width: '100%', height: '500px'}"></div& ...

  3. golang学习之win7下go web之revel安装

    接着上回记录的win7下go环境搭建,go的开发,现在除了sublime外,LiteIDE比较推荐,下载链接 下载安装后直接打开,需要配置下go环境(本机使用的是window 386版本),如下: 打 ...

  4. isPrototypeOf、instanceof、hasOwnProperty函数整理

    isPrototypeOf 作用:检测一个对象是否是另一个对象的原型.或者说一个对象是否被包含在另一个对象的原型链中 var p = {x:1};//定义一个原型对象 var o = Object.c ...

  5. 自己实现async和await

    无意当中看了一些博文,说有人想自己尝试实现基于异步操作的方法: 1)直接使用Task(不说咯,这个是微软给我们的标准实现方法). 2)必须继承INotifyCompletion接口,同时自己实现IsC ...

  6. Linux 文件系统大小调整

    有些使用需要进行文件系统的大小调整,比如使用LVM,或者在loopback设备上建立文件系统等,但该文件系统不是根文件系统时可以通过一下步骤,简单的进行: e2fsck -f /dev/loop0 r ...

  7. 获取页面z-index最大值

    getMaxZIndex = function () { var maxZ = Math.max.apply(null, $.map($('body *'), function(e,n) { if ( ...

  8. img底部空白以及多余的空白文本节点解决方案

    1:img底部有空白的问题 img的css属性display的默认值是inline,这样会导致img的vertical-align的默认值是 baseline; baseline又不是bottom,只 ...

  9. org.apache.ibatis.binding.BindingException

    1.异常提示: org.apache.ibatis.binding.BindingException: Mapper method 'com.artup.dao.WorksDao.selectWork ...

  10. timestamp to time 时间戳转日期

    function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);   //timestamp 为10位需*100 ...