前言:好久好久没另写一篇博客了,都是在更新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;
}
!!!如有错误,请指正!!!