递归遍历文件夹,递归解压,压缩文件,写解压和压缩过程的日志,

前言好久好久没另写一篇博客了,都是在更新QT常用函数里面内容,这瘟疫影响了我躁动奋进的心,又是大半年没努力了,气人!

这次做的是个多个压缩包 将压缩包里面的内容合并起来 打包成对应的压缩包的工具。

1、递归解压文件:给我一个压缩包,里面还有很多压缩包,都解压完成才能拿到想要的文件:

bool MainWindow::UnzipFile(QString ZipFilePath)//ZipFilePath是给的初始压缩包
{
    QString strExeName = QApplication::applicationDirPath() + "/unzip.exe";

    QString name = QFileInfo(ZipFilePath).fileName();
    QString str = ZipFilePath.mid(0,ZipFilePath.indexOf(name));
    name = name.mid(0,name.indexOf(".zip"));
    	
    QStringList commands;
    commands << "-o" << "-d" << str + name << ZipFilePath;

    QProcess process;
    process.start(strExeName,commands);
    process.waitForFinished(30*1000);

    QByteArray content = process.readAll();
    AddLogDirect(QString::fromLocal8Bit(content));//这个接口是打log用的
	
    if(process.exitCode() != 0)
    {
        return false;
    }
	QFileInfoList filelist = GetFileList(str + name);//递归遍历文件夹

    foreach(QFileInfo fileinfo, filelist)
    {
        if(fileinfo.suffix() == "zip")
        {
            UnzipFile(fileinfo.filePath());
        }
    }
    return true;
}

2、递归遍历文件夹:我这里用的是找到zip结尾的文件再解压

QFileInfoList GetFileList(QString path)
{
    QDir dir(path);

    //列出dir(path)目录文件下所有文件和目录信息,存储到file_list容器
    QFileInfoList file_list = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    //列出dir(path)目录下所有子文件夹
    QFileInfoList folder_list = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
    //进行子文件夹folder_list递归遍历,将内容存入file_list容器
    for(int i= 0; i != folder_list.size(); i++)
    {
        QString name = folder_list.at(i).absoluteFilePath();
        QFileInfoList child_file_list = GetFileList(name);
        file_list.append(child_file_list);
    }
    return file_list;
}

3、压缩文件

QStringList versionFilelist;
dir.mkdir(strDirName);

QString strExeName = QApplication::applicationDirPath() + "/zip.exe";
QStringList commands;
QProcess process;
commands<< "-j" << strDirName;
for(int i=0; i<m_ListScriptFile.length(); i++)
{
    commands << m_ListScriptFile.at(i).filePath();
}

process.start(strExeName, commands);
process.waitForFinished(30*1000);

QByteArray content = process.readAll();
AddLogDirect(QString::fromLocal8Bit(content));


for(int i=0; i<versionFilelist.length(); i++)
{
    dir.remove(versionFilelist.at(i));
}

dir.rmdir(strDirName);

if(process.exitCode() != 0)
{
    QMessageBox::warning(this,"Warning","zip the files of Script failed");
    return;
}
else
    QMessageBox::information(this,"Warning","zip the files of Script successfully");

}

4、打log:在解压跟压缩文件的时候用的那个接口

QByteArray content = process.readAll();
AddLogDirect(QString::fromLocal8Bit(content));

void MainWindow::AddLogDirect(const QString& strContent)
{
    ui->textEditLog->append(strContent);
    m_eventLoop->processEvents();//QEventLoop *m_eventLoop;
}

!!!如有错误,请指正!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值