使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类
2009-07-20 11:55 chenkai 阅读(5597) 评论(1) 编辑 收藏 举报在处理后台附件上载由于文件较多,需要每个文件单独上传关键是有些文件数据量比较少 也需要单独上传,这样导致后台数据流量较大而且用户操作麻烦.
在处理这方面业务时,可以简化:首先验证用户上传文件的大小,设定不超过1M文件为限制并记录,当用户点击一次操作时后台程序把所有小数据量文件进行压缩成一个单独文件来上传,这样简化用户操作难度 增强用户体验,在获得上载文件时同样把这个文件进行解压本地即可...
使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类: 完整代码如下
A:ICSharpCode.SharpZipLib.DLL组件下载地址,如果要实现必须在项目中引用该组件DLL
下载地址:http://good.gd/203866.htm
B:完整的操作类代码实例:
在处理这方面业务时,可以简化:首先验证用户上传文件的大小,设定不超过1M文件为限制并记录,当用户点击一次操作时后台程序把所有小数据量文件进行压缩成一个单独文件来上传,这样简化用户操作难度 增强用户体验,在获得上载文件时同样把这个文件进行解压本地即可...
使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类: 完整代码如下
A:ICSharpCode.SharpZipLib.DLL组件下载地址,如果要实现必须在项目中引用该组件DLL
下载地址:http://good.gd/203866.htm
B:完整的操作类代码实例:
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
6
//using the Compent Comspac
7
using System.IO;
8
using System.Text;
9
using System.Threading;
10
using ICSharpCode.SharpZipLib;
11
using ICSharpCode.SharpZipLib.Zip;
12
using ICSharpCode.SharpZipLib.Checksums;
13
14
namespace TestJqueryAjax
15
{
16
/// <summary>
17
/// 使用ICSharpZipCode.Dll实现解压缩
18
/// Author:chenkai Time:2009年7月13日22:03:27
19
/// Version:Beta1.0.0-(测试版)
20
/// </summary>
21
public class ICSharpZipCodeTest
22
{
23
24
/// <summary>
25
/// 实现压缩功能
26
/// </summary>
27
/// <param name="filenameToZip">要压缩文件(绝对文件路径)</param>
28
/// <param name="Zipedfiledname">压缩(绝对文件路径)</param>
29
/// <param name="CompressionLevel">压缩比</param>
30
/// <param name="password">加密密码</param>
31
/// <param name="comment">压缩文件描述</param>
32
/// <returns>异常信息</returns>
33
public static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel,
34
string password, string comment)
35
{
36
try
37
{
38
//使用正则表达式-判断压缩文件路径
39
System.Text.RegularExpressions.Regex newRegex = new System.Text.
40
RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))");
41
42
if (!newRegex.Match(Zipedfiledname).Success)
43
{
44
File.Delete(Zipedfiledname);
45
return "压缩文件的路径有误!";
46
}
47
48
//创建ZipFileOutPutStream
49
ZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname,
50
FileMode.OpenOrCreate));
51
52
//判断Password
53
if (password != null && password.Length > 0)
54
{
55
newzipstream.Password = password;
56
}
57
58
if (comment != null && comment.Length > 0)
59
{
60
newzipstream.SetComment(comment);
61
}
62
63
//设置CompressionLevel
64
newzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression
65
66
//执行压缩
67
foreach (string filename in filenameToZip)
68
{
69
FileStream newstream = File.OpenRead(filename);//打开预压缩文件
70
71
//判断路径
72
if (!newRegex.Match(Zipedfiledname).Success)
73
{
74
File.Delete(Zipedfiledname);
75
return "压缩文件目标路径不存在!";
76
}
77
78
byte[] setbuffer=new byte[newstream.Length];
79
newstream.Read(setbuffer,0,setbuffer.Length);//读入文件
80
81
//新建ZipEntrity
82
ZipEntry newEntry = new ZipEntry(filename);
83
84
//设置时间-长度
85
newEntry.DateTime = DateTime.Now;
86
newEntry.Size = newstream.Length;
87
88
newstream.Close();
89
90
newzipstream.PutNextEntry(newEntry);//压入
91
92
newzipstream.Write(setbuffer,0,setbuffer.Length);
93
94
}
95
//重复压入操作
96
newzipstream.Finish();
97
newzipstream.Close();
98
99
}catch (Exception e)
100
{
101
//出现异常
102
File.Delete(Zipedfiledname);
103
return e.Message.ToString();
104
}
105
106
return "";
107
}
108
109
/// <summary>
110
/// 实现解压操作
111
/// </summary>
112
/// <param name="zipfilename">要解压文件Zip(物理路径)</param>
113
/// <param name="UnZipDir">解压目的路径(物理路径)</param>
114
/// <param name="password">解压密码</param>
115
/// <returns>异常信息</returns>
116
public static string UnMakeZipFile(string zipfilename,string UnZipDir,string password)
117
{
118
//判断待解压文件路径
119
if (!File.Exists(zipfilename))
120
{
121
File.Delete(UnZipDir);
122
return "待解压文件路径不存在!";
123
}
124
125
//创建ZipInputStream
126
ZipInputStream newinStream = new ZipInputStream(File.OpenRead(zipfilename));
127
128
//判断Password
129
if (password != null && password.Length > 0)
130
{
131
newinStream.Password = password;
132
}
133
134
//执行解压操作
135
try
136
{
137
ZipEntry theEntry;
138
139
//获取Zip中单个File
140
while ((theEntry = newinStream.GetNextEntry()) != null)
141
{
142
//判断目的路径
143
if (Directory.Exists(UnZipDir))
144
{
145
Directory.CreateDirectory(UnZipDir);//创建目的目录
146
}
147
148
//获得目的目录信息
149
string Driectoryname = Path.GetDirectoryName(UnZipDir);
150
string pathname = Path.GetDirectoryName(theEntry.Name);//获得子级目录
151
string filename = Path.GetFileName(theEntry.Name);//获得子集文件名
152
153
//处理文件盘符问题
154
pathname = pathname.Replace(":", "$");//处理当前压缩出现盘符问题
155
Driectoryname = Driectoryname + "\\" + pathname;
156
157
//创建
158
Directory.CreateDirectory(Driectoryname);
159
160
//解压指定子目录
161
if (filename != string.Empty)
162
{
163
FileStream newstream = File.Create(Driectoryname + "\\" + pathname);
164
165
int size = 2048;
166
167
byte[] newbyte = new byte[size];
168
169
while (true)
170
{
171
size = newinStream.Read(newbyte, 0, newbyte.Length);
172
173
if (size > 0)
174
{
175
//写入数据
176
newstream.Write(newbyte, 0, size);
177
}
178
else
179
{
180
break;
181
}
182
newstream.Close();
183
}
184
}
185
186
}
187
newinStream.Close();
188
}
189
catch (Exception se)
190
{
191
192
return se.Message.ToString();
193
}
194
finally
195
{
196
newinStream.Close();
197
}
198
199
200
return "";
201
}
202
}
203
}
204

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架