12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "FileUtil.h"
- #include <QCoreApplication>
- #include <QFileDialog>
- #include <QSettings>
- #include <QDebug>
- #include <qdir.h>
- FileUtil::FileUtil() { }
- bool FileUtil::copyDirectoryFiles(QString &srcFilePath, QString &tgtFilePath, bool coverFileIfExist)
- {
- QDir sourceDir(tgtFilePath);
- QDir targetDir(srcFilePath);
- if (!targetDir.exists()) { /**< 如果目标目录不存在,则进行创建 */
- if (!targetDir.mkdir(targetDir.absolutePath()))
- return false;
- }
- QFileInfoList fileInfoList = sourceDir.entryInfoList();
- foreach (QFileInfo fileInfo, fileInfoList) {
- if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
- continue;
- if (fileInfo.isDir()) { /**< 当为目录时,递归的进行copy */
- QString str = fileInfo.filePath();
- QString strTar = targetDir.filePath(fileInfo.fileName());
- if (!copyDirectoryFiles(str, strTar, coverFileIfExist))
- return false;
- } else { /**< 当允许覆盖操作时,将旧文件进行删除操作 */
- if (coverFileIfExist && targetDir.exists(fileInfo.fileName())) {
- targetDir.remove(fileInfo.fileName());
- }
- /// 进行文件copy
- if (!QFile::copy(fileInfo.filePath(),
- targetDir.filePath(fileInfo.fileName()))) {
- return false;
- }
- }
- }
- return true;
- }
- bool FileUtil::copyFileToPath(QString sourceDir, QString toDir, bool coverFileIfExist)
- {
- toDir.replace("\\", "/");
- if (sourceDir == toDir) {
- return true;
- }
- if (!QFile::exists(sourceDir)) {
- return false;
- }
- QDir createfile;
- bool exist = createfile.exists(toDir);
- if (exist) {
- if (coverFileIfExist) {
- createfile.remove(toDir);
- // createfile.mkdir(toDir);
- }
- } // end if
- if (!QFile::copy(sourceDir, toDir)) {
- return false;
- }
- return true;
- }
- bool FileUtil::importEngineerFile()
- {
- QFileDialog::Options options;
- options |= QFileDialog::DontUseNativeDialog;
- QString fileName = QFileDialog::getOpenFileName(nullptr, "导入资源包", "/", "");
- qDebug() << "fileName-------" << fileName;
- QString sysDbPath = QCoreApplication::applicationDirPath();
- sysDbPath += "/importData/exportData.ini";
- qDebug() << "sysDbPath-------" << sysDbPath;
- qDebug() << "fileName-------" << fileName;
- bool result = FileUtil().copyFileToPath(fileName, sysDbPath, true);
- qDebug() << "result-------" << result;
- return result;
- }
|