ICSharpCode.SharpZipLib
ICSharpCode.SharpZipLib 压缩、解压文件 附源码
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, BZip2 和Tar格式
我们需要dll 在官网上也有,也可以从百度网盘下载
好了,深入的大家还要多多研究,今天我们简单介绍一下 简单的 单文件、文件夹的压缩和解压
先给大家看一下效果:
一、引入ICSharpCode.SharpZipLib
我们新建个帮助类 ZipHelper.cs 然后 添加 dll 引用
二、添加完dll引用之后 我们 需要添加 这几个Using引用
1 using ICSharpCode.SharpZipLib.Checksums;
2 using ICSharpCode.SharpZipLib.Zip;
3 using System;4 using System.IO;
三、压缩单个文件
这里我添加了几个参数 大家可以根据自己的需要 修改一下

1 /// <summary>
2 /// ZIP:压缩单个文件
3 /// add yuangang by 2016-06-13
4 /// </summary>
5 /// <param name="FileToZip">需要压缩的文件(绝对路径)</param>
6 /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
7 /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
8 /// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
9 /// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
10 /// <param name="IsEncrypt">是否加密(默认 加密)</param>
11 public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = true)
12 {
13 //如果文件没有找到,则报错
14 if (!System.IO.File.Exists(FileToZip))
15 {
16 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
17 }
18
19 //文件名称(默认同源文件名称相同)
20 string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
21
22 using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
23 {
24 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
25 {
26 using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
27 {
28 string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1);
29
30 ZipEntry ZipEntry = new ZipEntry(fileName);
31
32 if (IsEncrypt)
33 {
34 //压缩文件加密
35 ZipStream.Password = “123”;
36 }
37
38 ZipStream.PutNextEntry(ZipEntry);
39
40 //设置压缩级别
41 ZipStream.SetLevel(CompressionLevel);
42
43 //缓存大小
44 byte[] buffer = new byte[BlockSize];
45
46 int sizeRead = 0;
47
48 try
49 {
50 do
51 {
52 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
53 ZipStream.Write(buffer, 0, sizeRead);
54 }
55 while (sizeRead > 0);
56 }
57 catch (System.Exception ex)
58 {
59 throw ex;
60 }
61
62 StreamToZip.Close();
63 }
64
65 ZipStream.Finish();
66 ZipStream.Close();
67 }
68
69 ZipFile.Close();
70 }
71 }

四、压缩文件夹

1 /// <summary>
2 /// ZIP:压缩文件夹
3 /// add yuangang by 2016-06-13
4 /// </summary>
5 /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
6 /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
7 /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
8 /// <param name="IsEncrypt">是否加密(默认 加密)</param>
9 public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
10 {
11 //如果目录不存在,则报错
12 if (!System.IO.Directory.Exists(DirectoryToZip))
13 {
14 throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
15 }
16
17 //文件名称(默认同源文件名称相同)
18 string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
19
20 using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
21 {
22 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
23 {
24 if (IsEncrypt)
25 {
26 //压缩文件加密
27 s.Password = “123”;
28 }
29 ZipSetp(DirectoryToZip, s, "");
30 }
31 }
32 }
33 /// <summary>
34 /// 递归遍历目录
35 /// add yuangang by 2016-06-13
36 /// </summary>
37 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
38 {
39 if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
40 {
41 strDirectory += Path.DirectorySeparatorChar;
42 }
43 Crc32 crc = new Crc32();
44
45 string[] filenames = Directory.GetFileSystemEntries(strDirectory);
46
47 foreach (string file in filenames)// 遍历所有的文件和目录
48 {
49
50 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
51 {
52 string pPath = parentPath;
53 pPath += file.Substring(file.LastIndexOf("\\") + 1);
54 pPath += "\\";
55 ZipSetp(file, s, pPath);
56 }
57
58 else // 否则直接压缩文件
59 {
60 //打开压缩文件
61 using (FileStream fs = File.OpenRead(file))
62 {
63
64 byte[] buffer = new byte[fs.Length];
65 fs.Read(buffer, 0, buffer.Length);
66
67 string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
68 ZipEntry entry = new ZipEntry(fileName);
69
70 entry.DateTime = DateTime.Now;
71 entry.Size = fs.Length;
72
73 fs.Close();
74
75 crc.Reset();
76 crc.Update(buffer);
77
78 entry.Crc = crc.Value;
79 s.PutNextEntry(entry);
80
81 s.Write(buffer, 0, buffer.Length);
82 }
83 }
84 }
85 }

五、解压一个ZIP文件

1 /// <summary>
2 /// ZIP:解压一个zip文件
3 /// add yuangang by 2016-06-13
4 /// </summary>
5 /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
6 /// <param name="TargetDirectory">解压到的目录</param>
7 /// <param name="Password">解压密码</param>
8 /// <param name="OverWrite">是否覆盖已存在的文件</param>
9 public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
10 {
11 //如果解压到的目录不存在,则报错
12 if (!System.IO.Directory.Exists(TargetDirectory))
13 {
14 throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
15 }
16 //目录结尾
17 if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
18
19 using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
20 {
21 zipfiles.Password = Password;
22 ZipEntry theEntry;
23
24 while ((theEntry = zipfiles.GetNextEntry()) != null)
25 {
26 string directoryName = "";
27 string pathToZip = "";
28 pathToZip = theEntry.Name;
29
30 if (pathToZip != "")
31 directoryName = Path.GetDirectoryName(pathToZip) + "\\";
32
33 string fileName = Path.GetFileName(pathToZip);
34
35 Directory.CreateDirectory(TargetDirectory + directoryName);
36
37 if (fileName != "")
38 {
39 if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
40 {
41 using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
42 {
43 int size = 2048;
44 byte[] data = new byte[2048];
45 while (true)
46 {
47 size = zipfiles.Read(data, 0, data.Length);
48
49 if (size > 0)
50 streamWriter.Write(data, 0, size);
51 else
52 break;
53 }
54 streamWriter.Close();
55 }
56 }
57 }
58 }
59
60 zipfiles.Close();
61 }
62 }

原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com
ICSharpCode.SharpZipLib的更多相关文章
- ICSharpCode.SharpZipLib 压缩、解压文件 附源码
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...
- 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature
写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...
- ICSharpCode.SharpZipLib.dll 移植WP
由于众所周知的原因. ICSharpCode.SharpZipLib.dll在Unity移植WP的时候出现诸多API不兼容,解决方案是在在Github上面找ICSharpCode.SharpZipLi ...
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩
这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http://icsharpcode.github.io/SharpZipLib/ 1.单个或多个文件加 ...
- C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类
最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...
- 未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=n
这个可能是因为,缺少文件ICSharpCode.SharpZipLib.dll文件. 我从网上下载了个dll文件,放到根目录中自己好了.
- npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案
项目中一直使用NPOI与memcached,一直相安无事,但是最近升级了npoi到最新版本,发生了ICSharpCode.SharpZipLib的版本冲突问题. 因为此前一直使用的是NPOI的1.x的 ...
- C#壓縮文件幫助類 使用ICSharpCode.SharpZipLib.dll
using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using System; using Syst ...
随机推荐
- Git从零教你入门(4):Git服务之 gogs部署安装
Git从零入门系列4: 先看上一篇文章: http://www.51testing.com/index.php?uid-497177-action-viewspace-itemid-3706817 今 ...
- Javascript之旅——终点站:困惑的settimeout
有时候结局不是很美好,但起码这也算是一种结局,这个系列的最后一篇settimeout,这是一个让人困惑的函数,也是我一直在吐槽JS的 原因,我们看不到JS的源代码,setimeout同样也是,从始到终 ...
- 【转载】CentOS 6.4下Squid代理服务器的安装与配置
一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...
- 工作中常用的Linux命令:crontab命令
本文链接:http://www.cnblogs.com/MartinChentf/p/6060252.html (转载请注明出处) crontab是一个用来设置.删除或显示供守护进程cron执行的定时 ...
- 使用virt-manager创建和管理虚拟机
1.虚拟机管理程序和虚拟机管理 一个服务器上只安装单一操作系统的时代已经过去,单个服务器可通过安装多个虚拟机来运行不同操作系统.虚拟机的大量使用减少了所需的服务其硬件,降低了服务器的功耗,但却带来了另 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
- CF721C. Journey[DP DAG]
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- 第15章 设备无关位图_15.3 DIB和DDB的结合
第15章 设备相关位图_15.3 DIB和DDB的结合 15.3.1 从DIB创建DDB (1)hBitmap =CreateDIBitmap(…)——注意这名称会误导,实际上创建的是DDB 参数 说 ...
- 06章 Struts2国际化
1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素.换言之,应用程序的功 ...
- Unity arm64
ERROR ITMS-90086 ERROR ITMS-90086:"missing 64-bit support. beginning on february 1, 2015, new i ...