📄 webform1.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
namespace UnZip
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if (! System.IO.File.Exists("[要解压的文件名,以.zip结尾]")) //要解压的文件名,如d:\\123\\Example.zip
{
Label2.Text = "文件不存在!";
}
else
{
ZipInputStream s = new ZipInputStream(File.OpenRead("[要解压的文件名,以.zip结尾]"));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName("[解压目录]"); //解压目录,如c:\\Example
string fileName = Path.GetFileName(theEntry.Name);
//生成解压目录
Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
{
//如果文件的压缩后的大小为0那么说明这个文件是空的因此不需要进行读出写入
if( theEntry.CompressedSize == 0)
break;
//解压文件到指定的目录
directoryName = Path.GetDirectoryName("[解压目录]"+theEntry.Name);
//建立下面的目录和子目录
Directory.CreateDirectory(directoryName);
FileStream streamWriter = File.Create("[解压目录]"+theEntry.Name);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
Label2.Text = "解压完毕!";
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
if(System.IO.Directory.Exists("[要压缩的文件目录]")) //解压后文件存放的目录,如c:\\123\\One
{
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create("[解压后生成的文件,以.zip结尾]")); //解压后生成的文件,如c:\\One.zip
s.SetLevel(6); // 0 - store only to 9 - means best compression
DirectoryInfo di = new DirectoryInfo("[要压缩的文件目录]");
FileInfo[] a = di.GetFiles();
//压缩这个目录下的所有文件
ZipClass ss= new ZipClass();
ss.cutStr = "[要压缩的文件目录的上一级目录]"; //要压缩的文件目录的上一级目录,如c:\\123
ss.writeStream(ref s,a,crc);
//压缩这个目录下子目录及其文件
ss.direct(di,ref s,crc);
s.Finish();
s.Close();
Label1.Text = "压缩完毕!";
}
else
Label1.Text = "目录不存在!";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -