FileUtil.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "FileUtil.h"
  2. #include <QCoreApplication>
  3. #include <QFileDialog>
  4. #include <QSettings>
  5. #include <QDebug>
  6. #include <qdir.h>
  7. FileUtil::FileUtil() { }
  8. bool FileUtil::copyDirectoryFiles(QString &srcFilePath, QString &tgtFilePath, bool coverFileIfExist)
  9. {
  10. QDir sourceDir(tgtFilePath);
  11. QDir targetDir(srcFilePath);
  12. if (!targetDir.exists()) { /**< 如果目标目录不存在,则进行创建 */
  13. if (!targetDir.mkdir(targetDir.absolutePath()))
  14. return false;
  15. }
  16. QFileInfoList fileInfoList = sourceDir.entryInfoList();
  17. foreach (QFileInfo fileInfo, fileInfoList) {
  18. if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  19. continue;
  20. if (fileInfo.isDir()) { /**< 当为目录时,递归的进行copy */
  21. QString str = fileInfo.filePath();
  22. QString strTar = targetDir.filePath(fileInfo.fileName());
  23. if (!copyDirectoryFiles(str, strTar, coverFileIfExist))
  24. return false;
  25. } else { /**< 当允许覆盖操作时,将旧文件进行删除操作 */
  26. if (coverFileIfExist && targetDir.exists(fileInfo.fileName())) {
  27. targetDir.remove(fileInfo.fileName());
  28. }
  29. /// 进行文件copy
  30. if (!QFile::copy(fileInfo.filePath(),
  31. targetDir.filePath(fileInfo.fileName()))) {
  32. return false;
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. bool FileUtil::copyFileToPath(QString sourceDir, QString toDir, bool coverFileIfExist)
  39. {
  40. toDir.replace("\\", "/");
  41. if (sourceDir == toDir) {
  42. return true;
  43. }
  44. if (!QFile::exists(sourceDir)) {
  45. return false;
  46. }
  47. QDir createfile;
  48. bool exist = createfile.exists(toDir);
  49. if (exist) {
  50. if (coverFileIfExist) {
  51. createfile.remove(toDir);
  52. // createfile.mkdir(toDir);
  53. }
  54. } // end if
  55. if (!QFile::copy(sourceDir, toDir)) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool FileUtil::importEngineerFile()
  61. {
  62. QFileDialog::Options options;
  63. options |= QFileDialog::DontUseNativeDialog;
  64. QString fileName = QFileDialog::getOpenFileName(nullptr, "导入资源包", "/", "");
  65. qDebug() << "fileName-------" << fileName;
  66. QString sysDbPath = QCoreApplication::applicationDirPath();
  67. sysDbPath += "/importData/exportData.ini";
  68. qDebug() << "sysDbPath-------" << sysDbPath;
  69. qDebug() << "fileName-------" << fileName;
  70. bool result = FileUtil().copyFileToPath(fileName, sysDbPath, true);
  71. qDebug() << "result-------" << result;
  72. return result;
  73. }