Qt之Quazip的編譯與使用(壓縮檔案)
阿新 • • 發佈:2018-12-14
1.QuaZip的的編譯依賴於zlib庫 Zlib庫下載地址: Zlib庫的下載地址 QuaZip的下載地址 QuaZipde 下載地址 編譯器: mingw32 Qt版本:Qt5.6.1; 本文下載的zlib庫的版本是:zlib-1.2.11 2.Zlib的編譯步驟: (1)解壓zlib庫; (2)啟動Qt5.6 for DeskTop(MinGW4.9.2) (3)切換當前目錄為zlib庫 cd zlib-1.2.11 (4)命令列輸入一下命令 copy win32\makefile.gcc makefile.gcc mingw32-make -f makefile.gcc 在當前目錄下可以看到生成的庫檔案:libz.a、libz.dll.a、zlib1.dll 需要用到標頭檔案:zconf.h、zlib.h 3.QuaZip編譯 (1)新增標頭檔案(上面提到的標頭檔案) (2)新增庫檔案 LIBS +=./debug/libz.a ./debug/libz.dll.a ./debug/lzlib1.dll 注意:新增的路徑一定要正確,並且有以上提到的三個庫檔案; 4.執行 在debug的目錄中就可以找到quazipd.dll動態庫 這種事debug模式下的除錯,如果要使用release模式下的庫,方法一樣; 5.Quazip庫的使用 QuaZip庫可以試想檔案的解壓縮功能,在這裡我只是使用到檔案的壓縮功能; 以下程式碼是檔案和檔案的壓縮功能 標頭檔案:
#ifndef ZIPDIR_H #define ZIPDIR_H #include "./include/JlCompress.h" #include <QThread> #include <QTime> class ZipDir : public QThread { Q_OBJECT public: explicit ZipDir(QObject *parent = 0); ~ZipDir(); virtual void run(); void setPath(QString systemfile,QString datafile,QString errorfile); public: QString systemPath; QString dataPath; QString errorPath; QTime tempTime; bool compressDir(QString path); bool removeDir(QString path); }; #endif // ZIPDIR_H
.cpp檔案
#include "zipdir.h" #include <QThread> #include <QDateTime> #include <QDebug> ZipDir::ZipDir(QObject *parent) : QThread(parent) { systemPath=""; dataPath=""; errorPath=""; tempTime=QTime(11,33,0); } ZipDir::~ZipDir() { } void ZipDir::run() { while(1){ QDateTime currTime=QDateTime::currentDateTime(); qDebug()<<currTime.time().secsTo(tempTime); if(currTime.time().secsTo(tempTime)<300 && currTime.time().secsTo(tempTime)>0 ){ if(systemPath!=""){ compressDir(systemPath); } if(dataPath !=""){ compressDir(dataPath); } if(errorPath !=""){ compressDir(errorPath); } } } this->exec(); } void ZipDir::setPath(QString systemfile,QString datafile,QString errorfile) { systemPath=systemfile; dataPath=datafile; errorPath=errorfile; } bool ZipDir::compressDir(QString path) { QDir dir(path); if(!dir.exists()){ qDebug()<<"error"; return false; } //系統日誌和資料日誌 dir.setFilter(QDir::Dirs|QDir::NoDotAndDotDot); QFileInfoList list=dir.entryInfoList(); qDebug()<<list.count(); for (int i=0;i<list.count();i++){ QFileInfo info=list.at(i); qDebug()<<info.fileName(); QString filename=info.fileName(); QString file=path+"/"+filename; qDebug()<<file; QString compressed=file+".zip"; JlCompress::compressDir(compressed,file); if(info.isDir()){ removeDir(file); } } //錯誤日誌 if(list.count()<=0){ QDir dir2(path); dir2.setFilter(QDir::Files); QStringList namefilter; namefilter<<"*.txt"; dir2.setNameFilters(namefilter); QFileInfoList listerror=dir2.entryInfoList(); qDebug()<<listerror.count(); QString compressFiles=path+"/"+ QDateTime::currentDateTime().toString("yyyy_MM_dd") + ".zip"; QStringList fileList; fileList.clear(); for(int i=0;i<listerror.count();i++){ QFileInfo infoerr=listerror.at(i); QString fileErrName=infoerr.fileName(); QString filePath=path+"/"+fileErrName; qDebug()<<filePath; fileList<<filePath; } if(listerror.count()!=0){ JlCompress::compressFiles(compressFiles,fileList); } //刪除錯誤日誌檔案 for(int i=0;i<listerror.count();i++){ QFileInfo infoerr=listerror.at(i); QFile tempFile(infoerr.filePath()); tempFile.remove(); } } return true; } bool ZipDir::removeDir(QString path) { QDir dir(path); QFileInfoList fileList; QFileInfo curFile; if(!dir.exists()){ qDebug()<<"error"; return false; } dir.setFilter(QDir::Files); fileList=dir.entryInfoList(); for(int i=0;i<fileList.count();i++){ curFile=fileList.at(i); if(curFile.isFile()){ QFile tempFile(curFile.filePath()); tempFile.remove(); } } dir.rmdir("."); return true; }
main.cpp
#include <QCoreApplication>
#include "./include/JlCompress.h"
#include <QDebug>
#include <QDateTime>
#include "zipdir.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ZipDir dir;
QString str1="E:/Work/build-ServerPro-Debug/debug/system";
QString str2="E:/Work/build-ServerPro-Debug/debug/datalog";
QString str3="E:/Work/build-ServerPro-Debug/debug/errlog";
dir.setPath(str1,str2,str3);
dir.start();
return a.exec();
}