-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFileUtil.cs
605 lines (539 loc) · 19.5 KB
/
FileUtil.cs
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
using AutoIt.Common;
using DigitalPlatform.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DigitalPlatform.IO
{
#if REMOVED
public static class StreamExtension
{
// 带有 Lock/Unlock 的写入操作
public static void LockingWrite(this Stream stream,
byte[] buffer,
int offset,
int length)
{
if (stream is FileStream)
{
FileStream file = stream as FileStream;
long lock_position = 0;
if (file != null)
{
lock_position = file.Position;
file.Lock(lock_position, length);
}
try
{
stream.Write(buffer, offset, length);
}
finally
{
if (file != null)
file.Unlock(lock_position, length);
}
}
else
stream.Write(buffer, offset, length);
}
}
/// <summary>
/// File功能扩展函数
/// </summary>
public class FileUtil
{
// 将一个文本文件的内容修改为 UTF-8 编码方式
public static int ConvertGb2312TextfileToUtf8(string strFilename,
out string strError)
{
strError = "";
// 2013/10/31 如果无法通过文件头部探测出来,则不作转换
Encoding encoding = FileUtil.DetectTextFileEncoding(strFilename, null);
if (encoding == null || encoding.Equals(Encoding.UTF8) == true)
return 0;
string strContent = "";
try
{
using (StreamReader sr = new StreamReader(strFilename, encoding))
{
strContent = sr.ReadToEnd();
}
}
catch (Exception ex)
{
strError = "从文件 " + strFilename + " 读取失败: " + ex.Message;
return -1;
}
try
{
using (StreamWriter sw = new StreamWriter(strFilename, false, Encoding.UTF8))
{
sw.Write(strContent);
}
}
catch (Exception ex)
{
strError = "写入文件 " + strFilename + " 失败: " + ex.Message;
return -1;
}
return 0;
}
// 比较两个文本文件的内容是否一致
// return:
// -1 出错
// 0 两个文件内容一样
// 1 两个文件内容不一样
public static int CompareTwoTextFile(string filename1,
string filename2,
out string strError)
{
strError = "";
int nRet = ConvertGb2312TextfileToUtf8(filename1,
out strError);
if (nRet == -1)
return -1;
nRet = ConvertGb2312TextfileToUtf8(filename2,
out strError);
if (nRet == -1)
return -1;
try
{
using (StreamReader sr1 = new StreamReader(filename1, Encoding.UTF8))
using (StreamReader sr2 = new StreamReader(filename2, Encoding.UTF8))
{
string s1 = sr1.ReadToEnd();
string s2 = sr2.ReadToEnd();
if (s1 == s2)
return 0;
return 1;
}
}
catch (Exception ex)
{
strError = "从文件 " + filename1 + " 或 " + filename2 + " 读取失败: " + ex.Message;
return -1;
}
}
// return:
// 0 相同
// 其他 不同
public static int CompareTwoFile(string filename1, string filename2)
{
using (Stream s1 = File.OpenRead(filename1))
using (Stream s2 = File.OpenRead(filename2))
{
if (s1.Length != s2.Length)
return -1;
int nChunkSize = 8192;
byte[] bytes1 = new byte[nChunkSize];
byte[] bytes2 = new byte[nChunkSize];
while (true)
{
int n = s1.Read(bytes1, 0, nChunkSize);
if (n <= 0)
break;
s2.Read(bytes2, 0, n);
if (ByteArray.Compare(bytes1, bytes2, n) != 0)
return -1;
}
}
return 0;
}
public static byte[] GetFileMd5(string filename)
{
using (var md5 = MD5.Create())
{
using (var stream = File.Open(
filename,
FileMode.Open,
FileAccess.ReadWrite, // Read会造成无法打开
FileShare.ReadWrite))
{
return md5.ComputeHash(stream);
}
}
}
// 检测字符串是否为纯数字(不包含'-','.'号)
public static bool IsPureNumber(string s)
{
if (s == null)
return false;
for (int i = 0; i < s.Length; i++)
{
if (s[i] > '9' || s[i] < '0')
return false;
}
return true;
}
// 文件扩展名是否为 ._1 ._2 ...
public static bool IsBackupFile(string strFileName)
{
string strExt = Path.GetExtension(strFileName);
if (string.IsNullOrEmpty(strExt) == true)
return false;
if (strExt.StartsWith("._") == false)
return false;
string strNumber = strExt.Substring(2);
if (IsPureNumber(strNumber) == true)
return true;
return false;
}
/// <summary>
/// 获得文件的长度
/// </summary>
/// <param name="strFileName">文件名全路径</param>
/// <returns>返回文件长度。如果是 -1,表示文件不存在</returns>
public static long GetFileLength(string strFileName)
{
FileInfo fi = new FileInfo(strFileName);
if (fi.Exists == false)
return -1;
return fi.Length;
}
// 根据时间戳信息,设置文件的最后修改时间
// parameters:
// baTimeStamp 8 byte 的表示 ticks 的文件最后修改时间。应该是 GMT 时间
public static void SetFileLastWriteTimeByTimestamp(string strFilePath,
byte[] baTimeStamp)
{
if (baTimeStamp == null || baTimeStamp.Length < 8)
return;
#if NO
byte [] baTime = new byte[8];
Array.Copy(baTimeStamp,
0,
baTime,
0,
8);
#endif
long lTicks = BitConverter.ToInt64(baTimeStamp, 0);
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Exists == false)
return;
fileInfo.LastWriteTimeUtc = new DateTime(lTicks);
}
// 根据文件的最后修改时间和尺寸, 获得时间戳信息
public static byte[] GetFileTimestamp(string strFilePath)
{
byte[] baTimestamp = null;
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Exists == false)
return baTimestamp;
long lTicks = fileInfo.LastWriteTimeUtc.Ticks;
byte[] baTime = BitConverter.GetBytes(lTicks);
byte[] baLength = BitConverter.GetBytes((long)fileInfo.Length);
//Array.Reverse(baLength);
baTimestamp = new byte[baTime.Length + baLength.Length];
Array.Copy(baTime,
0,
baTimestamp,
0,
baTime.Length);
Array.Copy(baLength,
0,
baTimestamp,
baTime.Length,
baLength.Length);
return baTimestamp;
}
// 写入日志文件。每天创建一个单独的日志文件
// Exception:
// 可能会抛出异常。
// System.UnauthorizedAccessException 对路径的访问被拒绝。
public static void WriteErrorLog(
object lockObj,
string strLogDir,
string strText,
string strPrefix = "log_",
string strPostfix = ".txt")
{
lock (lockObj)
{
DateTime now = DateTime.Now;
// 每天一个日志文件
string strFilename = Path.Combine(strLogDir, strPrefix + DateTimeUtil.DateTimeToString8(now) + strPostfix);
string strTime = now.ToString();
// Exception:
// 可能会抛出异常。
// System.UnauthorizedAccessException 对路径的访问被拒绝。
StreamUtil.WriteText(strFilename,
strTime + " " + strText + "\r\n");
}
}
// 能自动识别文件内容的编码方式的读入文本文件内容模块
// parameters:
// lMaxLength 装入的最大长度。如果超过,则超过的部分不装入。如果为-1,表示不限制装入长度
// return:
// -1 出错 strError中有返回值
// 0 文件不存在 strError中有返回值
// 1 文件存在
// 2 读入的内容不是全部
public static int ReadTextFileContent(string strFilePath,
long lMaxLength,
out string strContent,
out Encoding encoding,
out string strError)
{
strError = "";
strContent = "";
encoding = null;
if (File.Exists(strFilePath) == false)
{
strError = "文件 '" + strFilePath + "' 不存在";
return 0;
}
encoding = FileUtil.DetectTextFileEncoding(strFilePath);
try
{
bool bExceed = false;
using (FileStream file = File.Open(
strFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
// TODO: 这里的自动探索文件编码方式功能不正确,
// 需要专门编写一个函数来探测文本文件的编码方式
// 目前只能用UTF-8编码方式
using (StreamReader sr = new StreamReader(file, encoding))
{
if (lMaxLength == -1)
{
strContent = sr.ReadToEnd();
}
else
{
long lLoadedLength = 0;
StringBuilder temp = new StringBuilder();
for (; ; )
{
string strLine = sr.ReadLine();
if (strLine == null)
break;
if (lLoadedLength + strLine.Length > lMaxLength)
{
strLine = strLine.Substring(0, (int)(lMaxLength - lLoadedLength));
temp.Append(strLine + " ...");
bExceed = true;
break;
}
temp.Append(strLine + "\r\n");
lLoadedLength += strLine.Length + 2;
if (lLoadedLength > lMaxLength)
{
temp.Append(strLine + " ...");
bExceed = true;
break;
}
}
strContent = temp.ToString();
}
/*
sr.Close();
sr = null;
* */
}
if (bExceed == true)
return 2;
}
catch (Exception ex)
{
strError = "打开或读入文件 '" + strFilePath + "' 时出错: " + ex.Message;
return -1;
}
return 1;
}
// 获得一个临时文件名
// parameters:
// strPostFix 如果用于表示文件扩展名,应包含点。否则可以当作一般后缀字符串使用
public static string NewTempFileName(string strDir,
string strPrefix,
string strPostFix)
{
if (string.IsNullOrEmpty(strDir) == true)
{
return Path.GetTempFileName();
}
string strFileName = "";
for (int i = 0; ; i++)
{
strFileName = PathUtil.MergePath(strDir, strPrefix + Convert.ToString(i) + strPostFix);
FileInfo fi = new FileInfo(strFileName);
if (fi.Exists == false)
{
// 创建一个0 byte的文件
using (FileStream f = File.Create(strFileName))
{
}
return strFileName;
}
}
}
// 把一个byte[] 写到指定的文件
// parameter:
// strFileName: 文件名,每次新创建,覆盖原来的文件
// data: byte数组
// 编写者: 任延华
public static void WriteByteArray(string strFileName,
byte[] data)
{
using (FileStream s = File.Create(strFileName))
{
s.Write(data,
0,
data.Length);
}
}
// 改动文件的“最后修改时间”属性
// 注意可能抛出异常
public static void ChangeFileLastModifyTimeUtc(string strPath,
string strTime)
{
FileInfo fi = new FileInfo(strPath);
DateTime time = DateTimeUtil.FromRfc1123DateTimeString(strTime);
fi.LastWriteTimeUtc = time;
fi.CreationTimeUtc = time;
}
// 文件是否存在并且是非空
public static bool IsFileExsitAndNotNull(string strFilename)
{
// TODO: 可以用 FileInfo 改写
try
{
using (FileStream file = File.Open(
strFilename,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
if (file.Length == 0)
return false;
return true;
}
}
catch (FileNotFoundException)
{
return false;
}
}
// 文件是否存在?
public static bool FileExist(string strFilePath)
{
FileInfo fi = new FileInfo(strFilePath);
return fi.Exists;
}
// 功能:将一个字符串写到文件,当文件不存在时,创建文件;存在,覆盖文件
// paramter:
// strContent: 字符串
// strFileName: 文件名
// 编写者:任延华
public static void String2File(string strContent,
string strFileName)
{
using (StreamWriter s = File.CreateText(strFileName))
{
s.Write(strContent);
}
}
// 功能:文件到字符串,使用直接读到尾的方法
// strFileName: 文件名
public static string File2StringE(string strFileName)
{
if (strFileName == null
|| strFileName == "")
return "";
using (StreamReader sr = new StreamReader(strFileName, true))
{
string strText = sr.ReadToEnd();
return strText;
}
}
// 如果未能探测出来,则当作 936
public static Encoding DetectTextFileEncoding(string strFilename)
{
Encoding encoding = DetectTextFileEncoding(strFilename, null);
if (encoding == null)
return Encoding.GetEncoding(936); // default
return encoding;
}
// 检测文本文件的encoding
/*
UTF-8: EF BB BF
UTF-16 big-endian byte order: FE FF
UTF-16 little-endian byte order: FF FE
UTF-32 big-endian byte order: 00 00 FE FF
UTF-32 little-endian byte order: FF FE 00 00
* */
public static Encoding DetectTextFileEncoding(string strFilename,
Encoding default_encoding)
{
byte[] buffer = new byte[4];
try
{
using (FileStream file = File.Open(
strFilename,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
if (file.Length >= 2)
{
file.Read(buffer, 0, 2); // 1, 2 BUG
if (buffer[0] == 0xff && buffer[1] == 0xfe)
{
return Encoding.Unicode; // little-endian
}
if (buffer[0] == 0xfe && buffer[1] == 0xff)
{
return Encoding.BigEndianUnicode;
}
}
if (file.Length >= 3)
{
file.Read(buffer, 2, 1);
if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
{
return Encoding.UTF8;
}
}
if (file.Length >= 4)
{
file.Read(buffer, 3, 1);
// UTF-32 big-endian byte order: 00 00 FE FF
// UTF-32 little-endian byte order: FF FE 00 00
if (buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
return Encoding.UTF32; // little-endian
}
if (buffer[0] == 0xff && buffer[1] == 0xfe && buffer[2] == 0x00 && buffer[3] == 0x00)
{
return Encoding.GetEncoding(65006); // UTF-32 big-endian
}
}
// 2018/11/6
// 检测是不是没有 BOM 的 UTF-8
{
byte[] temp_buffer = new byte[4096];
file.Seek(0, SeekOrigin.Begin);
int length = file.Read(temp_buffer, 0, temp_buffer.Length);
TextEncodingDetect detector = new TextEncodingDetect();
TextEncodingDetect.Encoding encoding = detector.DetectEncoding(temp_buffer, length);
switch(encoding)
{
case TextEncodingDetect.Encoding.Utf8Bom:
case TextEncodingDetect.Encoding.Utf8Nobom:
return Encoding.UTF8;
}
}
}
}
catch
{
}
return default_encoding; // default
}
}
#endif
}