zoukankan      html  css  js  c++  java
  • 文件的压缩与解压XZip,XUnzip

    参考http://www.codeproject.com/KB/cpp/xzipunzip.aspx

    CreateZip() –创建一个空的 zip 文件

    HZIP CreateZip(void *z, unsigned int len, DWORD flags);

    // Parameters: z - 压缩文件名
    // len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小; // 其他情况,这个值应当是 0 // flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME // // Returns: HZIP - 非 0 值为成功, 非则返回 0

    ZipAdd() 将一个文件添加进压缩文件

    // Parameters: hz - 创建或 打开的zip文件句柄
    // dstzn - 在zip文件中显示的文件名
    // src - 对于(ZIP_FILENAME)的情况,这是将要添加进zip文件的文件名
    // len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
    // 其他情况,这个值应当是 0
    // flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
    // // Returns: ZRESULT - 成功则返回 ZR_OK
    OpenZip() 打开一个已经存在的压缩文件
    // Parameters: z - 压缩文件名
    // len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
    // 其他情况,这个值应当是 0
    // flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
    // // Returns: HZIP - 非 0 值为成功, 非则返回 0

    GetZipItem() 得到压缩文件里面元素的信息

    // Parameters: hz - 创建或 打开的zip文件句柄
    // index - zip文件里的元素下标(从0开始)
    // ze - 一个指向ZIPENTRY 结构体的指针(ANSI) 或者 ZIPENTRYW (Unicode) // // Returns: ZRESULT - 成功则返回 ZR_OK

    FindZipItem() 在压缩文件里查找某元素

    // Parameters: hz - 创建或 打开的zip文件句柄
    // name - 你要在zip文件中查找的元素名字
    // ic - 值为 TRUE 不区分大小写
    // index - 返回要查找元素的下标,否则是-1 // ze - 返回指向ZIPENTRY 结构体的指针(ANSI) 或者 ZIPENTRYW (Unicode)
    // // Returns: ZRESULT - 成功则返回 ZR_OK

    UnzipItem() 解压压缩文件里的某元素

    // Parameters: hz - 创建或 打开的zip文件句柄
    // index - 要解压元素的下标
    // dst - 解压出来文件的名字
    // len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
    // 其他情况,这个值应当是 0
    // flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
    // // Returns: ZRESULT - 成功则返回 ZR_OK

    CloseZip() 关闭压缩文件

    // Parameters: hz - 要操作的zip文件句柄
    // // Returns: ZRESULT - 成功则返回 ZR_OK

     

    注意,不能一次压缩整个目录,可以相对路径来压缩目录


    void CZipDlg::OnOK()

    {

    // TODO: Add extra validation here

    //********** 压缩文件 ******************************

    HZIP hz = CreateZip("test.zip",0,ZIP_FILENAME);

    ZipAdd(hz,"NameInZip.txt", "ReadMe.txt",0,ZIP_FILENAME);

    ZipAdd(hz,"zip.h", "打包压缩文件zip.h",0,ZIP_FILENAME);

    CloseZip(hz);

    //********** 压缩目录和文件 **************************

    HZIP hz1 = CreateZip("test1.zip",0,ZIP_FILENAME);

    //空目录

    ZipAdd(hz1,"aa", "Debug",0,ZIP_FOLDER);//ZIP_FOLDER

    //有两个文件的目录

    ZipAdd(hz1,"res\NameInZip.txt", "ReadMe.txt",0,ZIP_FILENAME);

    ZipAdd(hz1,"res\zip.h", "打包压缩文件zip.h",0,ZIP_FILENAME);

    CloseZip(hz1);

    // CDialog::OnOK();

    }



    不使用任何lib和dll,压缩/解压缩文件(zip格式)   


    发布者:   soarlove   ——> 查看soarlove在VCCode发布的所有文章     文章类型:翻译     
    发布日期:2003.05.18         
    升级次数:0     
    今日浏览:2     
    总浏览:502     

    -------------------------------------------------------------------------------- 
      
    评价等级:               
      代码下载     
    2位用户为此文章评分,平均分为5.0   


    作者:Hans   Dietrich     译自:CodeProject 

    摘要 
    这篇文章介绍XZip和XUnzip两个文件,功能是不需要在你的工程中加入任何的.lib或.dll文件,完成压缩和解压缩的功能(zip格式)。 

    首先,让我感谢Lucian   Wischik的工作,他把从Info-ZIP得到的很多.c和.h文件转化对应的.cpp和.h文件,这些文件是XZip的基础。 

    XZip和XUnzip的功能 
    大多数的函数在XZip演示工程中都有使用演示。这里列出其中重要的一些函数:   

    CreateZip()   -   创建zip档案文件   ///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   向zip档案中添加一个文件   ///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   打开一个已存在的zip档案文件   ///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   得到打开的zip档案文件中的相关条目的信息   ///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   通过名称查找条目并返回相关信息///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   通过索引找到相关条目并对其解压缩///////////////////////////////////////////////////////////////// 
    // 
    //   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()   -   关闭已存在的档案条目/////////////////////////////////////////////////////////////////
    // 
    //   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 
    // 
    使用方法 
    为了在你的工程中使用XZip,首先你需要在你的工程中添加以下文件:   

    XZip.cpp   
    XZip.h   
    XUnzip.cpp   
    XUnzip.h   
    如果你在已包含XZip的工程中使用了precompiled   headers,那么你必须改变C/C++   Precompiled   Headers的设置,使用Not   using   precompiled   headers。(见常见问题2,soarlove注) 

    接下来,在工程的适当文件中加入XZip.h和XUnzip.h声明。现在,你已经做好了使用XZip准备。在使用中有许多的注意事项,在使用函数时,请仔细查看相关函数中的说明。 

    演示程序 
    XZipTest.exe演示程序演示了在程序中使用XZip和XUnzip中API的方法。比如: 

                      

    常见问题 
    我可以在非MFC工程中使用XZip吗?   可以。这写函数可以在任何的Win32程序中使用。   
    当我尝试把XZip.cpp文件引入我的MFC工程时,在XZip.cpp(2918)得到一个编译错误:   fatal   error   C1010:   unexpected   end   of   file   while   looking   for   precompiled   header   directive。怎样修复?   当你在工程中使用precompiled   headers时,   那么你必须改变C/C++   Precompiled   Headers的设置,使用Not   using   precompiled   headers。如下图: 
                      

    当我编译演示代码时,得到如下错误error       LINK   :   fatal   error   LNK1104:   cannot   open   file   "mfc42u.lib "   Error   executing   link.exe。怎样修复?   Visual   C++   v6.0在默认安装中没有安装Unicode库文件,所以产生了找不到mfc42u.lib或mfc42ud.lib的错误。修复方法,1.你可以使用VC++安装CD安装Unicode库文件。2.在Build   |   Set   Active   Configuration中选择一个非Unicode编译配置。   
                      

    我不需要使用Zip/Unzip函数,是否可以排斥(exclude)XZip.cpp/XUnzip.cpp文件?   可以,你只需要在你的工程中引入include你需要部分的.h/.cpp文件   
    我可以在我们的共享/商业软件中使用XZip吗?   可以,你可以使用XZip,不需要支付任何的费用,但是你需要遵守在XZip.cpp文件中的关于使用Info-ZIP的限制。 
  • 相关阅读:
    C/C++定义全局变量/常量几种方法的区别
    可变参数宏__VA_ARGS__
    mysql 命令重命名表RENAME TABLE 句法
    贝尔实验室的历史
    SVN代码回滚
    linux下查看进程占用端口和端口占用进程命令
    php操作mongodb中的ISODate格式日期
    Vim多行缩进技巧
    关于XCode工程中PrefixHead.pch文件的使用
    Object C函数指针@selector
  • 原文地址:https://www.cnblogs.com/redrainblog/p/4209722.html
Copyright © 2011-2022 走看看