Qt压缩与解压:自带zip压缩QZipReader和QZipWriter
作者:互联网
使用Qt自带类QZipReader和QZipWriter,不用再自行编译zlib、quazip,亲测简单快捷,使用方法如下:
1.在pro文件添加 QT += gui-private
2.添加下面两个头文件
#include "QtGui/private/qzipreader_p.h" #include "QtGui/private/qzipwriter_p.h"
3. zip包解压缩
//! \brief zipReader zip包解压缩 //! \param zipPath 压缩包路径 //! \param zipDir 解压缩目录 //! \return 是否解压成功 bool zipReader(QString zipPath="test.zip", QString zipDir="") { QDir tempDir; if(!tempDir.exists(zipDir)) tempDir.mkpath(zipDir); QZipReader reader(zipPath); return reader.extractAll(zipDir); }
4. 压缩zip包
//! \brief zipWriter 压缩zip包 //! \param zipPath 压缩包路径 //! \param fileList 需要压缩的文件列表 void zipWriter(QString zipPath="test.zip", QFileInfoList fileList=QFileInfoList()) { QZipWriter *writer = new QZipWriter(zipPath); // 遍历文件列表 foreach (QFileInfo fileInfo, fileList) { //添加需要压缩的文件 QFile file(fileInfo.filePath()); if (!file.exists()) continue; file.open(QIODevice::ReadOnly); writer->addFile(fileInfo.fileName(), file.readAll()); file.close(); } writer->close(); if(writer) { delete writer; writer = nullptr; } }
标签:Qt,zip,压缩,writer,zipDir,param,zipPath,file 来源: https://www.cnblogs.com/tingtaishou/p/16549452.html