qt有个quazip的库,但是倒腾过来还要编译,我是qt小白,一直没做出来.
考虑只需要windows的环境能用就行,改用QProcess和7z.exe直接按命令行操作实现一个类;
参考连接 : <<7z命令行>>https://www.jianshu.com/p/4f9be6b47161
参考连接 : <<7z 压缩命令行工具>>https://www.cnblogs.com/Frank99/p/5951177.html
参考连接 : <
分享代码仅做参考,有以下缺点:
1现在还是同步操作,压缩大文件耗时较长的,会卡UI,需要的请改线程方式;
2输入输出异常不做异常捕获,容易输入错误导致执行失败;
3数据无价,代码仅为分享和学习.不要轻易应用到你的程序中,导致数据丢失不负责.
7z.exe和7z.dll从7z官网安装包中获取即可,我是从安装包7z1900.exe改为7z1900.rar直接解压缩.取的文件;
虽然很多文件没带上,但是用来压缩解压好像是没问题;
7z.exe和7z.dll加入qrc资源文件这个不解释.
#ifndef ZIP7Z_H
#define ZIP7Z_H
#include
#include
#include
#include
class Zip7z : public QObject
{
Q_OBJECT
public:
static bool Zip(QString dest,QString src_partten,QString &out);
static bool UnZip(QString src,QString dest,QString &out);
private:
static bool Init(QString &out);
};
#endif // ZIP7Z_H
#include "zip7z.h"
bool Zip7z::Zip(QString dest,QString src_partten,QString &out){
QString filestate = "";
out = "";
if(Init(filestate)==false){
out = filestate + QString::fromLocal8Bit("不能执行压缩");
return false;
}
else{
QProcess p(0);
QStringList args = QStringList();
args.append("a");
args.append(dest);
args.append(src_partten);
p.start(QApplication::applicationDirPath() + "/7z.exe",args);
p.waitForStarted();
p.waitForFinished();
out = QString::fromLocal8Bit(p.readAllStandardOutput());
return true;
}
}
bool Zip7z::UnZip(QString src,QString dest,QString &out){
QString filestate = "";
out = "";
if(Init(filestate)==false){
out = filestate + QString::fromLocal8Bit("不能执行压缩");
return false;
}
else{
QProcess p(0);
QStringList args = QStringList();
args.append("x");
args.append(src);
args.append("-o" + dest);//-o和目标目录必须连在一起
args.append("-aoa");//不提示,直接覆盖同名文件
p.start(QApplication::applicationDirPath() + "/7z.exe",args);
p.waitForStarted();
p.waitForFinished();
out = QString::fromLocal8Bit(p.readAllStandardOutput());
return true;
}
}
bool Zip7z::Init(QString &out){
out = "";
QStringList depends = QStringList();
depends.append("7z.exe");
depends.append("7z.dll");
for(QString file : depends){
QString loaclfile = QApplication::applicationDirPath() + "/" + file;
if(QDir().exists(file)==false){
//从资源释放文件到本地
QString qrcfile = ":/7z/" + file;
QFile::copy(qrcfile,loaclfile);
}
}
out=QString::fromLocal8Bit("文件检查完成,正常.");
return true;
}
调用:
void MainWindow::on_pushButton_4_clicked()
{
QString out;
bool successful = Zip7z::Zip(QApplication::applicationDirPath() + "/123.zip",
QApplication::applicationDirPath() + "/789.txt",out);
if(!successful){
QApplication::beep();
}
qDebug() << out;
qDebug() << "--------------------------------------------------------";
Zip7z::UnZip(QApplication::applicationDirPath() + "/123.zip",
QApplication::applicationDirPath() + "/unzip",out);
qDebug() << out;
}
输出:
7-Zip 19.00 (x86) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Open archive: C:/Users/user/Desktop/qt_cpp_lib/build-cpp_lib/debug/123.zip
--
Path = C:/Users/user/Desktop/qt_cpp_lib/build-cpp_lib/debug/123.zip
Type = zip
Physical Size = 44090955
Scanning the drive:
1 file, 45059039 bytes (43 MiB)
Updating archive: C:/Users/user/Desktop/qt_cpp_lib/build-cpp_lib/debug/123.zip
Add new data to archive: 1 file, 45059039 bytes (43 MiB)
Files read from disk: 1
Archive size: 44090955 bytes (43 MiB)
Everything is Ok
--------------------------------------------------------
7-Zip 19.00 (x86) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Scanning the drive for archives:
1 file, 44090955 bytes (43 MiB)
Extracting archive: C:\Users\user\Desktop\qt_cpp_lib\build-cpp_lib\debug\123.zip
--
Path = C:\Users\user\Desktop\qt_cpp_lib\build-cpp_lib\debug\123.zip
Type = zip
Physical Size = 44090955
Everything is Ok
Size: 45059039
Compressed: 44090955