参考
https://blog.csdn.net/lki_suidongdong/article/details/20942977
重点:
实现多级子目录的压缩,类似winrar,可以选择是否排除基准目录
1
public
void ZipDirectoryTest()
2
{
3
string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), DateTime.Now.Ticks.ToString());
4
foreach (string sub in
new
string[] { "bin", "release", "test", "test\\bin", "" })
5
{
6
string subPath = System.IO.Path.Combine(path, sub);
7
if (!System.IO.Directory.Exists(subPath))
8
System.IO.Directory.CreateDirectory(subPath);
9 System.IO.File.WriteAllText(System.IO.Path.Combine(subPath, "1.cs"), "");
10 System.IO.File.WriteAllText(System.IO.Path.Combine(subPath, "1.txt"), "");
11 System.IO.File.WriteAllText(System.IO.Path.Combine(subPath, "1.html"), "");
12 System.IO.File.WriteAllText(System.IO.Path.Combine(subPath, "1.bin"), "");
13
14
}
15
Console.WriteLine(path);
16
17
new ZipHelper().ZipDirectory(path, "e:\\temp\\tt.zip",false);
18 ZipHelper.UnZip("e:\\temp\\tt.zip", "e:\\temp\\tt2");
19
//System.IO.Directory.Delete(path, true);
20
//Q.Helper.FileHelper.SelectFile(path);
21 }
代码
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Linq;
6
7
using System.Text;
8
9
using System.IO;
10
11
using ICSharpCode.SharpZipLib;
12
13
using ICSharpCode.SharpZipLib.Zip;
14
15
#if NETSTANDARD2_0
16
17
using ICSharpCode.SharpZipLib.Checksum;
18
19
#else
20
21
using ICSharpCode.SharpZipLib.Checksums;
22
23
#endif
24
25
26
27
namespace Q.Helper.Zip
28
29
{
30
31
32
33
///
<summary>
34
35
/// 适用与ZIP压缩
36
37
///
</summary>
38
39
public
class ZipHelper
40
41
{
42
43
public
int Level
44
45
{
46
47
get; set;
48
49
}
50
51
#region 压缩
52
53
54
55
///
<summary>
56
57
/// 递归压缩文件夹的内部方法(排除相对路径)
58
59
///
</summary>
60
61
///
<param name="folderToZip">要压缩的文件夹路径</param>
62
63
///
<param name="zipStream">压缩输出流</param>
64
65
///
<param name="parentFolderName">此文件夹的上级文件夹</param>
66
67
///
<param name="includeFloderName">是否包含目录名</param>
68
69
///
<returns></returns>
70
71
private
bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName, bool createBaseFolder = true)
72
73
{
74
75 folderToZip = folderToZip.Replace("\\", "/");
76
77
bool result = true;
78
79
string[] folders, files;
80
81 ZipEntry ent = null;
82
83 FileStream fs = null;
84
85 Crc32 crc = new Crc32();
86
87
88
89
try
90
91
{
92
93
string entPath = Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/").Replace("\\", "/");
94
95
if (!createBaseFolder)
96
97 entPath = entPath.Substring(entPath.IndexOf("/") + 1);
98
99
if (!string.IsNullOrEmpty(entPath))
100
101
{
102
103 ent = new ZipEntry(entPath);
104
105
Console.WriteLine(entPath);
106
107
zipStream.PutNextEntry(ent);
108
109
zipStream.Flush();
110
111
}
112
113 files = Directory.GetFiles(folderToZip);
114
115
foreach (string file in files)
116
117
{
118
119 fs = File.OpenRead(file);
120
121
122
123
byte[] buffer = new
byte[fs.Length];
124
125 fs.Read(buffer, 0, buffer.Length);
126
127 entPath = Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)).Replace("\\", "/");
128
129
if (!createBaseFolder)
130
131 entPath = entPath.Substring(entPath.IndexOf("/") + 1);
132
133
Console.WriteLine(entPath);
134
135 ent = new ZipEntry(entPath);
136
137 ent.DateTime = DateTime.Now;
138
139 ent.Size = fs.Length;
140
141
142
143
fs.Close();
144
145
146
147
crc.Reset();
148
149
crc.Update(buffer);
150
151
152
153 ent.Crc = crc.Value;
154
155
zipStream.PutNextEntry(ent);
156
157 zipStream.Write(buffer, 0, buffer.Length);
158
159
}
160
161
162
163
}
164
165
catch (Exception ex)
166
167
{
168
169 result = false;
170
171
throw ex;
172
173
}
174
175
finally
176
177
{
178
179
if (fs != null)
180
181
{
182
183
fs.Close();
184
185
fs.Dispose();
186
187
}
188
189
if (ent != null)
190
191
{
192
193 ent = null;
194
195
}
196
197
GC.Collect();
198
199 GC.Collect(1);
200
201
}
202
203
204
205 folders = Directory.GetDirectories(folderToZip);
206
207
//多级递归时需要记住相对目录
208
209
foreach (string folder in folders)
210
211
{
212
213
if (!ZipDirectory(folder, zipStream, Path.Combine(parentFolderName, Path.GetFileName(folderToZip)), createBaseFolder))
214
215
return
false;
216
217
}
218
219
return result;
220
221
}
222
223
224
225
///
<summary>
226
227
/// 压缩文件夹
228
229
///
</summary>
230
231
///
<param name="folderToZip">要压缩的文件夹路径</param>
232
233
///
<param name="zipedFile">压缩文件完整路径</param>
234
235
///
<param name="password">密码</param>
236
237
///
<returns>是否压缩成功</returns>
238
239
public
bool ZipDirectory(string folderToZip, string zipedFile, string password, bool includeFloderName = true)
240
241
{
242
243
bool result = false;
244
245
if (!Directory.Exists(folderToZip))
246
247
return result;
248
249
250
251 ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));
252
253
zipStream.SetLevel(Level);
254
255
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
256
257
258
259 result = ZipDirectory(folderToZip, zipStream, "", includeFloderName);
260
261
262
263
zipStream.Finish();
264
265
zipStream.Close();
266
267
268
269
return result;
270
271
}
272
273
274
275
///
<summary>
276
277
/// 压缩文件夹
278
279
///
</summary>
280
281
///
<param name="folderToZip">要压缩的文件夹路径</param>
282
283
///
<param name="zipedFile">压缩文件完整路径</param>
284
285
///
<returns>是否压缩成功</returns>
286
287
public
bool ZipDirectory(string folderToZip, string zipedFile, bool includeFloderName = true)
288
289
{
290
291
bool result = ZipDirectory(folderToZip, zipedFile, "", includeFloderName);
292
293
return result;
294
295
}
296
297
298
299
///
<summary>
300
301
/// 压缩文件
302
303
///
</summary>
304
305
///
<param name="fileToZip">要压缩的文件全名</param>
306
307
///
<param name="zipedFile">压缩后的文件名</param>
308
309
///
<param name="password">密码</param>
310
311
///
<returns>压缩结果</returns>
312
313
public
bool ZipFile(string fileToZip, string zipedFile, string password)
314
315
{
316
317
bool result = true;
318
319 ZipOutputStream zipStream = null;
320
321 FileStream fs = null;
322
323 ZipEntry ent = null;
324
325
326
327
if (!File.Exists(fileToZip))
328
329
return
false;
330
331
332
333
try
334
335
{
336
337 fs = File.OpenRead(fileToZip);
338
339
byte[] buffer = new
byte[fs.Length];
340
341 fs.Read(buffer, 0, buffer.Length);
342
343
fs.Close();
344
345
346
347 fs = File.Create(zipedFile);
348
349 zipStream = new ZipOutputStream(fs);
350
351
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
352
353 ent = new ZipEntry(Path.GetFileName(fileToZip));
354
355
zipStream.PutNextEntry(ent);
356
357
zipStream.SetLevel(Level);
358
359
360
361 zipStream.Write(buffer, 0, buffer.Length);
362
363
364
365
}
366
367
catch
368
369
{
370
371 result = false;
372
373
}
374
375
finally
376
377
{
378
379
if (zipStream != null)
380
381
{
382
383
zipStream.Finish();
384
385
zipStream.Close();
386
387
|
请发表评论