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(); }