Qt实现复制文件线程要点
1.文件复制
2.文件夹的创建
思路:获取源路径下文件及文件夹相对于路径的位置,利用线程进行相关操作。
根据上图:
-
// 文件位置集合
-
QStringList fileList;
-
fileList <<
"3.txt" <<
"4.txt" <<
"1/5.txt";
-
-
// 文件夹集合
-
QStringList dirList;
-
dirList<<
"1" <<
"2";
-
#ifndef COPYTHREAD_H
-
#define COPYTHREAD_H
-
-
#include <QThread>
-
#include <QString>
-
#include <QSet>
-
-
// 复制仓库
-
typedef
struct sourceRepositoryStu
-
{
-
QStringList fileList;
// 复制路径下的文件相对名字及位置
-
QStringList dirList;
// 复制路径下的文件夹
-
}SourceRepository;
-
-
// 用法
-
//QStringList fileList;
-
//QStringList dirList;
-
//fileList << "updater.cpp" << "updater.h" << "packager.cpp" << "updater/downloadmanager.cpp";
-
//dirList << "updater" << "packager";
-
//SourceRepository stu;
-
//stu.fileList = fileList;
-
//stu.dirList = dirList;
-
//CopyThread test(stu, "E:\\Test\\src", "E:\\Copy\\src");
-
//test.start();
-
/**
-
* @brief 复制线程
-
*
-
* 包括复制文件及建立文件夹
-
*/
-
class CopyThread :
public QThread
-
{
-
Q_OBJECT
-
public:
-
CopyThread(
const SourceRepository &sourceRepository,
-
const QString &sourceDir,
-
const QString &destinationDir, QObject *parent =
0);
-
-
signals:
-
void warning(const QString &warning);
-
void progression(int copiedFileCount, int totalFileCount);
-
void copyFinished(const QString &errorString);
-
-
protected:
-
void run() Q_DECL_OVERRIDE;
-
-
private:
-
SourceRepository m_sourceRepository;
// 源路径下的文件及文件夹名字
-
QString m_destinationDir;
// 目标路径
-
QString m_sourceDir;
// 源路径
-
};
-
-
#endif // COPYTHREAD_H
-
#include "CopyThread.h"
-
-
#include <QFile>
-
#include <QFileInfo>
-
#include <QDir>
-
-
#include <QDebug>
-
-
QString cleanPath(
const QString &pathName, bool separatorAtEnd)
-
{
-
if (pathName.isEmpty())
-
return pathName;
-
-
QString cleaned = pathName;
-
cleaned.replace(
'\\',
'/');
-
-
if (separatorAtEnd)
-
{
-
if (!cleaned.endsWith(
'/'))
-
cleaned +=
'/';
-
}
-
else
-
{
-
if (cleaned.endsWith(
'/'))
-
cleaned.chop(
1);
-
}
-
-
return cleaned;
-
}
-
CopyThread::CopyThread(
const SourceRepository &sourceRepository,
-
const QString &sourceDir,
-
const QString &destinationDir, QObject *
parent) :
-
QThread(
parent), m_sourceRepository(sourceRepository)
-
{
-
m_sourceDir = cleanPath(sourceDir,
true);
-
m_destinationDir = cleanPath(destinationDir,
true);
-
qWarning() << m_sourceDir << m_destinationDir;
-
}
-
-
void CopyThread::run()
-
{
-
try
-
{
-
QSet<QString> sourcefiles = m_sourceRepository.fileList.toSet();
-
QSet<QString> dirfiles = m_sourceRepository.dirList.toSet();
-
-
int i =
0;
-
QFileInfo destFileInfo;
-
QDir dir;
-
QString src, dest;
-
-
foreach(
const QString &dirname, dirfiles)
-
{
-
dest = m_destinationDir + dirname;
-
if (!dir.mkpath(dest))
-
qWarning() << tr(
"Unable to create path %1").arg(dest);
-
}
-
-
foreach(
const QString &filename, sourcefiles)
-
{
-
src = m_sourceDir + filename;
-
dest = m_destinationDir + filename;
-
-
destFileInfo.setFile(dest);
-
if (!dir.mkpath(destFileInfo.absolutePath()))
-
qWarning() << tr(
"Unable to create path %1").arg(destFileInfo.absolutePath());
-
-
if(QFile::exists(dest) && !QFile::remove(dest))
-
qWarning() << tr(
"Unable to remove %1").arg(dest);
-
-
if(!QFile::copy(src, dest))
-
qWarning() << tr(
"Unable to copy %1 to %2").arg(src, dest);
-
-
emit progression(++i, sourcefiles.size());
-
}
-
-
emit copyFinished(QString());
-
}
-
catch(std::exception &msg)
-
{
-
emit copyFinished(msg.what());
-
}
-
}