forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.cs
More file actions
408 lines (362 loc) · 12.5 KB
/
FileUtils.cs
File metadata and controls
408 lines (362 loc) · 12.5 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using SiteServer.Utils.Enumerations;
namespace SiteServer.Utils
{
public static class FileUtils
{
public static string TryReadText(string filePath)
{
var text = string.Empty;
try
{
if (IsFileExists(filePath))
{
text = ReadText(filePath, Encoding.UTF8);
}
}
catch
{
// ignored
}
return text;
}
public static string ReadText(string filePath, ECharset charset)
{
var sr = new StreamReader(filePath, ECharsetUtils.GetEncoding(charset));
var text = sr.ReadToEnd();
sr.Close();
return text;
}
public static string ReadText(string filePath, Encoding encoding)
{
var sr = new StreamReader(filePath, encoding);
var text = sr.ReadToEnd();
sr.Close();
return text;
}
public static async Task<string> ReadTextAsync(string filePath, Encoding encoding)
{
var sr = new StreamReader(filePath, encoding);
var text = await sr.ReadToEndAsync();
sr.Close();
return text;
}
public static async Task WriteTextAsync(string filePath, Encoding encoding, string content)
{
DirectoryUtils.CreateDirectoryIfNotExists(filePath);
byte[] encodedText = encoding.GetBytes(content);
using (FileStream sourceStream = new FileStream(filePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize: 4096, useAsync: true))
{
await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
}
}
public static void WriteText(string filePath, ECharset charset, string content)
{
WriteText(filePath, ECharsetUtils.GetEncoding(charset), content);
}
public static void WriteText(string filePath, string content)
{
WriteText(filePath, Encoding.UTF8, content);
}
public static void WriteText(string filePath, Encoding encoding, string content)
{
DirectoryUtils.CreateDirectoryIfNotExists(filePath);
var file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
using (var writer = new StreamWriter(file, encoding))
{
writer.Write(content);
writer.Flush();
writer.Close();
file.Close();
}
}
public static void AppendText(string filePath, string content)
{
AppendText(filePath, Encoding.UTF8, content);
}
public static void AppendText(string filePath, ECharset charset, string content)
{
AppendText(filePath, ECharsetUtils.GetEncoding(charset), content);
}
public static void AppendText(string filePath, Encoding encoding, string content)
{
DirectoryUtils.CreateDirectoryIfNotExists(filePath);
var file = new FileStream(filePath, FileMode.Append, FileAccess.Write);
using (var writer = new StreamWriter(file, encoding))
{
writer.Write(content);
writer.Flush();
writer.Close();
file.Close();
}
}
public static async Task AppendTextAsync(string filePath, Encoding encoding, string content)
{
DirectoryUtils.CreateDirectoryIfNotExists(filePath);
var file = new FileStream(filePath, FileMode.Append, FileAccess.Write);
using (var writer = new StreamWriter(file, encoding))
{
await writer.WriteAsync(content);
writer.Flush();
writer.Close();
file.Close();
}
}
public static void RemoveReadOnlyAndHiddenIfExists(string filePath)
{
if (File.Exists(filePath))
{
var fileAttributes = File.GetAttributes(filePath);
if (IsReadOnly(fileAttributes) || IsHidden(fileAttributes))
{
File.SetAttributes(filePath, FileAttributes.Normal);
}
}
}
public static bool IsReadOnly(FileAttributes fileAttributes)
{
return ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
}
public static bool IsHidden(FileAttributes fileAttributes)
{
return (fileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden;
}
public static FileStream GetFileStreamReadOnly(string filePath)
{
return new FileStream(filePath, FileMode.Open);
}
public static string ReadBase64StringFromFile(string filePath)
{
var fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var storage = new byte[fin.Length];
fin.Read(storage, 0, storage.Length);
fin.Close();
var text = Convert.ToBase64String(storage, 0, storage.Length);
return text;
}
public static bool IsFileExists(string filePath)
{
return File.Exists(filePath);
}
public static bool DeleteFileIfExists(string filePath)
{
var retval = true;
try
{
if (IsFileExists(filePath))
{
File.Delete(filePath);
}
}
catch
{
//try
//{
// Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
// fso.DeleteFile(filePath, true);
//}
//catch
//{
// retval = false;
//}
retval = false;
}
return retval;
}
public static void DeleteFilesIfExists(string directoryPath, List<string> fileNameArrayList)
{
foreach (string fileName in fileNameArrayList)
{
var filePath = Path.Combine(directoryPath, fileName);
DeleteFileIfExists(filePath);
}
}
public static void DeleteFilesIfExists(string[] filePaths)
{
foreach (var filePath in filePaths)
{
DeleteFileIfExists(filePath);
}
}
public static bool CopyFile(string sourceFilePath, string destFilePath)
{
return CopyFile(sourceFilePath, destFilePath, true);
}
public static bool CopyFile(string sourceFilePath, string destFilePath, bool isOverride)
{
var retval = true;
try
{
DirectoryUtils.CreateDirectoryIfNotExists(destFilePath);
File.Copy(sourceFilePath, destFilePath, isOverride);
}
catch
{
retval = false;
}
return retval;
}
//public static bool MoveFile(string sourceFilePath, string destFilePath)
//{
// DirectoryUtils.CreateDirectoryIfNotExists(destFilePath);
// bool retval = true;
// try
// {
// File.Move(sourceFilePath, destFilePath);
// }
// catch
// {
// retval = false;
// }
// return retval;
//}
public static void MoveFile(string sourceFilePath, string destFilePath, bool isOverride)
{
//如果文件不存在,则进行复制。
var isExists = IsFileExists(destFilePath);
if (isOverride)
{
if (isExists)
{
DeleteFileIfExists(destFilePath);
}
CopyFile(sourceFilePath, destFilePath);
}
else if (!isExists)
{
CopyFile(sourceFilePath, destFilePath);
}
}
public static ECharset GetFileCharset(string filePath)
{
var encoding = EncodingType.GetType(filePath);
return ECharsetUtils.GetEnumType(encoding.BodyName);
}
public static string ComputeHash(string filePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
}
}
}
public class EncodingType
{
/// <summary>
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns>文件的编码类型</returns>
public static Encoding GetType(string fileName)
{
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name="fs">文件流</param>
/// <returns>文件的编码类型</returns>
public static Encoding GetType(FileStream fs)
{
var reVal = Encoding.Default;
var r = new BinaryReader(fs, Encoding.Default);
//int i;
//int.TryParse(fs.Length.ToString(), out i);
var i = TranslateUtils.ToInt(fs.Length.ToString());
var ss = r.ReadBytes(i);
if (IsUtf8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
/// <summary>
/// 判断是否是不带 BOM 的 UTF8 格式
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static bool IsUtf8Bytes(byte[] data)
{
var charByteCounter = 1; //计算当前正分析的字符应还有的字节数
foreach (var t in data)
{
var curByte = t;
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
}
public static string GetFileSizeByFilePath(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
var theFile = new FileInfo(filePath);
var fileSize = theFile.Length;
if (fileSize < 1024)
{
return fileSize.ToString() + "B";
}
else if (fileSize >= 1024 && fileSize < 1048576)
{
return (fileSize / 1024).ToString() + "KB";
}
else
{
return (fileSize / 1048576).ToString() + "MB";
}
}
else
{
return string.Empty;
}
}
}
}