最近在翻看项目代码时,看到了这两个函数,想到这个功能十分常用,因此拿出来与大家分享,希望对大家有用。几点说明:
1、记得当初写代码那会,是参考了网上的帖子写的,做了一点小修改。因此代码源于网络。
2、同时感谢原作者,只可惜当时没能记下原文网址,实在抱歉!刚才搜了一下,也没搜着,大家若发现原文出处,请跟帖提醒。谢谢!
3、到目前为止,代码在项目中测试、运行正常,大家若使用时发现Bug,请跟帖指出,我待验证后会及时修改更新。谢谢!
- bool copyDir(const QString &source, const QString &destination, bool override)
- {
- QDir directory(source);
- if (!directory.exists())
- {
- return false;
- }
-
- QString srcPath = QDir::toNativeSeparators(source);
- if (!srcPath.endsWith(QDir::separator()))
- srcPath += QDir::separator();
- QString dstPath = QDir::toNativeSeparators(destination);
- if (!dstPath.endsWith(QDir::separator()))
- dstPath += QDir::separator();
-
- bool error = false;
- QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
- for (QStringList::size_type i=0; i != fileNames.size(); ++i)
- {
- QString fileName = fileNames.at(i);
- QString srcFilePath = srcPath + fileName;
- QString dstFilePath = dstPath + fileName;
- QFileInfo fileInfo(srcFilePath);
- if (fileInfo.isFile() || fileInfo.isSymLink())
- {
- if (override)
- {
- QFile::setPermissions(dstFilePath, QFile::WriteOwner);
- }
- QFile::copy(srcFilePath, dstFilePath);
- }
- else if (fileInfo.isDir())
- {
- QDir dstDir(dstFilePath);
- dstDir.mkpath(dstFilePath);
- if (!copyDir(srcFilePath, dstFilePath, override))
- {
- error = true;
- }
- }
- }
-
- return !error;
- }
QtCreator版本(在阅读 QtCreator 源码时,看到一个和以上功能一样的函数,想必像QtCreator这样的项目代码质量比我等程序员的代码质量更高。因此,特摘抄下来已做更新):
- static bool copyRecursively(const QString &srcFilePath,
- const QString &tgtFilePath)
- {
- QFileInfo srcFileInfo(srcFilePath);
- if (srcFileInfo.isDir()) {
- QDir targetDir(tgtFilePath);
- targetDir.cdUp();
- if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
- return false;
- QDir sourceDir(srcFilePath);
- QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
- foreach (const QString &fileName, fileNames) {
- const QString newSrcFilePath
- = srcFilePath + QLatin1Char(‘/‘) + fileName;
- const QString newTgtFilePath
- = tgtFilePath + QLatin1Char(‘/‘) + fileName;
- if (!copyRecursively(newSrcFilePath, newTgtFilePath))
- return false;
- }
- } else {
- if (!QFile::copy(srcFilePath, tgtFilePath))
- return false;
- }
- return true;
- }
- bool deleteDir(const QString &dirName)
- {
- QDir directory(dirName);
- if (!directory.exists())
- {
- return true;
- }
-
- QString srcPath = QDir::toNativeSeparators(dirName);
- if (!srcPath.endsWith(QDir::separator()))
- srcPath += QDir::separator();
-
- QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
- bool error = false;
- for (QStringList::size_type i=0; i != fileNames.size(); ++i)
- {
- QString filePath = srcPath + fileNames.at(i);
- QFileInfo fileInfo(filePath);
- if (fileInfo.isFile() || fileInfo.isSymLink())
- {
- QFile::setPermissions(filePath, QFile::WriteOwner);
- if (!QFile::remove(filePath))
- {
- qDebug() << "remove file" << filePath << " faild!";
- error = true;
- }
- }
- else if (fileInfo.isDir())
- {
- if (!deleteDir(filePath))
- {
- error = true;
- }
- }
- }
-
- if (!directory.rmdir(QDir::toNativeSeparators(directory.path())))
- {
- qDebug() << "remove dir" << directory.path() << " faild!";
- error = true;
- }
-
- return !error;
- }
http://blog.csdn.net/e5max/article/details/11923803
Qt递归拷贝和删除目录
原文:http://www.cnblogs.com/findumars/p/5196236.html