Exemple #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();
                int index = 0;
				while (continueRunning_ && enumerator.MoveNext())
				{
					ZipEntry entry = (ZipEntry)enumerator.Current;
					if (entry.IsFile)
					{
						if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name))
						{
							if (events_ == null || events_.OnProcessFile(entry.Name))
							{
								ExtractEntry(entry);
							}
						}
					}
					else if (entry.IsDirectory)
					{
						if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories)
						{
							if (events_ == null || events_.OnProcessDirectory(entry.Name, true))
							{
								ExtractEntry(entry);
							}
						}
					}
					else
					{
						// Do nothing for volume labels etc...
                    }
                    if (events_.TotalProgress != null)
                        events_.TotalProgress(this, new ProgressEventArgs(entry.Name, ++index, zipFile_.Count));
				}
			}
		}
Exemple #2
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;
            this.overwrite = overwrite;
            this.confirmDelegate = confirmDelegate;
            this.targetDirectory = targetDirectory;
            this.fileFilter = new NameFilter(fileFilter);
            this.directoryFilter = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract = restoreDateTime;

            using (zipFile = new ZipFile(zipFileName))
            {
            #if !NETCF_1_0
                if (password != null)
                {
                    zipFile.Password = password;
                }
            #endif

                IEnumerator enumerator = zipFile.GetEnumerator();
                while (continueRunning && enumerator.MoveNext())
                {
                    var entry = (ZipEntry) enumerator.Current;
                    if (entry.IsFile)
                    {
                        if (this.directoryFilter.IsMatch(Path.GetDirectoryName(entry.Name)) &&
                            this.fileFilter.IsMatch(entry.Name))
                        {
                            if (events == null || events.OnProcessFile(entry.Name))
                            {
                                ExtractEntry(entry);
                            }
                        }
                    }
                    else if (entry.IsDirectory)
                    {
                        if (this.directoryFilter.IsMatch(entry.Name) && CreateEmptyDirectories)
                        {
                            if (events == null || events.OnProcessDirectory(entry.Name, true))
                            {
                                ExtractEntry(entry);
                            }
                        }
                    }
                }
            }
        }