| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | namespace ICSharpCode.SharpZipLib.Core |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// PathFilter filters directories and files using a form of <see cref="System.Text.RegularExpressions.Regex">regular |
| | | 8 | | /// by full path name. |
| | | 9 | | /// See <see cref="NameFilter">NameFilter</see> for more detail on filtering. |
| | | 10 | | /// </summary> |
| | | 11 | | public class PathFilter : IScanFilter |
| | | 12 | | { |
| | | 13 | | #region Constructors |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initialise a new instance of <see cref="PathFilter"></see>. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="filter">The <see cref="NameFilter">filter</see> expression to apply.</param> |
| | 8 | 18 | | public PathFilter(string filter) |
| | | 19 | | { |
| | 8 | 20 | | nameFilter_ = new NameFilter(filter); |
| | 8 | 21 | | } |
| | | 22 | | #endregion |
| | | 23 | | |
| | | 24 | | #region IScanFilter Members |
| | | 25 | | /// <summary> |
| | | 26 | | /// Test a name to see if it matches the filter. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="name">The name to test.</param> |
| | | 29 | | /// <returns>True if the name matches, false otherwise.</returns> |
| | | 30 | | /// <remarks><see cref="Path.GetFullPath(string)"/> is used to get the full path before matching.</remarks> |
| | | 31 | | public virtual bool IsMatch(string name) |
| | | 32 | | { |
| | 14147 | 33 | | bool result = false; |
| | | 34 | | |
| | 14147 | 35 | | if (name != null) { |
| | 14147 | 36 | | string cooked = (name.Length > 0) ? Path.GetFullPath(name) : ""; |
| | 14147 | 37 | | result = nameFilter_.IsMatch(cooked); |
| | | 38 | | } |
| | 14147 | 39 | | return result; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | readonly |
| | | 43 | | #endregion |
| | | 44 | | |
| | | 45 | | #region Instance Fields |
| | | 46 | | NameFilter nameFilter_; |
| | | 47 | | #endregion |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// ExtendedPathFilter filters based on name, file size, and the last write time of the file. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <remarks>Provides an example of how to customise filtering.</remarks> |
| | | 54 | | public class ExtendedPathFilter : PathFilter |
| | | 55 | | { |
| | | 56 | | #region Constructors |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initialise a new instance of ExtendedPathFilter. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="filter">The filter to apply.</param> |
| | | 61 | | /// <param name="minSize">The minimum file size to include.</param> |
| | | 62 | | /// <param name="maxSize">The maximum file size to include.</param> |
| | | 63 | | public ExtendedPathFilter(string filter, |
| | | 64 | | long minSize, long maxSize) |
| | | 65 | | : base(filter) |
| | | 66 | | { |
| | | 67 | | MinSize = minSize; |
| | | 68 | | MaxSize = maxSize; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Initialise a new instance of ExtendedPathFilter. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="filter">The filter to apply.</param> |
| | | 75 | | /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param> |
| | | 76 | | /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param> |
| | | 77 | | public ExtendedPathFilter(string filter, |
| | | 78 | | DateTime minDate, DateTime maxDate) |
| | | 79 | | : base(filter) |
| | | 80 | | { |
| | | 81 | | MinDate = minDate; |
| | | 82 | | MaxDate = maxDate; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Initialise a new instance of ExtendedPathFilter. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="filter">The filter to apply.</param> |
| | | 89 | | /// <param name="minSize">The minimum file size to include.</param> |
| | | 90 | | /// <param name="maxSize">The maximum file size to include.</param> |
| | | 91 | | /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param> |
| | | 92 | | /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param> |
| | | 93 | | public ExtendedPathFilter(string filter, |
| | | 94 | | long minSize, long maxSize, |
| | | 95 | | DateTime minDate, DateTime maxDate) |
| | | 96 | | : base(filter) |
| | | 97 | | { |
| | | 98 | | MinSize = minSize; |
| | | 99 | | MaxSize = maxSize; |
| | | 100 | | MinDate = minDate; |
| | | 101 | | MaxDate = maxDate; |
| | | 102 | | } |
| | | 103 | | #endregion |
| | | 104 | | |
| | | 105 | | #region IScanFilter Members |
| | | 106 | | /// <summary> |
| | | 107 | | /// Test a filename to see if it matches the filter. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="name">The filename to test.</param> |
| | | 110 | | /// <returns>True if the filter matches, false otherwise.</returns> |
| | | 111 | | /// <exception cref="System.IO.FileNotFoundException">The <see paramref="fileName"/> doesnt exist</exception> |
| | | 112 | | public override bool IsMatch(string name) |
| | | 113 | | { |
| | | 114 | | bool result = base.IsMatch(name); |
| | | 115 | | |
| | | 116 | | if (result) { |
| | | 117 | | var fileInfo = new FileInfo(name); |
| | | 118 | | result = |
| | | 119 | | (MinSize <= fileInfo.Length) && |
| | | 120 | | (MaxSize >= fileInfo.Length) && |
| | | 121 | | (MinDate <= fileInfo.LastWriteTime) && |
| | | 122 | | (MaxDate >= fileInfo.LastWriteTime) |
| | | 123 | | ; |
| | | 124 | | } |
| | | 125 | | return result; |
| | | 126 | | } |
| | | 127 | | #endregion |
| | | 128 | | |
| | | 129 | | #region Properties |
| | | 130 | | /// <summary> |
| | | 131 | | /// Get/set the minimum size/length for a file that will match this filter. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <remarks>The default value is zero.</remarks> |
| | | 134 | | /// <exception cref="ArgumentOutOfRangeException">value is less than zero; greater than <see cref="MaxSize"/></excep |
| | | 135 | | public long MinSize { |
| | | 136 | | get { return minSize_; } |
| | | 137 | | set { |
| | | 138 | | if ((value < 0) || (maxSize_ < value)) { |
| | | 139 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | minSize_ = value; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Get/set the maximum size/length for a file that will match this filter. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <remarks>The default value is <see cref="System.Int64.MaxValue"/></remarks> |
| | | 150 | | /// <exception cref="ArgumentOutOfRangeException">value is less than zero or less than <see cref="MinSize"/></except |
| | | 151 | | public long MaxSize { |
| | | 152 | | get { return maxSize_; } |
| | | 153 | | set { |
| | | 154 | | if ((value < 0) || (minSize_ > value)) { |
| | | 155 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | maxSize_ = value; |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Get/set the minimum <see cref="DateTime"/> value that will match for this filter. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <remarks>Files with a LastWrite time less than this value are excluded by the filter.</remarks> |
| | | 166 | | public DateTime MinDate { |
| | | 167 | | get { |
| | | 168 | | return minDate_; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | set { |
| | | 172 | | if (value > maxDate_) { |
| | | 173 | | throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MaxDate"); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | minDate_ = value; |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Get/set the maximum <see cref="DateTime"/> value that will match for this filter. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <remarks>Files with a LastWrite time greater than this value are excluded by the filter.</remarks> |
| | | 184 | | public DateTime MaxDate { |
| | | 185 | | get { |
| | | 186 | | return maxDate_; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | set { |
| | | 190 | | if (minDate_ > value) { |
| | | 191 | | throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MinDate"); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | maxDate_ = value; |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | #endregion |
| | | 198 | | |
| | | 199 | | #region Instance Fields |
| | | 200 | | long minSize_; |
| | | 201 | | long maxSize_ = long.MaxValue; |
| | | 202 | | DateTime minDate_ = DateTime.MinValue; |
| | | 203 | | DateTime maxDate_ = DateTime.MaxValue; |
| | | 204 | | #endregion |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// NameAndSizeFilter filters based on name and file size. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <remarks>A sample showing how filters might be extended.</remarks> |
| | | 211 | | [Obsolete("Use ExtendedPathFilter instead")] |
| | | 212 | | public class NameAndSizeFilter : PathFilter |
| | | 213 | | { |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Initialise a new instance of NameAndSizeFilter. |
| | | 217 | | /// </summary> |
| | | 218 | | /// <param name="filter">The filter to apply.</param> |
| | | 219 | | /// <param name="minSize">The minimum file size to include.</param> |
| | | 220 | | /// <param name="maxSize">The maximum file size to include.</param> |
| | | 221 | | public NameAndSizeFilter(string filter, long minSize, long maxSize) |
| | | 222 | | : base(filter) |
| | | 223 | | { |
| | | 224 | | MinSize = minSize; |
| | | 225 | | MaxSize = maxSize; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Test a filename to see if it matches the filter. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="name">The filename to test.</param> |
| | | 232 | | /// <returns>True if the filter matches, false otherwise.</returns> |
| | | 233 | | public override bool IsMatch(string name) |
| | | 234 | | { |
| | | 235 | | bool result = base.IsMatch(name); |
| | | 236 | | |
| | | 237 | | if (result) { |
| | | 238 | | var fileInfo = new FileInfo(name); |
| | | 239 | | long length = fileInfo.Length; |
| | | 240 | | result = |
| | | 241 | | (MinSize <= length) && |
| | | 242 | | (MaxSize >= length); |
| | | 243 | | } |
| | | 244 | | return result; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Get/set the minimum size for a file that will match this filter. |
| | | 249 | | /// </summary> |
| | | 250 | | public long MinSize { |
| | | 251 | | get { return minSize_; } |
| | | 252 | | set { |
| | | 253 | | if ((value < 0) || (maxSize_ < value)) { |
| | | 254 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | minSize_ = value; |
| | | 258 | | } |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Get/set the maximum size for a file that will match this filter. |
| | | 263 | | /// </summary> |
| | | 264 | | public long MaxSize { |
| | | 265 | | get { return maxSize_; } |
| | | 266 | | set { |
| | | 267 | | if ((value < 0) || (minSize_ > value)) { |
| | | 268 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | maxSize_ = value; |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | #region Instance Fields |
| | | 276 | | long minSize_; |
| | | 277 | | long maxSize_ = long.MaxValue; |
| | | 278 | | #endregion |
| | | 279 | | } |
| | | 280 | | } |