Qt 之 ZIP开源库 QuaZIP

来源:互联网 发布:主板编程器使用方法 编辑:程序博客网 时间:2024/05/17 03:23

一。简介

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

二。编译

          QuaZIP目前支持以下几个平台:
  • linux-g++ (Ubuntu 11.10, Qt 4.7.4)
  • freebsd-g++ (Qt 4.0.0
  • hpux-acc (HP-UX 11.11)
  • hpux-g++ (HP-UX 11.11)
  • win32-g++ (MinGW)
  • win32-msvc2010 (MS VS 2010 Express, Qt 4.8.4)
  • win32-msvc2010 (Qt Creator, Qt 5.0.1)
  • some Symbian version, reportedly
      QuaZIP是基于ZLIB库的,所以编译之前必须编译完ZLIB或者导入ZLIB的LIB和头文件。

      默认QuaZIP是编译为DLL或者SO文件的。如果你想直接使用源代码的话你可以把所有导出类的标识符QUAZIP_EXPORT注释了,就可以编译通过
而不会生成DLL或者SO文件。
        下面你就可以按照你所使用的平台和方式编译QuaZIP。

三。使用

QuaZIP共有以下几个类
JlCompress典型操作工具类QuaAdler32Adler32 算法校验和QuaChecksum32校验和接口QuaCrc32CRC32 校验和QuaGzipFileGZIP 文件操作QuaZIODevice 压缩/解压 QIODeviceQuaZipZIP 文件QuaZipDirZIP文件内目录导航QuaZipFileZIP文件内的文件QuaZipFileInfoZIP压缩包内的文件信息QuaZipFilePrivateQuaZip的接口QuaZipNewInfo被创建的文件信息QuaZipPrivateQuaZIP内部类
下面给出一个自己用的解压缩ZIP的例子
[cpp] view plaincopyprint?
  1. bool Ziper::Extract(const QString& in_file_path, const QString& out_file_path)  
  2. {  
  3.     QuaZip archive(in_file_path);  
  4.     if (!archive.open(QuaZip::mdUnzip))  
  5.         return false;  
  6.       
  7.     QString path = out_file_path;  
  8.     if (!path.endsWith("/") && !out_file_path.endsWith("\\"))  
  9.         path += "/";  
  10.   
  11.     QDir dir(out_file_path);  
  12.     if (!dir.exists())  
  13.         dir.mkpath(out_file_path);  
  14.   
  15.     forbool f = archive.goToFirstFile(); f; f = archive.goToNextFile() )  
  16.     {  
  17.         QString filePath = archive.getCurrentFileName();  
  18.         QuaZipFile zFile(archive.getZipName(), filePath);  
  19.         zFile.open(QIODevice::ReadOnly );  
  20.         QByteArray ba = zFile.readAll();  
  21.         zFile.close();  
  22.   
  23.         if (filePath.endsWith("/"))  
  24.         {  
  25.             dir.mkpath(filePath);  
  26.         }  
  27.         else  
  28.         {  
  29.             QFile dstFile(path + filePath);  
  30.             if (!dstFile.open(QIODevice::WriteOnly))  
  31.                 return false;  
  32.             dstFile.write(ba);  
  33.             dstFile.close();  
  34.         }  
  35.     }  
  36.   
  37.     return true;  
  38. }  


0 0