Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)

(一). 实现功能
对文件及目录的压缩及解压功能
(二). 运行图片示例

(三).代码

1. 压缩类

1 /**/ ///<summary>
2///压缩类
3///</summary>

4 public class ZipClass
5 {
6publicstaticvoidZipFile(stringFileToZip,stringZipedFile,intCompressionLevel,intBlockSize)
7{
8//如果文件没有找到,则报错
9if(!System.IO.File.Exists(FileToZip))
10{
11thrownewSystem.IO.FileNotFoundException("指定要压缩的文件:"+FileToZip+"不存在!");
12}

13
14System.IO.FileStreamStreamToZip=newSystem.IO.FileStream(FileToZip,System.IO.FileMode.Open,System.IO.FileAccess.Read);
15System.IO.FileStreamZipFile=System.IO.File.Create(ZipedFile);
16ZipOutputStreamZipStream=newZipOutputStream(ZipFile);
17ZipEntryZipEntry=newZipEntry("ZippedFile");
18ZipStream.PutNextEntry(ZipEntry);
19ZipStream.SetLevel(CompressionLevel);
20byte[]buffer=newbyte[BlockSize];
21System.Int32size=StreamToZip.Read(buffer,0,buffer.Length);
22ZipStream.Write(buffer,0,size);
23try
24{
25while(size<StreamToZip.Length)
26{
27intsizeRead=StreamToZip.Read(buffer,0,buffer.Length);
28ZipStream.Write(buffer,0,sizeRead);
29size+=sizeRead;
30}

31}

32catch(System.Exceptionex)
33{
34throwex;
35}

36ZipStream.Finish();
37ZipStream.Close();
38StreamToZip.Close();
39}

40
41/**////<summary>
42///压缩目录
43///</summary>
44///<paramname="args">数组(数组[0]:要压缩的目录;数组[1]:压缩的文件名)</param>

45publicstaticvoidZipFileDictory(string[]args)
46{
47string[]filenames=Directory.GetFiles(args[0]);
48
49Crc32crc=newCrc32();
50ZipOutputStreams=newZipOutputStream(File.Create(args[1]));
51s.SetLevel(6);
52foreach(stringfileinfilenames)
53{
54//打开压缩文件
55FileStreamfs=File.OpenRead(file);
56
57byte[]buffer=newbyte[fs.Length];
58fs.Read(buffer,0,buffer.Length);
59ZipEntryentry=newZipEntry(file);
60
61entry.DateTime=DateTime.Now;
62
63entry.Size=fs.Length;
64fs.Close();
65
66crc.Reset();
67crc.Update(buffer);
68
69entry.Crc=crc.Value;
70
71s.PutNextEntry(entry);
72
73s.Write(buffer,0,buffer.Length);
74
75}

76
77s.Finish();
78s.Close();
79}

80
81/**////<summary>
82///压缩文件
83///</summary>
84///<paramname="FileToZip">要进行压缩的文件名</param>
85///<paramname="ZipedFile">压缩后生成的压缩文件名</param>

86publicstaticvoidZipFile(stringFileToZip,stringZipedFile)
87{
88//如果文件没有找到,则报错
89if(!File.Exists(FileToZip))
90{
91thrownewSystem.IO.FileNotFoundException("指定要压缩的文件:"+FileToZip+"不存在!");
92}

93FileStreamfs=File.OpenRead(FileToZip);
94byte[]buffer=newbyte[fs.Length];
95fs.Read(buffer,0,buffer.Length);
96fs.Close();
97
98FileStreamZipFile=File.Create(ZipedFile);
99ZipOutputStreamZipStream=newZipOutputStream(ZipFile);
100ZipEntryZipEntry=newZipEntry("ZippedFile");
101ZipStream.PutNextEntry(ZipEntry);
102ZipStream.SetLevel(6);
103
104ZipStream.Write(buffer,0,buffer.Length);
105ZipStream.Finish();
106ZipStream.Close();
107}

108}

109
110 /**/ ///<summary>
111///解压类
112///</summary>

113 public class UnZipClass
114 {
115/**////<summary>
116///解压功能(解压压缩文件到指定目录)
117///</summary>
118///<paramname="args">待解压的文件</param>
119///<paramname="args">指定解压目标目录</param>

120publicstaticvoidUnZip(string[]args)
121{
122ZipInputStreams=newZipInputStream(File.OpenRead(@args[0].Trim()));
123ZipEntrytheEntry;
124stringdirectoryName=Path.GetDirectoryName(@args[1].Trim());
125
126if(!Directory.Exists(@args[1].Trim()))
127{
128Directory.CreateDirectory(directoryName);
129}

130while((theEntry=s.GetNextEntry())!=null)
131{
132;
133stringfileName=Path.GetFileName(theEntry.Name);
134
135if(fileName!=String.Empty)
136{
137FileStreamstreamWriter=File.Create(@args[1].Trim()+fileName);
138
139intsize=2048;
140byte[]data=newbyte[2048];
141while(true)
142{
143size=s.Read(data,0,data.Length);
144if(size>0)
145{
146streamWriter.Write(data,0,size);
147}

148else
149{
150break;
151}

152}

153
154streamWriter.Close();
155}

156}

157s.Close();
158}

159

2. 前台页面代码

1 < body >
2 < formid = " form1 " runat = " server " >
3 < div >
4 & nbsp; < asp:LabelID = " Label1 " runat = " server " BackColor = " #C0C0FF " Font - Size = " XX-Large "
5 Height = " 44px " Text = " 压缩文件/文件夹示例 " Width = " 366px " ></ asp:Label >
6 < asp:PanelID = " Panel1 " runat = " server " Height = " 1px " Width = " 369px " BackColor = " #FFFFC0 " >
7 <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值