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

📄 frmcompress.cs

📁 压缩和解压缩的源码,可以支持大多数文件的压缩和解压缩
💻 CS
字号:
using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace SoundCondense
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class frmCompress : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.Button btnBrowseFile;
		private System.Windows.Forms.TextBox txtCompressFile;
		private System.Windows.Forms.Button btnCompressFile;
		private System.Windows.Forms.Button btnReleaseFile;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCompress()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBrowseFile = new System.Windows.Forms.Button();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.txtCompressFile = new System.Windows.Forms.TextBox();
			this.btnCompressFile = new System.Windows.Forms.Button();
			this.btnReleaseFile = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnBrowseFile
			// 
			this.btnBrowseFile.Location = new System.Drawing.Point(224, 32);
			this.btnBrowseFile.Name = "btnBrowseFile";
			this.btnBrowseFile.Size = new System.Drawing.Size(40, 23);
			this.btnBrowseFile.TabIndex = 0;
			this.btnBrowseFile.Text = "...";
			this.btnBrowseFile.Click += new System.EventHandler(this.btnBrowseFile_Click);
			// 
			// txtCompressFile
			// 
			this.txtCompressFile.Location = new System.Drawing.Point(40, 32);
			this.txtCompressFile.Name = "txtCompressFile";
			this.txtCompressFile.Size = new System.Drawing.Size(168, 21);
			this.txtCompressFile.TabIndex = 1;
			this.txtCompressFile.Text = "";
			// 
			// btnCompressFile
			// 
			this.btnCompressFile.Location = new System.Drawing.Point(40, 80);
			this.btnCompressFile.Name = "btnCompressFile";
			this.btnCompressFile.Size = new System.Drawing.Size(88, 23);
			this.btnCompressFile.TabIndex = 2;
			this.btnCompressFile.Text = "压缩文件";
			this.btnCompressFile.Click += new System.EventHandler(this.btnCompressFile_Click);
			// 
			// btnReleaseFile
			// 
			this.btnReleaseFile.Location = new System.Drawing.Point(176, 80);
			this.btnReleaseFile.Name = "btnReleaseFile";
			this.btnReleaseFile.Size = new System.Drawing.Size(88, 23);
			this.btnReleaseFile.TabIndex = 3;
			this.btnReleaseFile.Text = "解压文件";
			this.btnReleaseFile.Click += new System.EventHandler(this.btnReleaseFile_Click);
			// 
			// frmCompress
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(304, 133);
			this.Controls.Add(this.btnReleaseFile);
			this.Controls.Add(this.btnCompressFile);
			this.Controls.Add(this.txtCompressFile);
			this.Controls.Add(this.btnBrowseFile);
			this.Name = "frmCompress";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "文件压缩";
			this.Load += new System.EventHandler(this.frmCompress_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmCompress());
		}	

		private void frmCompress_Load(object sender, System.EventArgs e)
		{
			this.MaximizeBox=false;
		}

		/// <summary>
		/// 浏览文件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnBrowseFile_Click(object sender, System.EventArgs e)
		{
//			OpenFileDialog dlg = new OpenFileDialog();
//			if (dlg.ShowDialog() == DialogResult.OK)
//			{
//				string fileName = System.IO.Path.GetFileName(dlg.FileName);
//
//				this.txtCompressFile.Text=this.openFileDialog.FileName;
//			}

//			if(this.openFileDialog.ShowDialog() == DialogResult.OK)
//			{
//				this.txtCompressFile.Text=this.openFileDialog.FileName;
//			}

			if(this.openFileDialog.ShowDialog()==DialogResult.OK)
			{
				string name = System.IO.Path.GetFileName(this.openFileDialog.FileName);
				this.txtCompressFile.Text = this.openFileDialog.FileName;
			}

			/*
		    OpenFileDialog opd = new OpenFileDialog(); 
			opd.InitialDirectory = "C:\\";
			opd.Filter = "文本文档(*.txt)|*.txt|RTF文档|*.rtf|所有文档(*.*)|*.*";
			opd.FilterIndex = 2;
			opd.RestoreDirectory = true;
			if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
			{
				RichTextBoxStreamType filetype;
				switch (opd.FilterIndex)
				{
					case 1: filetype = RichTextBoxStreamType.PlainText;
						break;
					case 2: filetype = RichTextBoxStreamType.RichText;
						break;
					default: filetype = RichTextBoxStreamType.UnicodePlainText;
						break;
				}
				richTextBox1.LoadFile(opd.FileName, filetype);
			}
			*/
		}

		/// <summary>
		/// 压缩文件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCompressFile_Click(object sender, System.EventArgs e)
		{
			if(this.txtCompressFile.Text=="")
			{
				MessageBox.Show("请选择文件!");
				return;
			}

			string fullName = this.txtCompressFile.Text.Trim();	             //C:\test\a.txt
			string destPath = System.IO.Path.GetDirectoryName(fullName);     //C:\test
	
			string [] FileProperties=new string[2];

			//待压缩文件
			FileProperties[0] = fullName;   
            //压缩后的目标文件
			FileProperties[1] = destPath+"\\"+System.IO.Path.GetFileNameWithoutExtension(fullName) + ".rar";            //"C:\zip\a.zip";  //压缩后的目标文件  

			ZipClass Zc=new ZipClass();
			Zc.ZipFileMain(FileProperties);

			//删除压缩前的文件
//			System.IO.File.Delete(fullName);
		}

		/// <summary>
		/// 解压文件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReleaseFile_Click(object sender, System.EventArgs e)
		{
			if(this.txtCompressFile.Text=="")
			{
				MessageBox.Show("请选择文件!");
				return;
			}

			string fullName = this.txtCompressFile.Text.Trim(); //C:\test\a.zip

			//解压文件
//			AttachmentUnZip.UpZip(fullName);
//
//			string [] FileProperties = new string[2];
//			FileProperties[0] = fullName;  //待解压的文件
//			FileProperties[1] = System.IO.Path.GetDirectoryName(fullName);  //解压后放置的目标目录
//			UnZipClass UnZc = new UnZipClass();
//			UnZc.UnZip(FileProperties);
	   
			string dir = System.IO.Path.GetDirectoryName(fullName);
			string fileName = System.IO.Path.GetFileName(fullName);
			UnZipClass.UnZipFile(fileName,dir);
		}		
	}
}

⌨️ 快捷键说明

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