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 ...
随机推荐
- WPF学习之路(十二)控件(Items控件)
ListBox 提供了一个选项列表,可以固定或者动态绑定 <StackPanel> <GroupBox Margin="> <GroupBox.Header& ...
- 【转】JAVA 8 日期/时间(Date Time)API指南
前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...
- SQL挑战——如何高效生成编码
有这样一个需求:需要根据输入的编码(这个编码值来自于数据库的一个表)生成下一个编码,编码规则如下所示(我们暂且不关心这个逻辑是否合理,只关心如何实现): 1: 最小值为A0000, 最大值为ZZZZZ ...
- ERROR [org.apache.hadoop.security.UserGroupInformation] - PriviledgedActionExcep
换了个环境,出现此异常 016-10-18 23:54:01,334 WARN [org.apache.hadoop.util.NativeCodeLoader] - Unable to load n ...
- R语言数据的输入
键盘输入 调用edit函数,比如我们要让用户输入一个长度为5的向量并赋值给变量a,那么可以: a<-vector() a<-edit(a) 另外也可以用函数fix来直接编辑变量,而不需要再 ...
- 基础篇之 Create Type
Create Type 的话呢,是创建一个自定义的数据类型,等于说为常用的数据类型建造一个别名的样纸.然后就可以通用当前数据库的当前架构.(当然了,一般来说我们都是使用dbo架构,所以都会无事前面那个 ...
- Office——EXCEL 显示 隐藏 Ribbon 调用宏
==================================声明================================== 本文版权归作者所有 未经作者授权 请勿转载 保留法律追究的 ...
- 烂泥:通过binlog恢复mysql数据库
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 在上一篇文章,我们讲解了有关mysql的binlog日志的基础知识.这篇文章,我们来讲解如何通过mysql的binlog日志来恢复数据库. 在使用bin ...
- wc
$wc [-lwc] filename统计的文件的信息,缺省参数会按照lwc的顺序输出全部三种信息 -l统计文件的行数 -w统计文件的字数,字以空格和换行符分隔 -c统计文件的字符数,包括换行等 例子 ...
- Elasticsearch 安装中文分词
github地址:https://github.com/medcl/elasticsearch-analysis-ik 注意版本要对应,否则编译完成后elasticsearch不能正常启动 下载文件, ...