// PathFilter.cs // // Copyright 2005 John Reilly // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // Linking this library statically or dynamically with other modules is // making a combined work based on this library. Thus, the terms and // conditions of the GNU General Public License cover the whole // combination. // // As a special exception, the copyright holders of this library give you // permission to link this library with independent modules to produce an // executable, regardless of the license terms of these independent // modules, and to copy and distribute the resulting executable under // terms of your choice, provided that you also meet, for each linked // independent module, the terms and conditions of the license of that // module. An independent module is a module which is not derived from // or based on this library. If you modify this library, you may extend // this exception to your version of the library, but you are not // obligated to do so. If you do not wish to do so, delete this // exception statement from your version. using System; using System.IO; namespace ICSharpCode.SharpZipLib.Core { /// /// PathFilter filters directories and files using a form of regular expressions /// by full path name. /// See NameFilter for more detail on filtering. /// public class PathFilter : IScanFilter { #region Constructors /// /// Initialise a new instance of . /// /// The filter expression to apply. public PathFilter(string filter) { nameFilter_ = new NameFilter(filter); } #endregion #region IScanFilter Members /// /// Test a name to see if it matches the filter. /// /// The name to test. /// True if the name matches, false otherwise. /// is used to get the full path before matching. public virtual bool IsMatch(string name) { bool result = false; if ( name != null ) { string cooked = (name.Length > 0) ? Path.GetFullPath(name) : ""; result = nameFilter_.IsMatch(cooked); } return result; } #endregion #region Instance Fields NameFilter nameFilter_; #endregion } /// /// ExtendedPathFilter filters based on name, file size, and the last write time of the file. /// /// Provides an example of how to customise filtering. public class ExtendedPathFilter : PathFilter { #region Constructors /// /// Initialise a new instance of ExtendedPathFilter. /// /// The filter to apply. /// The minimum file size to include. /// The maximum file size to include. public ExtendedPathFilter(string filter, long minSize, long maxSize) : base(filter) { MinSize = minSize; MaxSize = maxSize; } /// /// Initialise a new instance of ExtendedPathFilter. /// /// The filter to apply. /// The minimum to include. /// The maximum to include. public ExtendedPathFilter(string filter, DateTime minDate, DateTime maxDate) : base(filter) { MinDate = minDate; MaxDate = maxDate; } /// /// Initialise a new instance of ExtendedPathFilter. /// /// The filter to apply. /// The minimum file size to include. /// The maximum file size to include. /// The minimum to include. /// The maximum to include. public ExtendedPathFilter(string filter, long minSize, long maxSize, DateTime minDate, DateTime maxDate) : base(filter) { MinSize = minSize; MaxSize = maxSize; MinDate = minDate; MaxDate = maxDate; } #endregion #region IScanFilter Members /// /// Test a filename to see if it matches the filter. /// /// The filename to test. /// True if the filter matches, false otherwise. /// The doesnt exist public override bool IsMatch(string name) { bool result = base.IsMatch(name); if ( result ) { FileInfo fileInfo = new FileInfo(name); result = (MinSize <= fileInfo.Length) && (MaxSize >= fileInfo.Length) && (MinDate <= fileInfo.LastWriteTime) && (MaxDate >= fileInfo.LastWriteTime) ; } return result; } #endregion #region Properties /// /// Get/set the minimum size/length for a file that will match this filter. /// /// The default value is zero. /// value is less than zero; greater than public long MinSize { get { return minSize_; } set { if ( (value < 0) || (maxSize_ < value) ) { throw new ArgumentOutOfRangeException("value"); } minSize_ = value; } } /// /// Get/set the maximum size/length for a file that will match this filter. /// /// The default value is /// value is less than zero or less than public long MaxSize { get { return maxSize_; } set { if ( (value < 0) || (minSize_ > value) ) { throw new ArgumentOutOfRangeException("value"); } maxSize_ = value; } } /// /// Get/set the minimum value that will match for this filter. /// /// Files with a LastWrite time less than this value are excluded by the filter. public DateTime MinDate { get { return minDate_; } set { if ( value > maxDate_ ) { #if NETCF_1_0 throw new ArgumentOutOfRangeException("value"); #else throw new ArgumentOutOfRangeException("value", "Exceeds MaxDate"); #endif } minDate_ = value; } } /// /// Get/set the maximum value that will match for this filter. /// /// Files with a LastWrite time greater than this value are excluded by the filter. public DateTime MaxDate { get { return maxDate_; } set { if ( minDate_ > value ) { #if NETCF_1_0 throw new ArgumentOutOfRangeException("value"); #else throw new ArgumentOutOfRangeException("value", "Exceeds MinDate"); #endif } maxDate_ = value; } } #endregion #region Instance Fields long minSize_; long maxSize_ = long.MaxValue; DateTime minDate_ = DateTime.MinValue; DateTime maxDate_ = DateTime.MaxValue; #endregion } /// /// NameAndSizeFilter filters based on name and file size. /// /// A sample showing how filters might be extended. [Obsolete("Use ExtendedPathFilter instead")] public class NameAndSizeFilter : PathFilter { /// /// Initialise a new instance of NameAndSizeFilter. /// /// The filter to apply. /// The minimum file size to include. /// The maximum file size to include. public NameAndSizeFilter(string filter, long minSize, long maxSize) : base(filter) { MinSize = minSize; MaxSize = maxSize; } /// /// Test a filename to see if it matches the filter. /// /// The filename to test. /// True if the filter matches, false otherwise. public override bool IsMatch(string name) { bool result = base.IsMatch(name); if ( result ) { FileInfo fileInfo = new FileInfo(name); long length = fileInfo.Length; result = (MinSize <= length) && (MaxSize >= length); } return result; } /// /// Get/set the minimum size for a file that will match this filter. /// public long MinSize { get { return minSize_; } set { if ( (value < 0) || (maxSize_ < value) ) { throw new ArgumentOutOfRangeException("value"); } minSize_ = value; } } /// /// Get/set the maximum size for a file that will match this filter. /// public long MaxSize { get { return maxSize_; } set { if ( (value < 0) || (minSize_ > value) ) { throw new ArgumentOutOfRangeException("value"); } maxSize_ = value; } } #region Instance Fields long minSize_; long maxSize_ = long.MaxValue; #endregion } }