-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchRule.cs
188 lines (160 loc) · 4.32 KB
/
MatchRule.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
using System;
using System.IO;
using System.Collections.Generic;
namespace RupPub
{
public abstract class MatchRule
{
bool _applyForFile = true;
/// <summary>
/// 默认为应在文件上,而不是目录。
/// </summary>
public bool ApplyForDirectory
{
get { return !_applyForFile; }
protected set { _applyForFile = !value; }
}
/// <summary>
/// 判断是否跳过目录(默认不跳过)
/// </summary>
/// <param name="di"></param>
/// <returns></returns>
public virtual bool SkipDirectory(DirectoryInfo di)
{
return false;
}
public abstract bool IsMatch(FileInfo fi);
}
public class AfterModifyTimeRule : MatchRule
{
public AfterModifyTimeRule(DateTime theTime)
{
Time = theTime;
}
public DateTime Time { get; set; }
public override bool IsMatch(FileInfo fi)
{
return fi.LastWriteTime > Time;
}
}
public class TheTimeZoneRule : MatchRule
{
public TheTimeZoneRule(DateTime beginTime, DateTime endTime)
{
BeginTime = beginTime;
EndTime = endTime;
}
public DateTime BeginTime { get; set; }
public DateTime EndTime { get; set; }
public override bool IsMatch(FileInfo fi)
{
return fi.LastWriteTime >= BeginTime && fi.LastWriteTime <= EndTime;
}
}
/// <summary>
/// 忽略的相关文件扩展名
/// </summary>
public class SkipFileExtRule : MatchRule
{
public SkipFileExtRule(string extSkipList)
{
if (string.IsNullOrEmpty(extSkipList))
{
throw new InvalidDataException("请传递有效的扩展名字符!");
}
skipExtArr = extSkipList.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
string[] skipExtArr = new string[0];
/// <summary>
/// 忽略的相关扩展名
/// </summary>
public string[] SkipExtensions
{
get { return skipExtArr; }
}
public override bool IsMatch(FileInfo fi)
{
string ext = fi.Extension;
return !Array.Exists<string>(SkipExtensions,
s => s.Equals(ext, StringComparison.InvariantCultureIgnoreCase));
}
}
/// <summary>
/// 获取特定文件扩展名的规则
/// </summary>
public class FileExtIncludeRule : MatchRule
{
public FileExtIncludeRule(string extIncludeList)
{
if (string.IsNullOrEmpty(extIncludeList))
{
throw new InvalidDataException("请传递有效的扩展名字符!");
}
includeExtArr = extIncludeList.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
string[] includeExtArr = new string[0];
/// <summary>
/// 必须包含的相关扩展名
/// </summary>
public string[] IncludeExtensions
{
get { return includeExtArr; }
}
public override bool IsMatch(FileInfo fi)
{
string ext = fi.Extension;
return Array.Exists<string>(includeExtArr, s => s.Equals(ext, StringComparison.InvariantCultureIgnoreCase));
}
}
public class SkipFolderRule : MatchRule
{
public SkipFolderRule(DirectoryInfo baseFolder, string pathSkipList)
{
ApplyForDirectory = true;
BaseFolder = baseFolder;
if (string.IsNullOrEmpty(pathSkipList))
{
throw new InvalidDataException("请传递有效的扩展名字符!");
}
skipPathArr = pathSkipList.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
public DirectoryInfo BaseFolder { get; set; }
string[] skipPathArr = new string[0];
/// <summary>
/// 忽略的相关路径名称
/// </summary>
public string[] SkipPathes
{
get { return skipPathArr; }
}
public override bool SkipDirectory(DirectoryInfo di)
{
return Array.Exists<string>(SkipPathes,
s =>
{
return di.Name.Equals(s, StringComparison.InvariantCultureIgnoreCase);
});
}
public override bool IsMatch(FileInfo fi)
{
string fullPath = fi.DirectoryName.TrimEnd(Path.DirectorySeparatorChar);
return !Array.Exists<string>(SkipPathes,
s =>
{
if (fullPath.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))
return true;
return (fullPath.IndexOf(Path.Combine(BaseFolder.FullName, s.Replace("/", "\\").TrimStart(Path.DirectorySeparatorChar)),
StringComparison.InvariantCultureIgnoreCase) != -1);
});
}
}
public class SkipHideRule : MatchRule
{
public override bool IsMatch(FileInfo fi)
{
bool isHide = (fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
//if (isHide) Console.WriteLine(fi.FullName);
return !isHide;
}
}
}