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

我们需要dll 在官网上也有,也可以从百度网盘下载

好了,深入的大家还要多多研究,今天我们简单介绍一下 简单的 单文件、文件夹的压缩和解压

先给大家看一下效果:

一、引入ICSharpCode.SharpZipLib

我们新建个帮助类 ZipHelper.cs  然后 添加 dll 引用

二、添加完dll引用之后 我们 需要添加 这几个Using引用

 using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;4 using System.IO;

三、压缩单个文件

这里我添加了几个参数 大家可以根据自己的需要 修改一下

 /// <summary>
/// ZIP:压缩单个文件
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="FileToZip">需要压缩的文件(绝对路径)</param>
/// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
/// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
/// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
/// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
/// <param name="IsEncrypt">是否加密(默认 加密)</param>
public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = , int BlockSize = , bool IsEncrypt = true)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
} //文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + ); ZipEntry ZipEntry = new ZipEntry(fileName); if (IsEncrypt)
{
//压缩文件加密
ZipStream.Password = “123”;
} ZipStream.PutNextEntry(ZipEntry); //设置压缩级别
ZipStream.SetLevel(CompressionLevel); //缓存大小
byte[] buffer = new byte[BlockSize]; int sizeRead = ; try
{
do
{
sizeRead = StreamToZip.Read(buffer, , buffer.Length);
ZipStream.Write(buffer, , sizeRead);
}
while (sizeRead > );
}
catch (System.Exception ex)
{
throw ex;
} StreamToZip.Close();
} ZipStream.Finish();
ZipStream.Close();
} ZipFile.Close();
}
}

四、压缩文件夹

 /// <summary>
/// ZIP:压缩文件夹
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
/// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
/// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
/// <param name="IsEncrypt">是否加密(默认 加密)</param>
public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
{
//如果目录不存在,则报错
if (!System.IO.Directory.Exists(DirectoryToZip))
{
throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
} //文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
if (IsEncrypt)
{
//压缩文件加密
s.Password = “123”;
}
ZipSetp(DirectoryToZip, s, "");
}
}
}
/// <summary>
/// 递归遍历目录
/// add yuangang by 2016-06-13
/// </summary>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - ] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录
{ if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + );
pPath += "\\";
ZipSetp(file, s, pPath);
} else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{ byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now;
entry.Size = fs.Length; fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value;
s.PutNextEntry(entry); s.Write(buffer, , buffer.Length);
}
}
}
}

五、解压一个ZIP文件

  /// <summary>
/// ZIP:解压一个zip文件
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
/// <param name="TargetDirectory">解压到的目录</param>
/// <param name="Password">解压密码</param>
/// <param name="OverWrite">是否覆盖已存在的文件</param>
public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
{
//如果解压到的目录不存在,则报错
if (!System.IO.Directory.Exists(TargetDirectory))
{
throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
}
//目录结尾
if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; } using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
{
zipfiles.Password = Password;
ZipEntry theEntry; while ((theEntry = zipfiles.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name; if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(TargetDirectory + directoryName); if (fileName != "")
{
if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = zipfiles.Read(data, , data.Length); if (size > )
streamWriter.Write(data, , size);
else
break;
}
streamWriter.Close();
}
}
}
} zipfiles.Close();
}
}

原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

ICSharpCode.SharpZipLib 压缩、解压文件 附源码的更多相关文章

  1. .NET使用ICSharpCode.SharpZipLib压缩/解压文件

    SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...

  2. ICSharpCode.SharpZipLi 压缩、解压文件 附源码

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

  3. ICSharpCode.SharpZipLib压缩解压

    一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...

  4. C#使用SharpZipLib压缩解压文件

    #region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> // ...

  5. 通过SharpZipLib来压缩解压文件

    在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...

  6. SharpZipLib压缩解压

    一.介绍 SharpZipLib是一个完全由C#编写的ZIP,GZIP,Tar和BZIP2 Library,可以方便的支持这几种格式的压缩和解压缩. https://github.com/icshar ...

  7. huffman压缩解压文件【代码】

    距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...

  8. 【转载】.NET压缩/解压文件/夹组件

    转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...

  9. SharpZipLib压缩解压的使用

    项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...

随机推荐

  1. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  2. 免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)

    很多的软件项目中都会使用到定时任务.定时轮询数据库同步,定时邮件通知等功能..NET Framework具有“内置”定时器功能,通过System.Timers.Timer类.在使用Timer类需要面对 ...

  3. 设计模式之创建类模式大PK

                                        创建类模式大PK 创建类模式包括工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式,他们能够提供对象的创建和管理职责.其 ...

  4. Kooboo CMS技术文档之三:切换数据存储方式

    切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...

  5. 移动应用App测试与质量管理一

    测试工程师 基于Html的WebApp测试, 现在一些移动App混Html5 HTML5性能测试 兼容性 整理后的脑图 测试招聘 弱化大量技术考察 看重看问题的高度 看重潜力 测试经验 质量管理 专项 ...

  6. js 基础篇(点击事件轮播图的实现)

    轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...

  7. Android Retrofit 2.0 使用-补充篇

    推荐阅读,猛戳: 1.Android MVP 实例 2.Android Retrofit 2.0使用 3.RxJava 4.RxBus 5.Android MVP+Retrofit+RxJava实践小 ...

  8. Open-Test 测试驱动模式与版本号管理机制

    以测试用例驱动项目开发,coding/case俩条线并走模式.   1.开发人员只负责功能实现:   2.测试人员提供自测用例,研发人员jenkins持续集成项目后自动化执行自测用例,通过后方可转测试 ...

  9. (转) 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    原文地址: http://www.cnblogs.com/lyhabc/p/4682986.html 这一篇是从0开始搭建SQL Server AlwaysOn 的第三篇,这一篇才真正开始搭建Alwa ...

  10. JavaScript

    2015-08-01 16:20 JavaScript使用时需要注意的地方 1.引入JS的位置:最好的做法是把<script>的标签放到HTML文档的最后.</body>标签之 ...