1.下载zlib,因为quazip依赖zlib
1.官网下载zlib1.3.1源码(需要编译)
2.用vs编译zlib源码(cmake),编译成功后得到lib和include文件
2.安装 QuaZip,需要执行以下步骤:
- 获取源代码:
-
- 您可以从 QuaZip 的 GitHub 页面下载最新的源代码压缩包。
- 或者,您可以使用 Git 克隆 QuaZip 的存储库:git clone https://github.com/stachenov/quazip.git
- 安装必要的依赖项:
-
- 您需要确保系统上安装了 zlib 库和 Qt 4/5/6。如果您使用的是 Linux 系统,则可能需要安装相应的开发包以获取 Qt 和 zlib 的头文件和其他必要文件。
- 使用 CMake 构建项目:
-
- 进入 QuaZip 源代码目录。
- 创建一个新的构建目录,例如在源代码目录外部创建一个名为 build 的目录。
- 在终端中,进入新创建的构建目录。
- 运行 CMake 来配置项目:
cmake -S /path/to/quazip/source -B /path/to/build/directory -DQUAZIP_QT_MAJOR_VERSION=6/5/4
-
-
- -S 指定源代码目录。
- -B 指定构建目录。
- -DQUAZIP_QT_MAJOR_VERSION 指定 Qt 的主要版本号,可以是 6、5 或 4,根据您系统上已安装的 Qt 版本选择相应的版本号。
-
- 构建项目:
-
- 在终端中,进入到构建目录。
- 运行构建命令:
cmake --build /path/to/build/directory
- 安装 QuaZip:
-
- 如果您想要将 QuaZip 安装到系统目录中,则可以运行以下命令:
cmake --build /path/to/build/directory --target install -DCMAKE_INSTALL_PREFIX=/installation/directory
-
-
- -DCMAKE_INSTALL_PREFIX 指定要安装的目标目录的路径。
-
-
- 如果您只想在当前目录中安装 QuaZip,则不需要指定 -DCMAKE_INSTALL_PREFIX。
- (默认安装到c盘,需要管理员权限安装)
3.在Qt调用Quazip
$$PWD为该项目.pro文件地址
# 指定 QuaZip 头文件路径
INCLUDEPATH += $$PWD/QUAZIP/include/QuaZip-Qt6-1.4/quazip
INCLUDEPATH += $$PWD/zlib/include
# 指定 QuaZip 库文件路径和名称
LIBS += -L$$PWD/QUAZIP/lib \
-lquazip1-qt6d
#include "quazip.h"
#include "quazipfile.h"
// 备份函数
bool userview::compressFile(const QString &sourcePath, const QString &targetPath)
{
QuaZip zip(targetPath);
if (!zip.open(QuaZip::mdCreate))
{
qDebug() << "无法创建压缩文件:" << targetPath;
return false;
}
std::function<bool(const QString &, const QString &)> addFiles;
addFiles = [&](const QString &basePath, const QString &relativePath) -> bool
{
QDir dir(basePath + "/" + relativePath);
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &fileInfo, fileInfoList)
{
QString filePath = relativePath + "/" + fileInfo.fileName();
if (fileInfo.isDir())
{
if (!addFiles(basePath, filePath))
{
return false;
}
}
else
{
QuaZipFile outFile(&zip);
if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(filePath, basePath + "/" + filePath)))
{
qDebug() << "无法写入文件到压缩包:" << filePath;
return false;
}
QFile inFile(fileInfo.filePath());
if (!inFile.open(QIODevice::ReadOnly))
{
qDebug() << "无法打开源文件:" << filePath;
return false;
}
if (outFile.write(inFile.readAll()) == -1)
{
qDebug() << "写入文件到压缩包失败:" << filePath;
inFile.close();
return false;
}
inFile.close();
outFile.close();
}
}
return true;
};
if (!addFiles(sourcePath, ""))
{
zip.close();
return false;
}
zip.close();
qDebug() << "文件已压缩到目标目录:" << targetPath;
QString nowtime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
ui->plainTextEdit->appendPlainText(nowtime +"【INFO】:文件已备份到目标目录:"+targetPath);
return true;
}
// 备份按钮
void userview::on_actionbeif_triggered()
{
QDir current = QDir::currentPath();
QString currentpath = current.path();
QString nowtime = QDateTime::currentDateTime().toString("yyyy-MM-dd--hh-mm-ss");
qDebug()<<currentpath;
QString sourcePath = currentpath+"/info/log";
QString targetPath = currentpath+"/info/backup/"+nowtime+".zip";
if (compressFile(sourcePath, targetPath))
qDebug() << "压缩成功。";
else
qDebug() << "压缩文件失败。";
}