⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zf.cs

📁 SharpZipLib之前叫做NZipLib
💻 CS
📖 第 1 页 / 共 3 页
字号:
			{
				zipFileName = Path.ChangeExtension(zipFileName, ".zip");
			}

			try
			{
				using (ZipFile zipFile = new ZipFile(zipFileName))
				{
					zipFile.BeginUpdate();
					for ( int i = 1; i < fileSpecs.Count; ++i )
					{
						zipFile.Delete((string)fileSpecs[i]);
					}
					zipFile.CommitUpdate();
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine("Problem deleting files - '{0}'", ex.Message);
			}
		}

		#endregion
		#region Adding
		/// <summary>
		/// Callback for adding a new file.
		/// </summary>
		/// <param name="sender">The scanner calling this delegate.</param>
		/// <param name="args">The event arguments.</param>
		void ProcessFile(object sender, ScanEventArgs args)
		{
			if ( !silent_ )
			{
				Console.WriteLine(args.Name);
			}
			activeZipFile_.Add(args.Name);
		}

		/// <summary>
		/// Callback for adding a new directory.
		/// </summary>
		/// <param name="sender">The scanner calling this delegate.</param>
		/// <param name="args">The event arguments.</param>
		/// <remarks>Directories are only added if they are empty and
		/// the user has specified that empty directories are to be added.</remarks>
		void ProcessDirectory(object sender, DirectoryEventArgs args)
		{
			if ( !args.HasMatchingFiles && addEmptyDirectoryEntries_ )
			{
				activeZipFile_.AddDirectory(args.Name);
			}
		}

		/// <summary>
		/// Add files to an archive
		/// </summary>
		/// <param name="fileSpecs">The specification for files to add.</param>
		void Add(ArrayList fileSpecs)
		{
			string zipFileName = fileSpecs[0] as string;
			if (Path.GetExtension(zipFileName).Length == 0) 
			{
				zipFileName = Path.ChangeExtension(zipFileName, ".zip");
			}

			fileSpecs.RemoveAt(0);

			ZipFile zipFile;

			try
			{
				if ( File.Exists(zipFileName) )
				{
					zipFile = new ZipFile(zipFileName);
				}
				else
				{
					zipFile = ZipFile.Create(zipFileName);
				}

				using (zipFile)
				{
					zipFile.Password = password_;
					zipFile.UseZip64 = useZip64_;
					
					zipFile.BeginUpdate();

					activeZipFile_ = zipFile;

					foreach (string spec in fileSpecs)
					{
						string path = Path.GetDirectoryName(Path.GetFullPath(spec));
						string fileSpec = Path.GetFileName(spec);

						zipFile.NameTransform = new ZipNameTransform(path);

						FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
						scanner.ProcessFile = new ProcessFileDelegate(ProcessFile);
						scanner.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
						scanner.Scan(path, recursive_);
					}
					zipFile.CommitUpdate();
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine("Problem adding to archive - '{0}'", ex.Message);
			}
		}

		#endregion
		#region Class Execute Command
		/// <summary>
		/// Parse command line arguments and 'execute' them.
		/// </summary>		
		void Execute(string[] args) 
		{
			if (SetArgs(args)) 
			{
				if (fileSpecs_.Count == 0) 
				{
					if (!silent_) 
					{
						Console.Out.WriteLine("Nothing to do");
					}
				}
				else 
				{
					switch (operation_) 
					{
						case Operation.List:
							List(fileSpecs_);
							break;
						
						case Operation.Create:
							Create(fileSpecs_);
							break;
						
						case Operation.Extract:
							Extract(fileSpecs_);
							break;

						case Operation.Delete:
							Delete(fileSpecs_);
							break;

						case Operation.Add:
							Add(fileSpecs_);
							break;

						case Operation.Test:
							Test(fileSpecs_);
							break;
					}
				}
			} 
			else 
			{
				if ( !silent_ ) 
				{
					ShowHelp();
				}
			}
		}
		
		#endregion
		#region Support Routines
		byte[] GetBuffer()
		{
			if ( buffer_ == null )
			{
				buffer_ = new byte[bufferSize_];
			}

			return buffer_;
		}
		#endregion
		#region Static support routines
		///<summary>
		/// Calculate compression ratio as a percentage
		/// Doesnt allow for expansion (ratio > 100) as the resulting strings can get huge easily
		/// </summary>
		static int GetCompressionRatio(long packedSize, long unpackedSize)
		{
			int result = 0;
			if ( (unpackedSize > 0) && (unpackedSize >= packedSize) )
			{
				result = (int) Math.Round((1.0 - ((double)packedSize / (double)unpackedSize)) * 100.0);
			}
			return result;
		}

		/// <summary>
		/// Interpret attributes in conjunction with operatingSystem
		/// </summary>
		/// <param name="operatingSystem">The operating system.</param>
		/// <param name="attributes">The external attributes.</param>
		/// <returns>A string representation of the attributres passed.</returns>
		static string InterpretExternalAttributes(int operatingSystem, int attributes)
		{
			string result = string.Empty;
			if ((operatingSystem == 0) || (operatingSystem == 10))
			{
				if ((attributes & 0x10) != 0)
					result = result + "D";
				else
					result = result + "-";

				if ((attributes & 0x08) != 0)
					result = result + "V";
				else
					result = result + "-";

				if ((attributes & 0x01) != 0)
					result = result + "r";
				else
					result = result + "-";

				if ((attributes & 0x20) != 0)
					result = result + "a";
				else
					result = result + "-";

				if ((attributes & 0x04) != 0)
					result = result + "s";
				else
					result = result + "-";

				if ((attributes & 0x02) != 0)
					result = result + "h";
				else
					result = result + "-";

				// Device
				if ((attributes & 0x4) != 0)
					result = result + "d";
				else
					result = result + "-";

				// OS is NTFS
				if ( operatingSystem == 10 )
				{
					// Encrypted
					if ( (attributes & 0x4000) != 0 ) 
					{
						result += "E";
					}
					else 
					{
						result += "-";
					}

					// Not content indexed
					if ( (attributes & 0x2000) != 0 ) 
					{
						result += "n";
					}
					else 
					{
						result += "-";
					}

					// Offline
					if ( (attributes & 0x1000) != 0 ) 
					{
						result += "O";
					}
					else 
					{
						result += "-";
					}

					// Compressed
					if ( (attributes & 0x0800) != 0 ) 
					{
						result += "C";
					}
					else 
					{
						result += "-";
					}

					// Reparse point
					if ( (attributes & 0x0400) != 0 ) 
					{
						result += "R";
					}
					else 
					{
						result += "-";
					}

					// Sparse
					if ( (attributes & 0x0200) != 0 ) 
					{
						result += "S";
					}
					else 
					{
						result += "-";
					}

					// Temporary
					if ( (attributes & 0x0100) != 0 ) 
					{
						result += "T";
					}
					else 
					{
						result += "-";
					}
				}
			}
			return result;
		}

		/// <summary>
		/// Determine if string is numeric [0-9]+
		/// </summary>
		/// <param name="rhs">string to test</param>
		/// <returns>true iff rhs is numeric</returns>
		static bool IsNumeric(string rhs)
		{
			bool result;
			if (rhs != null && rhs.Length > 0) 
			{
				result = true;
				for (int i = 0; i < rhs.Length; ++i) 
				{
					if (!char.IsDigit(rhs[i])) 
					{
						result = false;
						break;
					}
				}
			} 
			else 
			{
				result = false;
			}
			return result;
		}

		/// <summary>
		/// Make external attributes suitable for a <see cref="ZipEntry"/>
		/// </summary>
		/// <param name="info">The <see cref="FileInfo"/> to convert</param>
		/// <returns>Returns External Attributes for Zip use</returns>
		static int MakeExternalAttributes(FileInfo info)
		{
			return (int)info.Attributes;
		}

		/// <summary>
		/// Convert a wildcard expression to a regular expression
		/// </summary>
		/// <param name="wildcard">The wildcard expression to convert.</param>
		/// <returns>A regular expression representing the converted wildcard expression.</returns>
		static string WildcardToRegex(string wildcard)
		{
			int dotPos = wildcard.IndexOf('.');
			bool dotted = (dotPos >= 0) && (dotPos < wildcard.Length - 1);
			string converted = wildcard.Replace(".", @"\.");
			converted = converted.Replace("?", ".");
			converted = converted.Replace("*", ".*");
			converted = converted.Replace("(", @"\(");
			converted = converted.Replace(")", @"\)");
			if ( dotted )
			{
				converted += "$";
			}

			return converted;
		}

		#endregion
		#region Main
		/// <summary>
		/// Entry point for program, creates archiver and runs it
		/// </summary>
		/// <param name="args">
		/// Command line argument to process
		/// </param>
		public static void Main(string[] args) 
		{
			ZipFileArchiver zf = new ZipFileArchiver();
			zf.Execute(args);
		}

		#endregion
		#region Instance Fields
		/// <summary>
		/// Has user already seen help output?
		/// </summary>
		bool seenHelp_;
		
		/// <summary>
		/// File specifications possibly with wildcards from command line
		/// </summary>
		ArrayList fileSpecs_ = new ArrayList();
		
		/// <summary>
		/// Deflate compression level
		/// </summary>
		int compressionLevel_ = Deflater.DEFAULT_COMPRESSION;
		
		/// <summary>
		/// Create entries for directories with no files
		/// </summary>
		bool addEmptyDirectoryEntries_;
		
		/// <summary>
		/// Apply operations recursively
		/// </summary>
		bool recursive_;

		/// <summary>
		/// Operate silently
		/// </summary>
		bool silent_;
		
		/// <summary>
		/// Restore file date and time to that stored in zip file on extraction
		/// </summary>
		bool restoreDateTime_;
		
		/// <summary>
		/// Overwrite files handling
		/// </summary>
		Overwrite overwriteFiles = Overwrite.Prompt;

		/// <summary>
		/// Optional password for archive
		/// </summary>
		string password_;
		
		/// <summary>
		/// Where things will go when decompressed.
		/// </summary>
		string targetOutputDirectory_;

		/// <summary>
		/// What to do based on parsed command line arguments
		/// </summary>
		Operation operation_ = Operation.List;

		/// <summary>
		/// Flag whose value is true if data should be tested; false if it should not.
		/// </summary>
		bool testData_;

		/// <summary>
		/// The currently active <see cref="ZipFile"/>.
		/// </summary>
		/// <remarks>Used for callbacks/delegates</remarks>
		ZipFile activeZipFile_;

		/// <summary>
		/// Buffer used during some operations
		/// </summary>
		byte[] buffer_;

		/// <summary>
		/// The size of buffer to provide. <see cref="GetBuffer"></see>
		/// </summary>
		int bufferSize_ = 4096;
		
		/// <summary>
		/// The Zip64 extension use to apply.
		/// </summary>
		UseZip64 useZip64_ = UseZip64.Off;
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -