.h文件
#ifndef SZIP_H
#define SZIP_H
#include <QString>
class SZip
{
public:
SZip();
bool zip(const QString &path, const QString &dstPath); // 压缩
bool deZip(const QString &path, const QString &dstPath); // 解压
};
#endif // SZIP_H
.cpp文件
#include "szip.h"
#include "qdebug.h"
#include <QProcess>
#include <QApplication>
#include <QFile>
#include <QTime>
SZip::SZip()
{
}
bool SZip::zip(const QString &path, const QString &dstPath)
{
QTime time;
time.start();
// 查看是否少库
QStringList depends;
depends << "7z.exe"
<< "7z.dll";
qDebug() << "QApplication::applicationDirPath(): " << QApplication::applicationDirPath();
for(const QString &file : depends)
{
QString strFile = QApplication::applicationDirPath() + "/" + file;
if (!QFile::exists(strFile))
{
qDebug() << "shao ku";
return false;
}
}
QProcess process(0);
QStringList args;
args.append("a");
args.append(dstPath);
args.append(path);
args.append("-mx=3");
process.start(QApplication::applicationDirPath() + "/7z.exe", args);
process.waitForStarted();
process.waitForFinished();
QString out = QString::fromLocal8Bit(process.readAllStandardOutput());
qDebug() << "zip " << time.elapsed() / 1000.0 << "s";
return true;
}
bool SZip::deZip(const QString &path, const QString &dstPath)
{
QTime time;
time.start();
QStringList depends;
depends << "7z.exe"
<< "7z.dll";
qDebug() << "QApplication::applicationDirPath(): " << QApplication::applicationDirPath();
for(const QString &file : depends)
{
QString strFile = QApplication::applicationDirPath() + "/" + file;
if (!QFile::exists(strFile))
{
qDebug() << "shao ku1";
return false;
}
}
QProcess process(0);
QStringList args;
args.append("x");
args.append(path);
args.append("-o" + dstPath);
args.append("-aoa");
process.start(QApplication::applicationDirPath() + "/7z.exe", args);
process.waitForStarted();
process.waitForFinished();
QString out = QString::fromLocal8Bit(process.readAllStandardOutput());
qDebug() << "dezip " << time.elapsed() / 1000.0;
return true;
}
SZip zip; zip.zip("test.txt", "test.zip"); zip.deZip("test.zip", "./");