qt使用7z压缩

文章定义了一个名为SZip的C++类,用于文件的压缩和解压操作。它依赖于7z.exe和7z.dll库,通过QProcess来执行命令行操作。类中包含了zip和deZip两个方法,分别处理压缩和解压任务,并检查相关库文件是否存在。
摘要由CSDN通过智能技术生成

.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", "./");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值