qt/LedOK/program/copydirthread.cpp

33 lines
1.3 KiB
C++
Raw Normal View History

2023-04-18 14:14:46 +08:00
#include "copydirthread.h"
#include <QDir>
CopyDirThread::CopyDirThread() {
connect(this, &CopyDirThread::finished, this, &CopyDirThread::deleteLater);
}
void CopyDirThread::run() {
for(; i<dirSrcs.size(); i++) {
copiedSize = 0;
copyDir(dirSrcs[i], dirDst+"/"+QFileInfo(dirSrcs[i]).fileName(), true);
}
}
bool CopyDirThread::copyDir(const QString &fromDir, const QString &toDir, bool coverIfExist) {
QDir targetDir(toDir);
2023-04-28 16:02:14 +08:00
if(! targetDir.exists() && ! targetDir.mkdir(toDir)) return false;
2023-04-18 14:14:46 +08:00
QFileInfoList fileInfos = QDir(fromDir).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
foreach(QFileInfo fileInfo, fileInfos) {
if(fileInfo.isDir()) { //< 当为目录时递归的进行copy
if(! copyDir(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()), coverIfExist)) return false;
} else { //当允许覆盖操作时,将旧文件进行删除操作
if(coverIfExist && targetDir.exists(fileInfo.fileName())) targetDir.remove(fileInfo.fileName());
if(! QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) return false;
else {
copiedSize += fileInfo.size();
emit sigProgress(i, copiedSize);
}
}
}
return true;
}