-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.cpp
359 lines (337 loc) · 10.6 KB
/
file.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "file.h"
#include "mainwindow.h"
#include <QDebug>
#include <QtWidgets/QMessageBox>
#include <QProcess>
#include <QDir>
#include <QFile>
#include <QCoreApplication>
#include <RestartManager.h>
#include <winbase.h>
#include <psapi.h>
#include <stdio.h>
#include "Start.h"
#include "Sandefine.h"
using namespace std;
void file_search(QString path,QStringList &fileList)
{
/*遍历目录*/
//path=path.replace("/","\\")+"\\";
//path =path.replace(" ","%20")+"/";
//qDebug()<<"开始遍历目标目录:"<<path;
QDir dir(path);
if (!dir.exists())
{
qDebug()<<dir.exists();
qDebug()<<dir.isRelative();
return;
}
//取到所有的文件和文件名,但是去掉.和..的文件夹
dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
//文件夹优先
dir.setSorting(QDir::DirsFirst);
//转化成一个list
QFileInfoList clist = dir.entryInfoList();
if(clist.size()< 1 ) {
return;
}
//递归
int i=0;
do{
//此时list带当前目录所有文件的信息,包括文件夹和文件
QFileInfo fileInfo = clist.at(i);
if(fileInfo.isDir())
{
//文件夹,递归
//qDebug()<<"文件夹:"<<fileInfo.filePath();
file_search(fileInfo.filePath(),fileList);
}else{
//文件,添加成员
fileList<<fileInfo.filePath();
}
//emit tworkProcess(i,clist.size());
i++;
}while(i < clist.size()); //遍历当前目录完毕
return;
}
/*获取环境变量temp*/
QString getTempPath(QString path)
{
Start::stlog(modulefile,
"获取环境变量"+path,
0);
return QProcessEnvironment::systemEnvironment().value(path).replace("\\","/");
}
/*创建文件夹*/
bool createFolderSlot(QString path)
{
Start::stlog(modulefile,
"尝试新建目录 "+path,
0);
bool res;
QDir dir;
if (!dir.exists(path))
{
res = dir.mkpath(path);
qDebug()<<"新建目录"<<path<<res;
if(res){
Start::stlog(modulefile,
"成功",
0);
}else{
Start::stlog(modulefile,
"失败",
0);
}
}else{
qDebug()<<"目录已存在"<<path;
Start::stlog(modulefile,
"目录已存在",
0);
res = true;
}
return res;
}
/*释放资源文件到目录*/
void saveResourecFile(QString resProfiex,QString resFileName,QString destFullPathFileName)
{
// :/CRT/Resource/crt/curl-ca-bundle.crt
QString resFile=":/"+resProfiex+"/"+resFileName;
QFileInfo info(destFullPathFileName);
createFolderSlot(info.path());
Start::stlog(modulefile,
"尝试释放资源文件\r\n"+
resFile+
destFullPathFileName,
0);
if(QFile::copy(resFile,destFullPathFileName))
{
Start::stlog(modulefile,
"成功",
0);
}else{
Start::stlog(modulefile,
"失败",
0);
}
}
void writeTXT(QString* data,QString Path,QString fileName)
{
createFolderSlot(Path);
QFile file(Path
+fileName
);
file.open(QFile::WriteOnly);
file.write(
data->replace("},{","},\r\n{")//数组分行
.toLocal8Bit()
);
file.close();
}
/*读取文本文件并返回字符串*/
QString readTXT(QString Path)
{
qDebug()<<"读取文件"<<Path;
QFile file(Path.replace("\\","/"));
QString TXT;
// qDebug()<<"已被打开"<<file.isOpen();
if(!file.open(QIODevice::ReadOnly))
{
qDebug()<<"Can't open the file!";
}
while(!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
//删除换行符 str.trimmed();
//qDebug()<<"正在读文件"<< str.trimmed();
TXT.append(str.trimmed());
}
file.close();
return TXT;
}
/*对比获取需要更新的文件*/
QStringList getUptater(QStringList localFilePath
,QStringList localFileMD5
,QStringList newFilePath
,QStringList newFileMD5
,QString lworkPath=""
)//已弃用
{
/*已弃用*/
QStringList needupdater;
int oldsize = localFileMD5.size();
int newsize = newFileMD5.size();
qDebug()<<"新文件"<<newsize<<"旧文件"<<oldsize;
bool duplicateFile;
for(int i=0;i<newsize;i++)
{
duplicateFile=false;
qDebug()<<i+1<<"/"<<newsize<<"|"<<newFileMD5.at(i)<<newFilePath.at(i);
for(int j=0;j<oldsize;j++)
{
if(newFileMD5.at(i)==localFileMD5.at(j)
&&
newFilePath.at(i)==QString(localFilePath.at(j)).replace(lworkPath+"/","")
)
{
// qDebug()<<newFilePath.at(i);
// qDebug()<<lworkPath;
// qDebug()<<QString(localFilePath.at(j)).replace(lworkPath+"/","");
duplicateFile=true;
break;
}
}
if(!duplicateFile)
{
needupdater<<newFilePath.at(i);
}
}
for(int z=0;z<needupdater.size();z++)
{
qDebug()<<"需要更新的文件:"<<z+1<<"/"<<newsize<<"|"<<needupdater.at(z);
}
return needupdater;
}
bool moveFile(QString oldPath,QString newPath)
{
int err = 0;
Start::stlog(modulefile,
"更新文件\t"+oldPath,
0);
Start::stlog(modulefile,
"\t"+newPath,
0);
oldPath.replace("\\","/");
newPath.replace("\\","/");
qDebug()<<"oldPath:"<<oldPath;
qDebug()<<"newPath:"<<newPath;
bool re;
QFile nfile(newPath);
if(nfile.exists())//删除目标文件
{
Start::stlog(modulefile,
"检查文件属性\t",
0);
qDebug()<<"检查文件属性";
SetLastError(err);
DWORD FileAttributes=GetFileAttributesW((LPCWSTR)newPath.replace("/","\\").unicode());
if(FileAttributes)
{
if (FileAttributes & FILE_ATTRIBUTE_READONLY)
{
Start::stlog(modulefile,
"文件为只读\tFILE_ATTRIBUTE_READONLY",
0);
qDebug()<<"文件为只读\tFILE_ATTRIBUTE_READONLY";
SetLastError(err);
SetFileAttributesW(
(LPCWSTR)newPath.replace("/","\\").unicode()
,FILE_ATTRIBUTE_NORMAL
);
err=GetLastError();
Start::stlog(modulefile,
"改写文件属性(下只读) "+errcode2str(err),
0);
qDebug()<<"SetFileAttributesW"<<errcode2str(err);
}
}else{
Start::stlog(modulefile,
"获取文件属性失败"+errcode2str(err),
0);
qDebug()<<"获取文件属性失败"+errcode2str(err);
}
Start::stlog(modulefile,
"删除目标文件",
0);
qDebug()<<"删除目标文件";
SetLastError(err);
if(!nfile.remove())
{
err=GetLastError();
Start::stlog(modulefile,
"QFile::remove fail "+errcode2str(err),
0);
qDebug()<<"QFile::remove fail "<<errcode2str(err);
SetLastError(err);
if(remove(newPath.replace("/","\\").toLocal8Bit().constData())==-1)
{
err=GetLastError();
Start::stlog(modulefile,
"std::remove fail "+errcode2str(err),
0);
qDebug()<<"std::remove fail"<<errcode2str(err);
SetLastError(err);
if(DeleteFileW((LPCWSTR)newPath.replace("/","\\").unicode())==0)
{
err=GetLastError();
Start::stlog(modulefile,
"winAPI remove fail "+errcode2str(err),
0);
qDebug()<<"winAPI remove fail"<<errcode2str(err);
SetLastError(err);
return false;
}
}
}
}
QFile ofile(oldPath);
if(ofile.isOpen())
{
Start::stlog(modulefile,
"文件占用?.?",
0);
qDebug()<<"文件占用?.?";
re = false;
}else{
SetLastError(err);
Start::stlog(modulefile,
"尝试移动文件",
0);
qDebug()<<"尝试移动文件";
re = QFile::rename(oldPath,newPath);
if(!re)
{
err=GetLastError();
Start::stlog(modulefile,
"QFile::rename file "+errcode2str(err),
0);
qDebug()<<"宽字符方法";
int reint;
SetLastError(err);
reint = rename(
oldPath.replace("/","\\").toLocal8Bit().constData(),
newPath.replace("/","\\").toLocal8Bit().constData()
);
err=GetLastError();
qDebug()<<"std::rename "<<reint<<errcode2str(err);
if(reint==0)
{
re=true;
}else{
Start::stlog(modulefile,
"std::rename fail "+errcode2str(err),
0);
}
}
}
if(re)
{
Start::stlog(modulefile,
"成功--------------\r\n",
0);
}else{
Start::stlog(modulefile,
"失败--------------\r\n",
0);
}
return re;
}
bool fileIsOpen(QString filePath)
{
return false;
}
QString dp0()
{
return QCoreApplication::applicationDirPath();
}