All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
什么是.net的接口和抽象类_[Asp.Net教程]
实用技巧,利用Apache实现禁止图片盗链_php资料_编程技术
详细介绍:Apache+PHP+MySQL配置攻略_php资料_编程技术
如何识别真实和自动创建的索引?_[SQL,Server教程]
ASP.NET数据库编程快速入门之技术慨述_[Asp.Net教程]
理解学习PHP编码规范之注释和文件结构_php资料_编程技术
PHP中使用XML-RPC构造Web,Service简单入门_php资料_编程技术
ASP.NET2.0登陆控件的使用(常见的三种方法)_[Asp.Net教程]
用,PHP,V5,开发多任务应用程序_php资料_编程技术
LINQ,in,Action,电子书下载_[Asp.Net教程]
Reading number is top 10 pictures
如果没有好报,为什么要做好人?
29 the belle stars after bath figure5
Female model behind the bitterness, often being overcharged2
Fierce! China's special forces training the devil2
Sell the barbecue as says father du breul3
The real super beauty9
Distribution of wealth in China survey status report
壮丽的云彩2
人物写真-谢楠
中国文革时期的色情图片2
Download software ranking
Tram sex maniac 2 (H) rar bag18
Proficient in Eclipse
ASP.NET.2.0.XML.高级编程(第3版)
Proficient in JavaScript
网络管理员第三版
C++编程教程第三版
Boxer's Top ten classic battle1
Jinling thirteen stock
Unix video tutorial2
JSP+Ajax Web development typical examples
delv published in(发表于) 2014/1/27 6:46:06 Edit(编辑)
Asp.net,2.0,C#实现压缩解压功能

Asp.net,2.0,C#实现压缩解压功能

Asp.net 2.0 C#实现压缩解压功能 (示例代码)_[Asp.Net教程]

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


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

1/**////


2/// 压缩类
3///

4public class ZipClass
5{
6 public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
7 {
8 //如果文件没有找到,则报错
9 if (!System.IO.File.Exists(FileToZip))
10 {
11 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
12 }
13
14 System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
15 System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
16 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
17 ZipEntry ZipEntry = new ZipEntry("ZippedFile");
18 ZipStream.PutNextEntry(ZipEntry);
19 ZipStream.SetLevel(CompressionLevel);
20 byte[] buffer = new byte[BlockSize];
21 System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
22 ZipStream.Write(buffer, 0, size);
23 try
24 {
25 while (size < StreamToZip.Length)
26 {
27 int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
28 ZipStream.Write(buffer, 0, sizeRead);
29 size += sizeRead;
30 }
31 }
32 catch (System.Exception ex)
33 {
34 throw ex;
35 }
36 ZipStream.Finish();
37 ZipStream.Close();
38 StreamToZip.Close();
39 }
40
41 /**////
42 /// 压缩目录
43 ///

44 /// 数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)
45 public static void ZipFileDictory(string[] args)
46 {
47 string[] filenames = Directory.GetFiles(args[0]);
48
49 Crc32 crc = new Crc32();
50 ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
51 s.SetLevel(6);
52 foreach (string file in filenames)
53 {
54 //打开压缩文件
55 FileStream fs = File.OpenRead(file);
56
57 byte[] buffer = new byte[fs.Length];
58 fs.Read(buffer, 0, buffer.Length);
59 ZipEntry entry = new ZipEntry(file);
60
61 entry.DateTime = DateTime.Now;
62
63 entry.Size = fs.Length;
64 fs.Close();
65
66 crc.Reset();
67 crc.Update(buffer);
68
69 entry.Crc = crc.Value;
70
71 s.PutNextEntry(entry);
72
73 s.Write(buffer, 0, buffer.Length);
74
75 }
76
77 s.Finish();
78 s.Close();
79 }
80
81 /**////
82 /// 压缩文件
83 ///

84 /// 要进行压缩的文件名
85 /// 压缩后生成的压缩文件名
86 public static void ZipFile(string FileToZip, string ZipedFile)
87 {
88 //如果文件没有找到,则报错
89 if (!File.Exists(FileToZip))
90 {
91 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
92 }
93 FileStream fs = File.OpenRead(FileToZip);
94 byte[] buffer = new byte[fs.Length];
95 fs.Read(buffer, 0, buffer.Length);
96 fs.Close();
97
98 FileStream ZipFile = File.Create(ZipedFile);
99 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
100 ZipEntry ZipEntry = new ZipEntry("ZippedFile");
101 ZipStream.PutNextEntry(ZipEntry);
102 ZipStream.SetLevel(6);
103
104 ZipStream.Write(buffer, 0, buffer.Length);
105 ZipStream.Finish();
106 ZipStream.Close();
107 }
108}
109
110/**////
111/// 解压类
112///

113public class UnZipClass
114{
115 /**////
116 /// 解压功能(解压压缩文件到指定目录)
117 ///

118 /// 待解压的文件
119 /// 指定解压目标目录
120 public static void UnZip(string[] args)
121 {
122 ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));
123 ZipEntry theEntry;
124 string directoryName = Path.GetDirectoryName(@args[1].Trim());
125
126 if (!Directory.Exists(@args[1].Trim()))
127 {
128 Directory.CreateDirectory(directoryName);
129 }
130 while ((theEntry = s.GetNextEntry()) != null)
131 {
132 ;
133 string fileName = Path.GetFileName(theEntry.Name);
134
135 if (fileName != String.Empty)
136 {
137 FileStream streamWriter = File.Create(@args[1].Trim() + fileName);
138
139 int size = 2048;
140 byte[] data = new byte[2048];
141 while (true)
142 {
143 size = s.Read(data, 0, data.Length);
144 if (size > 0)
145 {
146 streamWriter.Write(data, 0, size);
147 }
148 else
149 {
150 break;
151 }
152 }
153
154 streamWriter.Close();
155 }
156 }
157 s.Close();
158 }
2. 前台页面代码
1
2
3

4 &nbsp; 5 Height="44px" Text="压缩文件/文件夹示例" Width="366px">
6
7

8

9

10
11

12
13 &nbsp;

14 onClick="btZipDictory_Click" Text="压缩目录" />

15

16

17
18
19

20
21 &nbsp;

22 onClick="btUnZipDictory_Click" />

23

24

25

26

27
28
29

30
31 &nbsp;

32 onClick="btZipFile_Click" />

33

34

35
36
37

38
39 &nbsp;

40 onClick="btUnZipFile_Click" />

41

42
43

44
45

46

47

48

49
50

51
52
53
3. 后台页面代码
1public partial class _Default : System.Web.UI.Page
2{
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7 protected void btZipDictory_Click(object sender, EventArgs e)
8 {
9 string[] FilePathS = new string[2];
10 FilePathS[0] = TextBox1.Text.Trim(); //待压缩的文件目录
11 FilePathS[1] = TextBox2.Text.Trim(); //生成的目标文件
12 ZipClass.ZipFileDictory(FilePathS);
13 }
14 protected void btUnZipDictory_Click(object sender, EventArgs e)
15 {
16 string[] FilePathS = new string[2];
17 FilePathS[0] = TextBox3.Text.Trim(); //待解压的文件
18 FilePathS[1] = TextBox4.Text.Trim(); //解压目标存放目录
19 UnZipClass.UnZip(FilePathS);
20 }
21 protected void btZipFile_Click(object sender, EventArgs e)
22 {
23 string[] FilePathS = new string[2];
24 FilePathS[0] = TextBox5.Text.Trim(); //待压缩的文件
25 FilePathS[1] = TextBox6.Text.Trim(); //生成的压缩文件名
26 ZipClass.ZipFile(FilePathS[0], FilePathS[1]);
27
28 }
29 protected void btUnZipFile_Click(object sender, EventArgs e)
30 {
31 string[] FilePathS = new string[2];
32 FilePathS[0] = TextBox7.Text.Trim(); //待解压的文件
33 FilePathS[1] = TextBox8.Text.Trim(); //解压目标存放目录
34 UnZipClass.UnZip(FilePathS);
35 }
36}
37







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.