.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", "./");
C++实现的文件压缩与解压类SZip,
文章定义了一个名为SZip的C++类,用于文件的压缩和解压操作。它依赖于7z.exe和7z.dll库,通过QProcess来执行命令行操作。类中包含了zip和deZip两个方法,分别处理压缩和解压任务,并检查相关库文件是否存在。
1226

被折叠的 条评论
为什么被折叠?



