Qt使用QuaZip对文件进行压缩解压操作

来源:互联网 发布:linux禁用用户怎么做 编辑:程序博客网 时间:2024/05/17 01:51


       八月份快要过去了,突然想到我还没有写一篇日志呢,想一想这一个月我并没有做开发以外的事情,三十天来还是尝试并且解决了不少技术上的问题的。所以这次我打算将其中一些作为日志分享出来。

       前几天正在讨论使用打包工具的问题,待选的方案是7z和zip。于是拿了QLib7z、Qt7z还有QuaZip来进行测试,后面发现,使用QuaZip这个方案实在是很方便,于是就将使用QuaZip的过程记录一下。大家可能看过别人有关介绍QuaZip的博客,我这篇没有参考其它人的写法,完全是来自官方测试项目。

下载

       QuaZip项目的地址来自sourceforge.Net。大家可以点击这个地址获取下载链接。

书写pri文件

       下载完毕之后,我们决定采用其源码的方式,而不是编译成一个静态的库。因此,我们新建一个qmake项目,这个项目包含描述QuaZip项目构建情况的文件,它的内容是:

[cpp] view plain copy
  1. # QuaZip.pri    
  2.      
  3. QUAZIP_INCLUDE_PATH= E:\QtReference\quazip-0.7.1    
  4.      
  5. INCLUDEPATH+= $$QUAZIP_INCLUDE_PATH/quazip    
  6. DEFINES+= QUAZIP_BUILD    
  7. LIBS+= -lz    
  8.      
  9. HEADERS+= \    
  10.         $$QUAZIP_INCLUDE_PATH/quazip/crypt.h \    
  11.         $$QUAZIP_INCLUDE_PATH/quazip/ioapi.h \    
  12.        $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.h \    
  13.        $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.h \    
  14.        $$QUAZIP_INCLUDE_PATH/quazip/quachecksum32.h \    
  15.         $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.h\    
  16.        $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.h \    
  17.        $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.h \    
  18.        $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.h \    
  19.        $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.h \    
  20.         $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.h\    
  21.        $$QUAZIP_INCLUDE_PATH/quazip/quazip_global.h \    
  22.         $$QUAZIP_INCLUDE_PATH/quazip/quazip.h \    
  23.        $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.h \    
  24.         $$QUAZIP_INCLUDE_PATH/quazip/unzip.h \    
  25.         $$QUAZIP_INCLUDE_PATH/quazip/zip.h    
  26.      
  27. SOURCES+= $$QUAZIP_INCLUDE_PATH/quazip/qioapi.cpp \    
  28.           $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.cpp \    
  29.           $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.cpp \    
  30.           $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.cpp \    
  31.           $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.cpp \    
  32.           $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.cpp \    
  33.           $$QUAZIP_INCLUDE_PATH/quazip/quazip.cpp \    
  34.           $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.cpp \    
  35.            $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.cpp\    
  36.           $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.cpp \    
  37.           $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.cpp \    
  38.            $$QUAZIP_INCLUDE_PATH/quazip/unzip.c\    
  39.            $$QUAZIP_INCLUDE_PATH/quazip/zip.c   

使用的时候只需要更改QUAZIP_INCLUDE_PATH变量的值就好了。然后再项目pro文件中添加这样一行:

[cpp] view plain copy
  1. include( QuaZip.pri )    

压缩操作

       使用QuaZip也是非常非常的简单。它的一个特点是将Zip内文件的读写操作封装成QIODevice的一个子类,这样可以使用我们常用的文件读写方法来对其操作了。

       下面是一个简单的将一个字符串数组写入压缩文件的代码:

[cpp] view plain copy
  1. const QString& zipName( "E:/Archive.zip" );    
  2. QuaZip zip( zipName );    
  3.     
  4. if ( !zip.open( QuaZip::mdCreate ) )    
  5. {    
  6.     qDebug( "Could not create zip: %s", qPrintable( zipName ) );    
  7.     return;    
  8. }    
  9.     
  10. QStringList data;    
  11. data.append( "AAaaAAaa" );    
  12. data.append( "BBbbBBbb" );    
  13. data.append( "CCccCCcc" );    
  14. data.append( "DDddDDdd" );    
  15.     
  16. foreach ( const QString& str, data )    
  17. {    
  18.     const QString& innerName = str;    
  19.     
  20.     QuaZipNewInfo newInfo( innerName );    
  21.     
  22.     QuaZipFile file( &zip );    
  23.     bool ret = file.open( QIODevice::WriteOnly,    
  24.                           newInfo,      // QuaZipNewInfo结构体引用    
  25.                           Q_NULLPTR,    // 密码    
  26.                           0,            // CRC值(默认值是0)    
  27.                           8 );          // 写入方法(0为文件夹,8为普通文件)    
  28.     if ( !ret ) continue;    
  29.     
  30.     // 开始写入文件的数据了    
  31.     file.write( str.toUtf8( ) );    
  32.     file.close( );    
  33. }    
  34.     
  35. zip.close( );    

解压操作

       解压操作和压缩操作相反,也是先构建QuaZip再构建QuaZipFile,当然也可以一步到位,直接使用QuaZipFile进行解压。下面的代码目的是从“Archive.zip”文件中解压“聊天.txt”文本文件,解压密码是“63636361”。

[cpp] view plain copy
  1. const QString& zipName( "E:/Archive.zip" );    
  2. const QString& fileName( "聊天.txt" );    
  3.         
  4. QuaZipFile file( zipName, fileName );    
  5. if ( file.open( QIODevice::ReadOnly,    // 打开模式    
  6.                         Q_NULLPTR,              // 压缩方法    
  7.                         Q_NULLPTR,              // 压缩等级    
  8.                         false,                  // 是否是保留原始文件?    
  9.                         "63636361" ) )          // 压缩密码    
  10. {    
  11.        ui->unzippedEdit->setPlainText( file.readAll( ) );    
  12.        file.close( );    
  13. }    
  14. else    
  15. {    
  16.        ui->unzippedEdit->setPlainText( "无法打开:" + zipName + '/' + fileName );    
  17. }    


原文链接:http://blog.csdn.net/gamesdev/article/details/48136687

原创粉丝点击