windows qt 将文件夹压缩成zip文件并以当前日期命名
要在Windows上使用Qt将文件夹压缩成zip文件并以当前日期命名,可以使用QProcess类来调用系统命令。以下是一个示例代码:
#include <QCoreApplication>
#include <QDateTime>
#include <QProcess>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString folderPath = "C:/path/to/folder"; // 替换为要压缩的文件夹路径
// 获取当前日期和时间
QDateTime currentDateTime = QDateTime::currentDateTime();
QString zipFileName = currentDateTime.toString("yyyyMMdd") + ".zip";
// 构建系统命令
#ifdef Q_OS_WIN
QString command = "powershell -Command \"Compress-Archive -Path '" + folderPath + "' -DestinationPath '" + zipFileName + "'\"";
#else
QString command = "zip -r " + zipFileName + " " + folderPath;
#endif
// 调用系统命令
QProcess process;
process.start(command);
process.waitForFinished();
qDebug() << "压缩完成:" << zipFileName;
return a.exec();
}
请注意,这段代码中使用了Qt的QDateTime类来获取当前日期和时间,并使用了系统命令来压缩文件夹。在Windows上,我们使用了PowerShell的Compress-Archive命令,而在其他操作系统上,我们使用了zip命令。
原文地址: https://www.cveoy.top/t/topic/jfLD 著作权归作者所有。请勿转载和采集!