forked from nerososft/Nider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehelper.cpp
executable file
·144 lines (129 loc) · 3.91 KB
/
filehelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "filehelper.h"
#include <QFileInfo>
#include <QDebug>
#include <QFile>
#include <errorhelper.h>
FileHelper::FileHelper()
{
}
//拷贝文件:
bool FileHelper::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
if (sourceDir == toDir){
return true;
}
if (!QFile::exists(sourceDir)){
return false;
}
QDir *createfile = new QDir;
bool exist = createfile->exists(toDir);
if (exist){
if(coverFileIfExist){
createfile->remove(toDir);
}
}//end if
if(!QFile::copy(sourceDir, toDir))
{
return false;
}
return true;
}
//拷贝文件夹:
bool FileHelper::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
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 */
if(!copyDirectoryFiles(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
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;
}
QFileInfoList FileHelper::getFileList(QString path,QString houzhui){
QFileInfoList InfoList = QDir( path).entryInfoList();//获取当前目录所有文件
QFileInfoList SuffixInfoList;//定义放提取文件的List
//遍历
foreach(QFileInfo fileInfo, InfoList){
if(!fileInfo.isFile()) continue;//不是文件继续,只用于加速,可不加
//qDebug()<<fileInfo.fileName()<<" "<<fileInfo.suffix();
//后缀不区分大小写,需要区分直接用“==”
if(fileInfo.suffix()==houzhui)
{
SuffixInfoList << fileInfo;//指定后缀,加入列表
}
}
return SuffixInfoList;
}
bool FileHelper::createFile(QString file,QString content){
QFile File(file);
if (!File.open(QIODevice::WriteOnly|QIODevice::Text)) {
return false;
}
QTextStream out(&File);
out<<content<<endl;
out.flush();
File.close();
return true;
}
QString FileHelper::loadFileToString(QString filepath){
QString buffer;
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
ErrorHelper::Error("文件"+filepath+"读取失败");
return "";
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
buffer+=line;
}
file.close();
return buffer;
}
QString FileHelper::loadCodeFileToString(QString filepath){
QString buffer;
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
ErrorHelper::Error("文件"+filepath+"读取失败");
return "";
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
buffer+=line+"\n";
}
file.close();
return buffer;
}
bool FileHelper::saveFile(QString filePath,QString content){
QFile File(filePath);
if (!File.open(QIODevice::WriteOnly|QIODevice::Text)) {
return false;
}
QTextStream out(&File);
out<<content<<endl;
out.flush();
File.close();
return true;
}