qt/LedOK/program/copydirthread.cpp
2023-04-18 14:14:46 +08:00

33 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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);
if(! targetDir.exists() && ! targetDir.mkdir(".")) return false;
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;
}