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

📄 zip.cs

📁 给出了 zip 压缩算法的完整实现过程。
💻 CS
📖 第 1 页 / 共 2 页
字号:
		public PrivilegeSaveEnum Privilege		{			get {return m_PrivilegeSave;}			set {m_PrivilegeSave = value;}		}//		public int Encryption//		{//			get {return m_Encryption;}//			set {m_Encryption = value;}//		}		public RecurseEnum Recurse		{			get {return m_Recurse;}			set {m_Recurse = value;}		}		public RepairEnum Repair		{			get {return m_Repair;}			set {m_Repair = value;}		}		public CompressionLevelEnum Level		{			get {return m_CompressionLevel;}			set {m_CompressionLevel = value;}		}		#endregion		#region Structures		//ZPOPT Is Used To Set The Options In The ZIP32.DLL		[ StructLayout( LayoutKind.Sequential )]		protected struct ZPOPT		{			[MarshalAs(UnmanagedType.LPTStr)]			public string Date;					// US Date (8 Bytes Long) "12/31/98"?			[MarshalAs(UnmanagedType.LPTStr)]			public string szRootDir;			// Root Directory Pathname (Up To 256 Bytes Long)			[MarshalAs(UnmanagedType.LPTStr)]			public string szTempDir;			// Temp Directory Pathname (Up To 256 Bytes Long)			public int fTemp;					// 1 If Temp dir Wanted, Else 0			public int fSuffix;					// Include Suffixes (Not Yet Implemented!)			public int fEncrypt;					// 1 If Encryption Wanted, Else 0			public int fSystem;					// 1 To Include System/Hidden Files, Else 0			public int fVolume;					// 1 If Storing Volume Label, Else 0			public int fExtra;						// 1 If Excluding Extra Attributes, Else 0			public int fNoDirEntries;			// 1 If Ignoring Directory Entries, Else 0			public int fExcludeDate;			// 1 If Excluding Files Earlier Than Specified Date, Else 0			public int fIncludeDate;			// 1 If Including Files Earlier Than Specified Date, Else 0			public int fVerbose;					// 1 If Full Messages Wanted, Else 0			public int fQuiet;						// 1 If Minimum Messages Wanted, Else 0			public int fCRLF_LF;				// 1 If Translate CR/LF To LF, Else 0			public int fLF_CRLF;				// 1 If Translate LF To CR/LF, Else 0			public int fJunkDir;					// 1 If Junking Directory Names, Else 0			public int fGrow;						// 1 If Allow Appending To Zip File, Else 0			public int fForce;						// 1 If Making Entries Using DOS File Names, Else 0			public int fMove;						// 1 If Deleting Files Added Or Updated, Else 0			public int fDeleteEntries;			// 1 If Files Passed Have To Be Deleted, Else 0			public int fUpdate;					// 1 If Updating Zip File-Overwrite Only If Newer, Else 0			public int fFreshen;					// 1 If Freshing Zip File-Overwrite Only, Else 0			public int fJunkSFX;				// 1 If Junking SFX Prefix, Else 0			public int fLatestTime;				// 1 If Setting Zip File Time To Time Of Latest File In Archive, Else 0			public int fComment;				// 1 If Putting Comment In Zip File, Else 0			public int fOffsets;					// 1 If Updating Archive Offsets For SFX Files, Else 0			public int fPrivilege;				// 1 If Not Saving Privileges, Else 0			public int fEncryption;				// Read Only Property!!!			public int fRecurse;					// 1 (-r), 2 (-R) If Recursing Into Sub-Directories, Else 0			public int fRepair;					// 1 = Fix Archive, 2 = Try Harder To Fix, Else 0			public byte flevel;					// Compression Level - 0 = Stored 6 = Default 9 = Max		}		//CallBack string.  This is a byte array 4K long		protected struct ZipCBChar		{			[MarshalAs(UnmanagedType.ByValArray, SizeConst= 4096, ArraySubType = UnmanagedType.U1)]			public byte [] ch;		}		//This Structure Is Used For The ZIP32.DLL Function Callbacks		[ StructLayout( LayoutKind.Sequential )]		protected struct ZIPUSERFUNCTIONS		{			public ZDLLPrintCallback ZDLLPRNT;			public ZDLLCommentCallback ZDLLCOMMENT;			public ZDLLPasswordCallback ZDLLPASSWORD;			public ZDLLServiceCallback ZDLLSERVICE;		}		#endregion		#region DLL Function Declares		//NOTE:		//All the following Declares assumes ZIP32.DLL Is In Your \Windows\System Directory!		//set zip callbacks		[DllImport("zip32.dll", SetLastError=true)]		protected static extern int ZpInit(ref ZIPUSERFUNCTIONS zuf);		//set zip options		[DllImport("zip32.dll", SetLastError=true)]		protected static extern int ZpSetOptions(ref ZPOPT zopts);		//get zip options		[DllImport("zip32.dll", SetLastError=true)]		protected static extern  ZPOPT ZpGetOptions();		//zip archive		[DllImport("zip32.dll", SetLastError=true)]		protected static extern int ZpArchive(int argc, string funame, string [] zipnames);				#endregion		#region Delegates		protected delegate int ZDLLPrintCallback (ref ZipCBChar m, uint x);		protected delegate int ZDLLServiceCallback (ref ZipCBChar m, uint x);		protected delegate short ZDLLPasswordCallback (ref ZipCBChar p, int n, ref ZipCBChar m, ref ZipCBChar name);		protected delegate short ZDLLCommentCallback (ref ZipCBChar m);		#endregion		#region Events					public event ZipDLLPrintMessageEventHandler ReceivePrintMessage;		public event ZipDLLServiceMessageEventHandler ReceiveServiceMessage;		#endregion		#region Protected Functions		protected virtual void OnReceivePrintMessage (ZipDLLPrintMessageEventArgs e)		{			if (ReceivePrintMessage != null) 			{				ReceivePrintMessage(this, e); 			}		}		protected virtual void OnReceiveServiceMessage (ZipDLLServiceMessageEventArgs e)		{			if (ReceiveServiceMessage != null) 			{				ReceiveServiceMessage(this, e); 			}		}		#endregion		#region Callback Functions		//The zip32.dll passes activity messages to the declared byte array.  Decode		//the byte array to see the message		protected int ZDLLPrint (ref ZipCBChar msg, uint x)		{			string s = string.Empty;			if (msg.ch[0] == 0) return 0;			s = m_Ascii.GetString(msg.ch,0, (int)x);			//Raise this event			ZipDLLPrintMessageEventArgs e = new ZipDLLPrintMessageEventArgs(s);			OnReceivePrintMessage(e);			return 0;		}		/*	 		DLLSERVICE *ServCallBk  = Callback function designed to be used for                          allowing the application to process Windows messages,                          or canceling the operation, as well as giving the                          option of a progress indicator. If this function                          returns a non-zero value, then it will terminate                          what it is doing. It provides the application with                          the name of the name of the archive member it has                          just processed, as well as it's original size.						  		msg.ch = the name of the file being zipped		x = The size of the file being zipped				 * */		protected int ZDLLService (ref ZipCBChar msg, uint x)		{			string s = string.Empty;			int i = 0;			for (i = 0; i <= msg.ch.Length; i ++)				if (msg.ch[i] == 0) break;			 s = m_Ascii.GetString(msg.ch,0,i);			//Raise this event			ZipDLLServiceMessageEventArgs e = new ZipDLLServiceMessageEventArgs(s, (int)x);			OnReceiveServiceMessage (e);			return m_Stop;		}		protected short ZDLLPassword (ref ZipCBChar p, int n, ref ZipCBChar m, ref ZipCBChar name)		{			if (m_Password == null | m_Password == string.Empty) return 1;			//clear the byte array			for (int i = 0; i <= n-1; i ++)				p.ch[i] = 0;			m_Ascii.GetBytes(m_Password, 0, m_Password.Length, p.ch, 0);						return 0;		}				protected short ZDLLComment (ref ZipCBChar msg)		{			if (m_Comment == null | m_Comment == string.Empty) return 1;			//clear the byte array			for (int i = 0; i <= msg.ch.Length-1; i ++)				msg.ch[i] = 0;			//set the comment in the zip file			m_Ascii.GetBytes(m_Comment, 0, m_Comment.Length, msg.ch, 0);			return 0;		}		#endregion		#region Public Functions		public int ZipFiles()		{			//check to see if there is enough information to proceed.			//Exceptions can be thrown if required data is not passed in			if (m_FilesToZip.Length == 0) return -1;			if (m_ZipFileName == string.Empty) return -1;			m_zuf = new ZIPUSERFUNCTIONS();			//set up the callback delegates			m_CallbackPrint = new ZDLLPrintCallback(ZDLLPrint);			m_CallbackService = new ZDLLServiceCallback(ZDLLService);			m_CallbackPassword = new ZDLLPasswordCallback(ZDLLPassword);			m_CallbackComment = new ZDLLCommentCallback(ZDLLComment);			//set up the callback structure			m_zuf.ZDLLPRNT = m_CallbackPrint;			m_zuf.ZDLLSERVICE = m_CallbackService;			m_zuf.ZDLLPASSWORD = m_CallbackPassword;			m_zuf.ZDLLCOMMENT = m_CallbackComment;						//init the zip process			int ret = ZpInit(ref m_zuf);			//set the zip options			ZPOPT zopt = CreateZPOPTOptions();			ret = ZpSetOptions(ref zopt);			//zip the files			try			{				ret = ZpArchive(m_FilesToZip.Length, m_ZipFileName, m_FilesToZip);			}			catch(Exception e)			{				MessageBox.Show (e.ToString() + "\r\n" + "Last Win32ErrorCode: " + Marshal.GetLastWin32Error());				//You can check the meaning of return codes here:				//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes__0-499_.asp			}			return ret;		}		public void Stop ()		{			//m_Stop gets returned from the DLLService callback.			//A value of 1 means abort processing.			m_Stop = 1;			Application.DoEvents();		}		#endregion		#region Private Functions		private ZPOPT CreateZPOPTOptions()		{			ZPOPT zopt = new ZPOPT();						zopt.Date = m_Date;			zopt.szRootDir = m_RootDir;			zopt.szTempDir = m_TempDir;			zopt.fTemp = (int)m_Temp;			zopt.fSuffix = 0;			zopt.fEncrypt = (int)m_EncryptionSpecified;			zopt.fSystem = (int)m_SystemFilesIgnored;			zopt.fVolume = (int)m_VolumeLabelStored;			zopt.fExtra = (int)m_ExtraAttributesExcluded;			zopt.fNoDirEntries = (int)m_DirEntriesIgnored;			zopt.fExcludeDate = (int)m_ExcludeDate;			zopt.fIncludeDate = (int)m_IncludeDate;			zopt.fVerbose = (int)m_VerboseMessages;			zopt.fQuiet = (int)m_QuietMessages;			zopt.fCRLF_LF = (int)m_CRLF_LFTranslate;			zopt.fLF_CRLF = (int)m_LF_CRLFTranslate;			zopt.fJunkDir = (int)m_JunkDir;			zopt.fGrow = (int)m_GrowZipFile;			zopt.fForce = (int)m_ForceDOSNames;			zopt.fMove = (int)m_Move;			zopt.fDeleteEntries = (int)m_DeleteEntries;			zopt.fUpdate = (int)m_Update;			zopt.fFreshen = (int)m_Freshen;			zopt.fJunkSFX = (int)m_JunkSFXPrefix;			zopt.fLatestTime = (int)m_LatestTime;			zopt.fComment = (int)m_CommentOption;			zopt.fOffsets = (int)m_Offsets;			zopt.fPrivilege = (int)m_PrivilegeSave;			zopt.fEncryption = 0;			zopt.fRecurse = (int)m_Recurse;			zopt.fRepair = (int)m_Repair;			zopt.flevel = (byte)m_CompressionLevel;			return zopt;		}		#endregion	}}

⌨️ 快捷键说明

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