Qt使用QuaZip对文件进行压缩解压操作
来源:互联网 发布:linux禁用用户怎么做 编辑:程序博客网 时间:2024/05/17 01:51
八月份快要过去了,突然想到我还没有写一篇日志呢,想一想这一个月我并没有做开发以外的事情,三十天来还是尝试并且解决了不少技术上的问题的。所以这次我打算将其中一些作为日志分享出来。
前几天正在讨论使用打包工具的问题,待选的方案是7z和zip。于是拿了QLib7z、Qt7z还有QuaZip来进行测试,后面发现,使用QuaZip这个方案实在是很方便,于是就将使用QuaZip的过程记录一下。大家可能看过别人有关介绍QuaZip的博客,我这篇没有参考其它人的写法,完全是来自官方测试项目。
下载
QuaZip项目的地址来自sourceforge.Net。大家可以点击这个地址获取下载链接。
书写pri文件
下载完毕之后,我们决定采用其源码的方式,而不是编译成一个静态的库。因此,我们新建一个qmake项目,这个项目包含描述QuaZip项目构建情况的文件,它的内容是:
- # QuaZip.pri
-
- QUAZIP_INCLUDE_PATH= E:\QtReference\quazip-0.7.1
-
- INCLUDEPATH+= $$QUAZIP_INCLUDE_PATH/quazip
- DEFINES+= QUAZIP_BUILD
- LIBS+= -lz
-
- HEADERS+= \
- $$QUAZIP_INCLUDE_PATH/quazip/crypt.h \
- $$QUAZIP_INCLUDE_PATH/quazip/ioapi.h \
- $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quachecksum32.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.h\
- $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.h\
- $$QUAZIP_INCLUDE_PATH/quazip/quazip_global.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quazip.h \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.h \
- $$QUAZIP_INCLUDE_PATH/quazip/unzip.h \
- $$QUAZIP_INCLUDE_PATH/quazip/zip.h
-
- SOURCES+= $$QUAZIP_INCLUDE_PATH/quazip/qioapi.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quazip.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.cpp\
- $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.cpp \
- $$QUAZIP_INCLUDE_PATH/quazip/unzip.c\
- $$QUAZIP_INCLUDE_PATH/quazip/zip.c
使用的时候只需要更改QUAZIP_INCLUDE_PATH变量的值就好了。然后再项目pro文件中添加这样一行:
压缩操作
使用QuaZip也是非常非常的简单。它的一个特点是将Zip内文件的读写操作封装成QIODevice的一个子类,这样可以使用我们常用的文件读写方法来对其操作了。
下面是一个简单的将一个字符串数组写入压缩文件的代码:
- const QString& zipName( "E:/Archive.zip" );
- QuaZip zip( zipName );
-
- if ( !zip.open( QuaZip::mdCreate ) )
- {
- qDebug( "Could not create zip: %s", qPrintable( zipName ) );
- return;
- }
-
- QStringList data;
- data.append( "AAaaAAaa" );
- data.append( "BBbbBBbb" );
- data.append( "CCccCCcc" );
- data.append( "DDddDDdd" );
-
- foreach ( const QString& str, data )
- {
- const QString& innerName = str;
-
- QuaZipNewInfo newInfo( innerName );
-
- QuaZipFile file( &zip );
- bool ret = file.open( QIODevice::WriteOnly,
- newInfo,
- Q_NULLPTR,
- 0,
- 8 );
- if ( !ret ) continue;
-
-
- file.write( str.toUtf8( ) );
- file.close( );
- }
-
- zip.close( );
解压操作
解压操作和压缩操作相反,也是先构建QuaZip再构建QuaZipFile,当然也可以一步到位,直接使用QuaZipFile进行解压。下面的代码目的是从“Archive.zip”文件中解压“聊天.txt”文本文件,解压密码是“63636361”。
- const QString& zipName( "E:/Archive.zip" );
- const QString& fileName( "聊天.txt" );
-
- QuaZipFile file( zipName, fileName );
- if ( file.open( QIODevice::ReadOnly,
- Q_NULLPTR,
- Q_NULLPTR,
- false,
- "63636361" ) )
- {
- ui->unzippedEdit->setPlainText( file.readAll( ) );
- file.close( );
- }
- else
- {
- ui->unzippedEdit->setPlainText( "无法打开:" + zipName + '/' + fileName );
- }
原文链接:http://blog.csdn.net/gamesdev/article/details/48136687