qt有个quazip的库,但是倒腾过来还要编译,我是qt小白,一直没做出来.

考虑只需要windows的环境能用就行,改用QProcess和7z.exe直接按命令行操作实现一个类;

参考连接 : <<7z命令行>>https://www.jianshu.com/p/4f9be6b47161

参考连接 : <<7z 压缩命令行工具>>https://www.cnblogs.com/Frank99/p/5951177.html

参考连接 : <<Windows下Qt用7z解压、7za压缩tar文件>>https://blog.csdn.net/yuchunhai321/article/details/100089384?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

分享代码仅做参考,有以下缺点:

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 <QObject>
#include <QDir>
#include <QProcess>
#include <QApplication>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-21Open 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 = 44090955Scanning the drive:
1 file, 45059039 bytes (43 MiB)Updating archive: C:/Users/user/Desktop/qt_cpp_lib/build-cpp_lib/debug/123.zipAdd 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-21Scanning 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 = 44090955Everything is OkSize:       45059039
Compressed: 44090955

qt调用7z.exe压缩文件相关推荐

  1. Qt调用7z实现压缩和解压缩

    生成随机密码 QString GeneratePwd() {char buf[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', ' ...

  2. powershell:调用7z,haozip解压缩文件

    因为windows内置了zip格式文件的解压缩,所以powershell中可以直接调用powershell的函数就能实现zip解压缩. 利用powershell内置功能实现zip解压缩的代码如下: # ...

  3. 程序调用winrar自动压缩文件

    程序调用winrar自动压缩文件,有时在备份文件等,完成后需要用本机的winrar对文件自动压缩并拷贝走. 这里提供一个例程: try{String winRarPath = null;if (!Ex ...

  4. linux 使用7z 分卷压缩文件到指定目录(附上安装包和安装方法)

    安装7z,下载地址 7z linux下载地址https://download.csdn.net/download/qq_27608983/11244291 解压安装 linux命令: 1.tar xj ...

  5. 如何在VC中使用7z SDK压缩文件

    首先登陆7z官方组织网站https://www.7-zip.org/,英文不好的朋友可先切换至中文. 打开左边LZMA SDK页面,下载如下图最新SDK和说明文档. 下载好后,SDK包内容对多种编程语 ...

  6. WinRAR.exe压缩文件夹的脚本bat

    压缩脚本 将该脚本复制下来,放到文本编辑器中.最后将文件的后缀名改为.bat 保存后将该脚本放在需要打包文件的同级目录. xcopy /E/Y .\destfile .\tmpfile destfil ...

  7. Unity3D调用7z解压文件

    原文地址 using UnityEngine; using System; using System.IO; using System.Diagnostics;public class Test : ...

  8. Python调用winrar.exe进行文件压缩

    文章目录 前言 简介 压缩实例 目录结构 执行代码 运行结果 小结 前言 Python语言中有一个zipfile库,可以利用zip进行压缩解压.我们知道,通常 Rar 具有更高的压缩率.在Python ...

  9. Qt 调用 chm格式的文件

    在windows平台下, 有一个系统默认的,打开chm格式文件的exe. -- hh.exe 在QT中使用, 只需要 QProcess process; process.startDetached(& ...

最新文章

  1. 缩略图信息提取工具vinetto
  2. python语法怎么读-python怎么读sql数据?
  3. ML之SVM:利用SVM算法对手写数字图片识别数据集(PCA降维处理)进行预测并评估模型(两种算法)性能
  4. python题目及解析_python知识点总结以及15道题的解析
  5. mybatis SqlMapConfig.xml mappers
  6. 使用 IntraWeb (21) - 基本控件之 TIWTabControl
  7. mysql 格式化_在线SQL代码格式化
  8. pg_bigm与pg_trgm的安装、测试、对比
  9. 微信注册验证成功之后不跳转_想申请小号的网友福利来了:绑定了微信的手机号怎么注册新的微信...
  10. c语言编译器提示隐式声明,在C中使用“隐式声明功能”警告有什么含义?
  11. eclipse建web工程的dynamic web module version 具体是什么意思
  12. Java中Arrays.sort()的三种常用用法(自定义排序规则)
  13. World Development Indicators(世界发展指标相关数据集)
  14. 2020年中考英语计算机考试,2020年中考英语听说测试考生问答
  15. 对Numpy库ndarray对象(矩阵)中的数据元素的访问、选取操作示例
  16. 测量角速度_自动驾驶系统的定海神针—惯性测量单元
  17. php投影,ppt投影怎么显示备注
  18. mysql实战(五)—— You can‘t specify target table ‘org_department‘ for update in FROM clause
  19. 基于单片机双路温度检测报警系统设计-基于单片机声光控智能开关控制灯设计-基于单片机热电偶智能体温检测设计-基于单片机小型家用燃气锅炉控制系统设计-基于单片机一氧化碳有毒气体采集报警系统设计
  20. js中深浅拷贝理解与方法

热门文章

  1. 批量新增,存在就修改
  2. 第七周作业——第三种
  3. Android Studio与夜神模拟器开发调试
  4. 浪潮服务器2016销售额,Gartner:2016前三季度,浪潮服务器销售额中国第一
  5. 英文版-安静-Hillsong-Still
  6. 解剖95后—洞悉未来十年消费红利.pdf
  7. php注入点判断,PHPEMS一处SQL注入漏洞
  8. 通信设备为什么要使用-48V的电压?为什么正极接地?
  9. MT7921:通过BLE发送指定UUID的广播
  10. 常见自来水水表类型大全