C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个通用的类,这样在工作中可以快速的完成压缩和解压缩的动作哦

官网下载地址:  http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用;

2. 在需要使用到ICSharpCode.SharpZipLib中定义的类的编码界面中将其导入(Imports)

 1 using ICSharpCode.SharpZipLib.Zip;
2 using System;
3 using System.IO;
4
5 namespace ZTO.WayBill.Utilities
6 {
7 /// <summary>
8 /// 压缩类
9 /// http://www.cnblogs.com/kissdodog/p/3525295.html
10
11 /// </summary>
12 public class ZipHelper
13 {
14 /// <summary>
15 /// 压缩文件夹
16 /// </summary>
17 /// <param name="source">源目录</param>
18 /// <param name="s">ZipOutputStream对象</param>
19 public static void Compress(string source, ZipOutputStream s)
20 {
21 string[] filenames = Directory.GetFileSystemEntries(source);
22 foreach (string file in filenames)
23 {
24 if (Directory.Exists(file))
25 {
26 // 递归压缩子文件夹
27 Compress(file, s);
28 }
29 else
30 {
31 using (FileStream fs = File.OpenRead(file))
32 {
33 byte[] buffer = new byte[4 * 1024];
34 // 此处去掉盘符,如D:\123\1.txt 去掉D:
35 ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file), ""));
36 entry.DateTime = DateTime.Now;
37 s.PutNextEntry(entry);
38 int sourceBytes;
39 do
40 {
41 sourceBytes = fs.Read(buffer, 0, buffer.Length);
42 s.Write(buffer, 0, sourceBytes);
43 } while (sourceBytes > 0);
44 }
45 }
46 }
47 }
48
49 /// <summary>
50 /// 解压缩
51 /// </summary>
52 /// <param name="sourceFile">压缩包完整路径地址</param>
53 /// <param name="targetPath">解压路径是哪里</param>
54 /// <returns></returns>
55 public static bool Decompress(string sourceFile, string targetPath)
56 {
57 if (!File.Exists(sourceFile))
58 {
59 throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));
60 }
61 if (!Directory.Exists(targetPath))
62 {
63 Directory.CreateDirectory(targetPath);
64 }
65 using (var s = new ZipInputStream(File.OpenRead(sourceFile)))
66 {
67 ZipEntry theEntry;
68 while ((theEntry = s.GetNextEntry()) != null)
69 {
70 if (theEntry.IsDirectory)
71 {
72 continue;
73 }
74 string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));
75 string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
76 if (!Directory.Exists(directorName))
77 {
78 Directory.CreateDirectory(directorName);
79 }
80 if (!String.IsNullOrEmpty(fileName))
81 {
82 using (FileStream streamWriter = File.Create(fileName))
83 {
84 int size = 4096;
85 byte[] data = new byte[size];
86 while (size > 0)
87 {
88 streamWriter.Write(data, 0, size);
89 size = s.Read(data, 0, data.Length);
90 }
91 }
92 }
93 }
94 }
95 return true;
96 }
97 }
98 }
 
 
分类: 后台开发日记

C# ICSharpCode.SharpZipLib的更多相关文章

  1. ICSharpCode.SharpZipLib 压缩、解压文件 附源码

    http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...

  2. 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature

    写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...

  3. ICSharpCode.SharpZipLib.dll 移植WP

    由于众所周知的原因. ICSharpCode.SharpZipLib.dll在Unity移植WP的时候出现诸多API不兼容,解决方案是在在Github上面找ICSharpCode.SharpZipLi ...

  4. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  5. C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩

    这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http://icsharpcode.github.io/SharpZipLib/ 1.单个或多个文件加 ...

  6. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

    最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...

  7. ICSharpCode.SharpZipLib

    ICSharpCode.SharpZipLib 压缩.解压文件 附源码   http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZipli ...

  8. 未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=n

    这个可能是因为,缺少文件ICSharpCode.SharpZipLib.dll文件. 我从网上下载了个dll文件,放到根目录中自己好了.

  9. npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案

    项目中一直使用NPOI与memcached,一直相安无事,但是最近升级了npoi到最新版本,发生了ICSharpCode.SharpZipLib的版本冲突问题. 因为此前一直使用的是NPOI的1.x的 ...

  10. C#壓縮文件幫助類 使用ICSharpCode.SharpZipLib.dll

    using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using System; using Syst ...

随机推荐

  1. 将DataTable 数据插入 SQL SERVER 数据库

    原文:将DataTable 数据插入 SQL SERVER 数据库 以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库: 一:使用sqlcommand.executenon ...

  2. 白板编程浅谈——Why, What, How(转)

    原文链接:http://lucida.me/blog/whiteboard-coding-demystified/ 这篇文章节选自我正在撰写的一本关于应届生面试求职的书籍,欢迎在评论或微博(@peng ...

  3. BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS

    标题效果:特定n点.涵盖所有的点与同方三面.斧头要求方垂直边界,最小平方的需求方长值 最大值至少.答案是很明显的二分法 但验证是一个问题 考虑仅仅有三个正方形,故用一个最小矩形覆盖这三个正方形时至少有 ...

  4. 【rman,1】经典案例增量备份

    一.备份策略: 1.星期天晚上      -level 0 backup performed(全备份) 2.星期一晚上      -level 2 backup performed 3.星期二晚上   ...

  5. vs2012 网站无法使用自定义服务器的解决方法

    我已经习惯新建一个Asp.net网站时把它挂载在IIS下调试运行,在使用Visual Studio 2012后,新建网站配置启动选项时,自定义服务器居然不可用 原来是Visual Studio 201 ...

  6. 【转】JTree调用UpdateUI()抛空指针

    最近在做东西时发现,当在线程里面刷新JTree时,有时会出现空指针错误,在网上查了很久,终于发现以下解决方法: JTree中调用UpdateUI()报以下错误 java.lang.NullPointe ...

  7. IOS 多于UIImageView 当加载较大的高清闪存管理

    当我们是一家人View  多于UIImageView,和UIImageView表明一个更大的HD,可能存在的存储器的警告的问题.假设第一次走进这个view,无记忆出现预警.当重新进入view,在那曾经 ...

  8. YUV摘要格式

    始终保持视频数据YUV联系,因为基于其产品的组织不同的公司格不同的类型定义,派生出多种不同YUV格公式. 大体,YUV从在点的形式的像素组织中,有两个:1.planer状态.2.packet状态. p ...

  9. NuttX 介绍

    (嵌入式 实时操作系统 rtos nuttx 7.1) NuttX 介绍 转载请注明出处:http://blog.csdn.net/zhumaill/article/details/24197637 ...

  10. 如何使用autolayout的UIView加入动画

    hi,all: 在经过了一番犹豫之后,我决定将我自己做的这个小APP的源代码发布给大家: 其出发点是和大家一起学习iOS开发,仅供学习參考之用. 之前代码是托管与gitlab 上的.今天我将其pull ...