zoukankan      html  css  js  c++  java
  • Qt中用QuaZip来压缩和解压缩文件

    1、简介

    QuaZIP是使用Qt,C++对ZLIB进行简单封装的用于压缩ZIP以及解压缩ZIP的开源库。如果你的Qt项目当中用到了压缩以及解压缩ZIP的话你可以考虑选择使用它。

    官方主页:http://quazip.sourceforge.net/

    souceforge下载地址:http://sourceforge.net/projects/quazip/

    2、编译

    QuaZip是基于Zlib库的,编译前要导入zlib的头文件,编译后会生成quazip.lib和quazip.dll文件(用的是vs)

    如果用Qt来编译,直接编译会报错,需要添加zlib.h的包含目录,在quazip/quazip.pro文件中添加INCLUDEPATH += "qt源码目录srcqtbasesrc3rdpartyzlib"

    同时将quazip.pro文件中的SUBDIRS = quazip qztest注释,换成SUBDIRS = quazip,选择“重新构建”会生成libquazipd.a和quazipd.dll

    3、应用

    在自己新建的工程中将quazip.lib所在库目录,头文件所在的目录添加到工程。

    用QuaZip中的JlCompress类来压缩和解压缩文件

    静态方法压缩文件

    static bool compressDir(QString fileCompressed, QString dir=QString(), bool recursive = true)

    第一个参数fileCompressed表示压缩后的文件

    第二个参数dir表示待压缩的目录

    第三个参数recursive表示是否递归

    解压缩用静态方法:extractDir

    static QStringList extractDir(QString fileCompressed, QString dir=QString())

    第一个参数fileCompressed表示待解压缩的文件

    第二个参数表示解压缩存放的目录

    下面给出程序代码例子:

    [cpp] view plain copy
     
    1. #include <QCoreApplication>  
    2. #include "JlCompress.h"  
    3.   
    4.   
    5.   
    6. int main(int argc, char *argv[])  
    7. {  
    8.     QCoreApplication a(argc, argv);  
    9.   
    10.     JlCompress::compressDir("d:\test.zip", "d:\test"); //压缩  
    11.     JlCompress::extractDir("d:\test.zip", "d:\test");//解压缩  
    12.     return a.exec();  
    13. }  

     
     
  • 相关阅读:
    Wannafly挑战赛13 C:zzf的好矩阵(思维)
    Wannafly挑战赛13 B:Jxc军训(逆元)
    TZOJ 1221 Tempter of the Bone(回溯+剪枝)
    AtCoder Regular Contest 092 C
    TZOJ 3030 Courses(二分图匹配)
    TOJ 2778 数据结构练习题――分油问题(广搜和哈希)
    PAT L3-001 凑零钱(01背包dp记录路径)
    [HNOI2009]通往城堡之路
    [HNOI2006]潘多拉的宝盒
    [bzoj4361]isn
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/7345541.html
Copyright © 2011-2022 走看看