参考

https://blog.csdn.net/lki_suidongdong/article/details/20942977

重点:

实现多级子目录的压缩,类似winrar,可以选择是否排除基准目录

 1publicvoid ZipDirectoryTest()
 2{
 3string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), DateTime.Now.Ticks.ToString());
 4foreach (string sub innewstring[] { "bin", "release", "test", "test\\bin", "" })
 5{
 6string subPath = System.IO.Path.Combine(path, sub);
 7if (!System.IO.Directory.Exists(subPath))
 8System.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}
15Console.WriteLine(path);
16
17new 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 }

代码

  1using System;
  2
  3using System.Collections.Generic;
  4
  5using System.Linq;
  6
  7using System.Text;
  8
  9using System.IO;
 10
 11using ICSharpCode.SharpZipLib;
 12
 13using ICSharpCode.SharpZipLib.Zip;
 14
 15#if NETSTANDARD2_0
 16
 17using ICSharpCode.SharpZipLib.Checksum;
 18
 19#else
 20
 21using ICSharpCode.SharpZipLib.Checksums;
 22
 23#endif
 24
 25
 26
 27namespace Q.Helper.Zip
 28
 29{
 30
 31
 32
 33///<summary>
 34
 35/// 适用与ZIP压缩
 36
 37///</summary>
 38
 39publicclass ZipHelper
 40
 41{
 42
 43publicint Level
 44
 45{
 46
 47get; 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
 71privatebool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName, bool createBaseFolder = true)
 72
 73{
 74
 75 folderToZip = folderToZip.Replace("\\", "/");
 76
 77bool result = true;
 78
 79string[] folders, files;
 80
 81 ZipEntry ent = null;
 82
 83 FileStream fs = null;
 84
 85 Crc32 crc = new Crc32();
 86
 87
 88
 89try
 90
 91{
 92
 93string entPath = Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/").Replace("\\", "/");
 94
 95if (!createBaseFolder)
 96
 97 entPath = entPath.Substring(entPath.IndexOf("/") + 1);
 98
 99if (!string.IsNullOrEmpty(entPath))
100
101{
102
103 ent = new ZipEntry(entPath);
104
105Console.WriteLine(entPath);
106
107zipStream.PutNextEntry(ent);
108
109zipStream.Flush();
110
111}
112
113 files = Directory.GetFiles(folderToZip);
114
115foreach (string file in files)
116
117{
118
119 fs = File.OpenRead(file);
120
121
122
123byte[] buffer = newbyte[fs.Length];
124
125 fs.Read(buffer, 0, buffer.Length);
126
127 entPath = Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)).Replace("\\", "/");
128
129if (!createBaseFolder)
130
131 entPath = entPath.Substring(entPath.IndexOf("/") + 1);
132
133Console.WriteLine(entPath);
134
135 ent = new ZipEntry(entPath);
136
137 ent.DateTime = DateTime.Now;
138
139 ent.Size = fs.Length;
140
141
142
143fs.Close();
144
145
146
147crc.Reset();
148
149crc.Update(buffer);
150
151
152
153 ent.Crc = crc.Value;
154
155zipStream.PutNextEntry(ent);
156
157 zipStream.Write(buffer, 0, buffer.Length);
158
159}
160
161
162
163}
164
165catch (Exception ex)
166
167{
168
169 result = false;
170
171throw ex;
172
173}
174
175finally
176
177{
178
179if (fs != null)
180
181{
182
183fs.Close();
184
185fs.Dispose();
186
187}
188
189if (ent != null)
190
191{
192
193 ent = null;
194
195}
196
197GC.Collect();
198
199 GC.Collect(1);
200
201}
202
203
204
205 folders = Directory.GetDirectories(folderToZip);
206
207//多级递归时需要记住相对目录
208
209foreach (string folder in folders)
210
211{
212
213if (!ZipDirectory(folder, zipStream, Path.Combine(parentFolderName, Path.GetFileName(folderToZip)), createBaseFolder))
214
215returnfalse;
216
217}
218
219return 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
239publicbool ZipDirectory(string folderToZip, string zipedFile, string password, bool includeFloderName = true)
240
241{
242
243bool result = false;
244
245if (!Directory.Exists(folderToZip))
246
247return result;
248
249
250
251 ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));
252
253zipStream.SetLevel(Level);
254
255if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
256
257
258
259 result = ZipDirectory(folderToZip, zipStream, "", includeFloderName);
260
261
262
263zipStream.Finish();
264
265zipStream.Close();
266
267
268
269return 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
287publicbool ZipDirectory(string folderToZip, string zipedFile, bool includeFloderName = true)
288
289{
290
291bool result = ZipDirectory(folderToZip, zipedFile, "", includeFloderName);
292
293return 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
313publicbool ZipFile(string fileToZip, string zipedFile, string password)
314
315{
316
317bool result = true;
318
319 ZipOutputStream zipStream = null;
320
321 FileStream fs = null;
322
323 ZipEntry ent = null;
324
325
326
327if (!File.Exists(fileToZip))
328
329returnfalse;
330
331
332
333try
334
335{
336
337 fs = File.OpenRead(fileToZip);
338
339byte[] buffer = newbyte[fs.Length];
340
341 fs.Read(buffer, 0, buffer.Length);
342
343fs.Close();
344
345
346
347 fs = File.Create(zipedFile);
348
349 zipStream = new ZipOutputStream(fs);
350
351if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
352
353 ent = new ZipEntry(Path.GetFileName(fileToZip));
354
355zipStream.PutNextEntry(ent);
356
357zipStream.SetLevel(Level);
358
359
360
361 zipStream.Write(buffer, 0, buffer.Length);
362
363
364
365}
366
367catch
368
369{
370
371 result = false;
372
373}
374
375finally
376
377{
378
379if (zipStream != null)
380
381{
382
383zipStream.Finish();
384
385zipStream.Close();
386
387}
388
389if (ent != null)
390
391{
392
393 ent = null;
394
395}
396
397if (fs != null)
398
399{
400
401fs.Close();
402
403fs.Dispose();
404
405}
406
407}
408
409GC.Collect();
410
411 GC.Collect(1);
412
413
414
415return result;
416
417}
418
419
420
421///<summary>
422
423/// 压缩文件
424
425///</summary>
426
427///<param name="fileToZip">要压缩的文件全名</param>
428
429///<param name="zipedFile">压缩后的文件名</param>
430
431///<returns>压缩结果</returns>
432
433publicbool ZipFile(string fileToZip, string zipedFile)
434
435{
436
437bool result = ZipFile(fileToZip, zipedFile, null);
438
439return result;
440
441}
442
443
444
445///<summary>
446
447/// 压缩文件或文件夹
448
449///</summary>
450
451///<param name="fileToZip">要压缩的路径</param>
452
453///<param name="zipedFile">压缩后的文件名</param>
454
455///<param name="password">密码</param>
456
457///<returns>压缩结果</returns>
458
459publicbool Zip(string fileToZip, string zipedFile, string password)
460
461{
462
463bool result = false;
464
465if (Directory.Exists(fileToZip))
466
467 result = ZipDirectory(fileToZip, zipedFile, password);
468
469elseif (File.Exists(fileToZip))
470
471 result = ZipFile(fileToZip, zipedFile, password);
472
473
474
475return result;
476
477}
478
479
480
481///<summary>
482
483/// 压缩文件或文件夹
484
485///</summary>
486
487///<param name="fileToZip">要压缩的路径</param>
488
489///<param name="zipedFile">压缩后的文件名</param>
490
491///<returns>压缩结果</returns>
492
493publicbool Zip(string fileToZip, string zipedFile)
494
495{
496
497bool result = Zip(fileToZip, zipedFile, null);
498
499return result;
500
501
502
503}
504
505
506
507#endregion
508
509
510
511#region 解压
512
513
514
515///<summary>
516
517/// 解压功能(解压压缩文件到指定目录)
518
519///</summary>
520
521///<param name="fileToUnZip">待解压的文件</param>
522
523///<param name="zipedFolder">指定解压目标目录</param>
524
525///<param name="password">密码</param>
526
527///<returns>解压结果</returns>
528
529publicstaticbool UnZip(string fileToUnZip, string zipedFolder, string password)
530
531{
532
533bool result = true;
534
535
536
537 ZipInputStream zipStream = null;
538
539 ZipEntry ent = null;
540
541string fileName;
542
543
544
545if (!File.Exists(fileToUnZip))
546
547returnfalse;
548
549
550
551if (!Directory.Exists(zipedFolder))
552
553Directory.CreateDirectory(zipedFolder);
554
555
556
557try
558
559{
560
561 zipStream = new ZipInputStream(File.OpenRead(fileToUnZip));
562
563if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
564
565while ((ent = zipStream.GetNextEntry()) != null)
566
567{
568
569if (!string.IsNullOrEmpty(ent.Name))
570
571{
572
573 fileName = Path.Combine(zipedFolder, ent.Name);
574
575 fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi
576
577
578
579if (fileName.EndsWith("\\"))
580
581{
582
583Directory.CreateDirectory(fileName);
584
585continue;
586
587}
588
589using (FileStream fs = File.Create(fileName))
590
591{
592
593int size = 2048;
594
595byte[] data = newbyte[size];
596
597while (true)
598
599{
600
601
602
603 size = zipStream.Read(data, 0, data.Length);
604
605if (size > 0)
606
607 fs.Write(data, 0, data.Length);
608
609else
610
611break;
612
613}
614
615fs.Flush();
616
617
618
619fs.Close();
620
621new FileInfo(fileName).LastWriteTime = ent.DateTime;
622
623}
624
625
626
627}
628
629}
630
631}
632
633catch
634
635{
636
637 result = false;
638
639}
640
641finally
642
643{
644
645
646
647if (zipStream != null)
648
649{
650
651zipStream.Close();
652
653zipStream.Dispose();
654
655}
656
657if (ent != null)
658
659{
660
661 ent = null;
662
663}
664
665GC.Collect();
666
667 GC.Collect(1);
668
669}
670
671return result;
672
673}
674
675
676
677///<summary>
678
679/// 解压功能(解压压缩文件到指定目录)
680
681///</summary>
682
683///<param name="fileToUnZip">待解压的文件</param>
684
685///<param name="zipedFolder">指定解压目标目录</param>
686
687///<returns>解压结果</returns>
688
689publicstaticbool UnZip(string fileToUnZip, string zipedFolder)
690
691{
692
693bool result = UnZip(fileToUnZip, zipedFolder, null);
694
695return result;
696
697}
698
699
700
701#endregion
702
703}
704
705 }

转载于:https://www.cnblogs.com/QinQouShui/p/9276781.html

C#实现多级子目录Zip压缩解压实例相关推荐

  1. Java中zip压缩解压

    1. 解压问题 360压缩文件 使用jdk API 读取压缩文件后解压,报错 java.lang.IllegalArgumentException:MALFORMED 如果是好压压缩文件,使用jdk ...

  2. android 解压加密zip,zip压缩解压加密器

    zip压缩解压加密器是一款非常好用的手机文件压缩解压应用软件,zip压缩解压加密器app为用户提供了各种文件压缩功能,可以批量操作,一键解压文件.感兴趣的朋友欢迎使用西西下载! 基本内容 zip压缩解 ...

  3. zip包怎么解压oracle,使用jar与zip压缩解压文件的区别

    使用jar命令压缩和解压文件不会继承原来的权限,切记! 而使用zip/unzip压缩解压文件则会保留文件原来的权限等信息,因此使用压缩解压的时候尽量使用专业的工具 下面是测试内容和结果: 1.首先确认 ...

  4. linux下gz和tar.gz、zip压缩解压

    文章目录 说明 分享 tar.gz 常用命令 gz 常用命令 zip 常用命令 总结 说明 本博客每周五更新一次. 日常数据处理中,经常需要压缩数据文件,减小传输带宽,方便分享和存储,整理gz.tar ...

  5. python3 zipfile模块 zip压缩解压

    一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...

  6. java zip压缩解压_JAVA实现实用的ZIP压缩与解压

    程序实现了ZIP压缩.共分为2部分 : 压缩(compression)与解压(decompression) 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压. ...

  7. 【Unity】Zip压缩 解压 文件介绍

    写在前面 我们在打包的时候unity会自动的把我们在场景中用到的资源与一些特殊文件夹中的资源压缩,但是如果我们需要做热更或者还想减少包的大小,虽然AssetBundles是可以为我们压缩的,但是可以把 ...

  8. java 分卷压缩_Apache Commons Compress介绍-Zip压缩解压

    Zip格式应该是最出名的压缩格式之一了,zlib.gzip这些辈分很老的库大家应该都用过,甚至大部分其他格式的压缩库,都可以处理zip格式.Commons Compress当然也少补了对zip格式的支 ...

  9. Linux下gz和tar.gz、与Windows天zip压缩解压

    tar.gz tar.gz是linux下常用文件或文件夹打包和压缩方式,它既支持打包,也支持压缩,linux下应用广泛. 常用命令: 打包文件,不压缩:tar -cvf 压缩文件名.tar 待压缩文件 ...

最新文章

  1. 使用git pull文件时和本地文件冲突怎么办?
  2. Java 网络编程(超级详细)
  3. 【华为云技术分享】【DevCloud · 敏捷智库】如何利用核心概念解决估算常见问题
  4. HTML5 文档定义Doctype
  5. 无人驾驶插秧机智能辅助系统_无人驾驶插秧机搭载北斗导航驾驶系统,误差仅在2.5厘米内...
  6. 管理历程篇---学会四心
  7. HCIE理论-IPV6
  8. matpower在matlab里面吗,matpower matlab 238万源代码下载- www.pudn.com
  9. 互联网晚报 | 12月5日 星期日 | 饿了么启用AI技术研发新菜品;搜狐将布局知识直播;《英雄联盟手游》获选苹果年度游戏...
  10. SpringBoot整合Magic-Api
  11. 制作透明背景图片,按钮
  12. 本地erp与云erp系统的差别
  13. Python——OCR识别
  14. 20221106EXCEL/腾讯文档 部分常用函数嵌套用法总结
  15. 基于WordPress搭建个人网站
  16. vim、用户管理、所有者所属组管理、监控和管理进程、服务管理
  17. 北京住房公积金联名卡查询使用
  18. 将阿拉伯数字转换成汉字数字(小写)
  19. java输入年月日,输出这是这一年的第几天
  20. 2022网络搭建国赛公开题mail服务器搭建

热门文章

  1. 最长公共子序列模板(LCS)和LICS模板
  2. 数据分析系统DIY1/3:CentOS7+MariaDB安装纪实
  3. JQuery实现——黑客帝国代码雨效果
  4. phoneGap 中修改生成APP的名字
  5. DropDownList实现无限分级
  6. Codewars-The wheat/rice and chessboard problem(棋盘放谷粒)
  7. Vue2.0 的漫长学习ing-1-5
  8. CC3200底板测试-烧写CC3200-LAUNCHXL
  9. 简答String类的操作特点以及static方法的注意事项
  10. Atitit.vod 视频播放系统 影吧系统的架构图 架构体系 解决方案