NameFilter is a string matching class which allows for both positive and negative matching. A filter is a sequence of independant regular expressions separated by semi-colons ';' Each expression can be prefixed by a plus '+' sign or a minus '-' sign to denote the expression is intended to include or exclude names. If neither a plus or minus sign is found include is the default A given name is tested for inclusion before checking exclusions. Only names matching an include spec and not matching an exclude spec are deemed to match the filter. An empty filter matches any name.
Example #1
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, 
							   Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, 
							   string fileFilter, string directoryFilter, bool restoreDateTime)
        {
            if ( (overwrite == Overwrite.Prompt) && (confirmDelegate == null) ) {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            targetDirectory_ = targetDirectory;
            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using ( zipFile_ = new ZipFile(zipFileName) ) {

            #if !NETCF_1_0
                if (password_ != null) {
                    zipFile_.Password = password_;
                }
            #endif

                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while ( continueRunning_ && enumerator.MoveNext()) {
                    ZipEntry entry = (ZipEntry) enumerator.Current;
                    if ( entry.IsFile )
                    {
                        if ( directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name) ) {
                            ExtractEntry(entry);
                        }
                    }
                    else if ( entry.IsDirectory ) {
                        if ( directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories ) {
                            ExtractEntry(entry);
                        }
                    }
                    else {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #2
0
		/// <summary>
		/// Initialise a new instance of <see cref="PathFilter"></see>.
		/// </summary>
		/// <param name="filter">The <see cref="NameFilter">filter</see> expression to apply.</param>
		public PathFilter(string filter)
		{
			nameFilter_ = new NameFilter(filter);
		}
        /// <summary>
        /// Extract the contents of a zip file held in a stream.
        /// </summary>
        /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
        /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param>
        public void ExtractZip(Stream inputStream, string targetDirectory,
                       Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
                       string fileFilter, string directoryFilter, bool restoreDateTime,
                       bool isStreamOwner)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using (zipFile_ = new ZipFile(inputStream)) {

#if !NETCF_1_0
                if (password_ != null) {
                    zipFile_.Password = password_;
                }
#endif
                zipFile_.IsStreamOwner = isStreamOwner;
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext()) {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory) {
                        if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) {
                            ExtractEntry(entry);
                        }
                    }
                    else {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #4
0
        // nupkg file %-encodes zip entry names. This method decodes entry names before writing to disk.
        // We must do this, or PathTooLongException may be thrown for some unicode entry names.
        public static void ExtractZipDecoded(string zipFilePath, string outFolder, string directoryFilter = null)
        {
            var zf = new ZipFile(zipFilePath);

            foreach (ZipEntry zipEntry in zf) {
                if (!zipEntry.IsFile) continue;

                var entryFileName = Uri.UnescapeDataString(zipEntry.Name);

                var buffer = new byte[4096];
                var zipStream = zf.GetInputStream(zipEntry);

                var fullZipToPath = Path.Combine(outFolder, entryFileName);
                var directoryName = Path.GetDirectoryName(fullZipToPath);
                var directoryFilter_ = new NameFilter(directoryFilter);
                if (directoryFilter_.IsMatch(directoryName)) {
                    if (directoryName.Length > 0) {
                        Directory.CreateDirectory(directoryName);
                    }

                    using (FileStream streamWriter = File.Create(fullZipToPath)) {
                        StreamUtils.Copy(zipStream, streamWriter, buffer);
                    }
                }
            }
            zf.Close();
        }
Example #5
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter, string directoryFilter)
        {
            continueRunning_ = true;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);

            using (zipFile_ = new ZipFile(zipFileName))
            {
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    var entry = (ZipEntry) enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) &&
                            fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, 
            Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
            string fileFilter, string directoryFilter, bool restoreDateTime)
        {
            if ( (overwrite == Overwrite.Prompt) && (confirmDelegate == null) ) {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            targetDirectory_ = targetDirectory;
            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using ( inputStream_ = new ZipInputStream(File.OpenRead(zipFileName)) ) {
                if (password_ != null) {
                    inputStream_.Password = password_;
                }

                ZipEntry entry;
                while ( continueRunning_ && (entry = inputStream_.GetNextEntry()) != null ) {
                    if ( directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name) ) {
                        ExtractEntry(entry);
                    }
                }
            }
        }
Example #7
0
 public void NullFilter()
 {
     var nf = new NameFilter(null);
     Assert.IsTrue(nf.IsIncluded("o78i6bgv5rvu\\kj//&*"));
 }
Example #8
0
 public void ExtractZip(Stream inputStream, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime, bool isStreamOwner)
 {
     if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null))
     {
         throw new ArgumentNullException("confirmDelegate");
     }
     this.continueRunning_ = true;
     this.overwrite_ = overwrite;
     this.confirmDelegate_ = confirmDelegate;
     this.extractNameTransform_ = new WindowsNameTransform(targetDirectory);
     this.fileFilter_ = new NameFilter(fileFilter);
     this.directoryFilter_ = new NameFilter(directoryFilter);
     this.restoreDateTimeOnExtract_ = restoreDateTime;
     using (this.zipFile_ = new ZipFile(inputStream))
     {
         if (this.password_ != null)
         {
             this.zipFile_.Password = this.password_;
         }
         this.zipFile_.IsStreamOwner = isStreamOwner;
         IEnumerator enumerator = this.zipFile_.GetEnumerator();
         while (this.continueRunning_ && enumerator.MoveNext())
         {
             ZipEntry current = (ZipEntry) enumerator.Current;
             if (current.IsFile)
             {
                 if (this.directoryFilter_.IsMatch(Path.GetDirectoryName(current.Name)) && this.fileFilter_.IsMatch(current.Name))
                 {
                     this.ExtractEntry(current);
                 }
             }
             else if ((current.IsDirectory && this.directoryFilter_.IsMatch(current.Name)) && this.CreateEmptyDirectories)
             {
                 this.ExtractEntry(current);
             }
         }
     }
 }
Example #9
0
 public PathFilter(string filter)
 {
     nameFilter_ = new NameFilter(filter);
 }
        public static async Task ExtractZipForInstall(string zipFilePath, string outFolder)
        {
            var zf = new ZipFile(zipFilePath);
            var directoryFilter = new NameFilter("lib");
            var entries = zf.OfType<ZipEntry>().ToArray();
            var re = new Regex(@"lib[\\\/][^\\\/]*[\\\/]", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

            try {
                await Utility.ForEachAsync(entries, (zipEntry) => {
                    if (!zipEntry.IsFile) return;

                    var entryFileName = Uri.UnescapeDataString(zipEntry.Name);
                    var fullZipToPath = Path.Combine(outFolder, entryFileName);

                    var directoryName = Path.GetDirectoryName(fullZipToPath);

                    if (!directoryFilter.IsMatch(directoryName)) return;
                    fullZipToPath = re.Replace(fullZipToPath, "", 1);
                    directoryName = re.Replace(directoryName, "", 1);

                    var buffer = new byte[64*1024];

                    if (directoryName.Length > 0) {
                        Utility.Retry(() => Directory.CreateDirectory(directoryName), 2);
                    }

                    Utility.Retry(() => {
                        using (var zipStream = zf.GetInputStream(zipEntry))
                        using (FileStream streamWriter = File.Create(fullZipToPath)) {
                            StreamUtils.Copy(zipStream, streamWriter, buffer);
                        }
                    }, 5);
                }, 4);
            } finally {
                zf.Close();
            }
        }
 /// <summary>
 ///   Initialise a new instance of <see cref = "PathFilter"></see>.
 /// </summary>
 /// <param name = "filter">The <see cref = "NameFilter">filter</see> expression to apply.</param>
 public IsolatedPathFilter(string filter)
 {
     nameFilter_ = new NameFilter(filter);
 }
Example #12
0
        /// <summary>
        /// Exatract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, 
		                       Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, 
		                       string fileFilter, string directoryFilter)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) {
                throw new ArgumentNullException("confirmDelegate");
            }
            this.overwrite = overwrite;
            this.confirmDelegate = confirmDelegate;
            this.targetDirectory = targetDirectory;
            this.fileFilter = new NameFilter(fileFilter);
            this.directoryFilter = new NameFilter(directoryFilter);

            inputStream = new ZipInputStream(File.OpenRead(zipFileName));

            try {

                if (password != null) {
                    inputStream.Password = password;
                }

                ZipEntry entry;
                while ( (entry = inputStream.GetNextEntry()) != null ) {
                    if ( this.directoryFilter.IsMatch(Path.GetDirectoryName(entry.Name)) && this.fileFilter.IsMatch(entry.Name) ) {
                        ExtractEntry(entry);
                    }
                }
            }
            finally {
                inputStream.Close();
            }
        }