Пример #1
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();
			}
		}
Пример #2
0
        public void Extract(string path)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(_filename));
            ZipEntry ze;

            while ((ze = s.GetNextEntry()) != null)
            {
                string dirName = Path.GetDirectoryName(ze.Name);
                string fileName = Path.GetFileName(ze.Name);

                if (ze.IsFile)
                {
                    if (!Directory.Exists(path + "\\" + dirName))
                    {
                        Directory.CreateDirectory(path + "\\" + dirName);
                    }

                    FileStream streamWriter = File.Create(path + "\\" + dirName + "\\" + fileName);

                    if (ze.Size > 0)
                    {
                        int size = 2048;
                        byte[] data = new byte[size];

                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    streamWriter.Close();
                }
            }

            s.Close();
        }