目录
1.自己的实现版本: 比较啰嗦
//复制一个文件中的全部内容到另一个文件夹中
void CopyFiles(QString strSour, QString strDest)
{
if(true == strSour.endsWith('/'))
{
strSour.chop(1);
}
if(true == strDest.endsWith('/'))
{
strDest.chop(1);
}
QDir dirDest(strDest);
//1.对参数进行判断,看是否存在路径
if(strSour.isEmpty() || strDest.isEmpty())
{
qDebug() << "传入的路径存在空的情况";
return;
}
//2.遍历strSour,得到完整的文件列表
QStringList strListTotal;
traversal(strSour, strListTotal);
qDebug() << "size:" << strListTotal.size();
qDebug() << "strListTotal:" << strListTotal;
//3.遍历列表,在目的目录下创建文件夹或把文件粘贴过去
int i = 0;
foreach(const QString & str, strListTotal)
{
QString strTmp = str;
QString strRear = strTmp.remove((strSour+"/"));//! 这里需要保证strSour以"/"结尾.
qDebug() << "strTmp:" << strTmp;
int nSlash = strRear.count('/');
bool bEndWithSlash = strRear.endsWith('/');
//如果不含'/',且strRear不为空,直接复制
if(0 == nSlash && false == strRear.isEmpty())
{
QFile::copy(str, strDest + "/" + strRear);
}
if(nSlash > 0)
{
if(bEndWithSlash)
{
QStringList strListRear = strRear.split('/');
QString strCombine = strDest;
//! 保证前一级的文件夹的存在
for(const QString &str: strListRear)
{
if(false == str.isEmpty())
{
strCombine = strCombine + "/" + str;
if(!dirDest.exists(strCombine))
{
dirDest.mkdir(strCombine);
}
}
}
}
else
{
bool bCopy = QFile::copy(str, strDest + "/" + strRear);
qDebug() << "str:" << str;
qDebug() << "bCopy:" << bCopy;
qDebug() << "strDest + strRear:" << (strDest + "/" + strRear);
qDebug() << i++;
}
}
}
}
void traversal(QString path, QStringList& strListTotal)
{
if(QDir(path).exists())
{
QString dirPath = path + "/";
strListTotal << dirPath;
QStringList strListPath = QDir(path).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::DirsFirst);//此处不可以仅使用NoDotAndDotDot,否则得到空列表
for(QString itemPath : strListPath)
{
qDebug() << "itemPath:" << itemPath;
QString absPath = path + "/" + itemPath;
traversal(absPath, strListTotal);
}
}
else
{
strListTotal << path;
}
}
void CopyFilesRecursive(QString strSour, QString strDest)
{
if(strSour.isEmpty())
{
return;
}
QDir dirSour(strSour);
QDir dirDest(strDest);
QStringList strList = dirSour.entryList(QDir::Dirs| QDir::Files | QDir::NoDotAndDotDot, QDir::DirsFirst);
for(const QString &str: strList)
{
QString fullPath = strSour + QDir::separator() + str;
QFileInfo fileInfo(fullPath);
if(fileInfo.isDir())
{
dirDest.mkdir(str);
CopyFilesRecursive(fullPath, strDest + QDir::separator() + str);//保持目录级别一致
}
else
{
QFile::copy(fullPath, strDest + QDir::separator() + str);//需要保证目标目录存在,文件才能copy成功
}
}
qDebug() << "info";
}
2.stackoverflow上的高赞答案
void copyPath(QString src, QString dst)
{
QDir dir(src);
if (! dir.exists())
return;
foreach (QString d, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
QString dst_path = dst + QDir::separator() + d;
dir.mkpath(dst_path);
copyPath(src+ QDir::separator() + d, dst_path);//use recursion
}
foreach (QString f, dir.entryList(QDir::Files)) {
QFile::copy(src + QDir::separator() + f, dst + QDir::separator() + f);
}
}
链接:https://stackoverflow.com/questions/2536524/copy-directory-using-qt