使用SharpZIpLib写的压缩解压操作类,已测试。

 public class ZipHelper
{
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="directory"></param>
/// <param name="targetPath"></param>
public static void Zip(string directory, string targetPath)
{
using (var stream = new ZipOutputStream(File.OpenWrite(targetPath)))
{
Zip(directory, stream);
}
} private static void Zip(string directory, ZipOutputStream stream, string entryName = "")
{
//压缩文件
var files = Directory.GetFiles(directory);
var buffer = new byte[];
foreach (string file in files)
{
var entry = new ZipEntry(entryName + Path.GetFileName(file));
entry.DateTime = DateTime.Now;
stream.PutNextEntry(entry); using (var fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
stream.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
} //压缩子目录
var subDirectories = Directory.GetDirectories(directory);
foreach (var subDirectory in subDirectories)
{
var subEntryName = Path.GetFileName(subDirectory) + "/";
var entry = new ZipEntry(subEntryName);
entry.DateTime = DateTime.Now;
stream.PutNextEntry(entry);
stream.Flush(); Zip(subDirectory, stream, subEntryName);
}
} /// <summary>
/// 解压zip文件
/// </summary>
/// <param name="zipfilePath"></param>
/// <param name="targetDirectory"></param>
public static void Unzip(string zipfilePath, string targetDirectory)
{
if (!File.Exists(zipfilePath))
return; if(!Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory); using (var stream = new ZipInputStream(File.OpenRead(zipfilePath)))
{
ZipEntry ent = null;
while ((ent = stream.GetNextEntry()) != null)
{
if (string.IsNullOrEmpty(ent.Name))
continue; var path = Path.Combine(targetDirectory, ent.Name);
path = path.Replace('/', '\\'); //创建目录
if (path.EndsWith("\\"))
{
Directory.CreateDirectory(path);
continue;
} //创建文件
using (var fs = File.Create(path))
{
var buffer = new byte[];
var lengthRead = ;
while ((lengthRead = stream.Read(buffer, , buffer.Length)) > )
fs.Write(buffer, , lengthRead);
} }
}
} }

使用SharpZIpLib写的压缩解压操作类的更多相关文章

  1. Linux 压缩解压操作

    Linux 压缩解压操作 Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的 ...

  2. [No0000DF]C# ZipFileHelper ZIP类型操作,压缩解压 ZIP 类封装

    using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...

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

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

  4. GZip 压缩解压 工具类 [ GZipUtil ]

    片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...

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

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

  6. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  7. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

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

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

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

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

随机推荐

  1. npm 关联 git包

    npm 关联 git包 由于现在项目越做越多,很多公共的部分相互公用,需要尽可能早地提炼出来,这样便可以在其他项目进行引用,而不是每次建一个项目就需要进行拷贝,这样太痛苦了,因而想通过类似npm包管理 ...

  2. RabbitMQ入门教程(四):工作队列(Work Queues)

    原文:RabbitMQ入门教程(四):工作队列(Work Queues) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https:/ ...

  3. AQS之Condition

    一.引言 一般我们在使用锁的Condition时,我们一般都是这么使用,以ReentrantLock为例, ReentrantLock lock = new ReentrantLock(); Cond ...

  4. window对象open方法详解

    window.open详解 window.open("sUrl","sName","sFeature","bReplace&quo ...

  5. servlet和Struts2的线程安全性对比

    1.>在servlet中,定义成员变量是不安全的,,因为,每次请求操作的是该同一个成员变量,,会出现线程不安全的问题. 2.>而在struts2中,在Action中定义成员变量是安全的,, ...

  6. vue动态渲染图片,引用路径需要注意的地方

    1.把图片放在和src同级的static里面,这用按照正常的方式进行引入,例如: 2.图片可以在其他文件夹,但是在script引入是必须加上require <img :src="ite ...

  7. vue-mixins和vue高阶组件

    我们在开发过程中,因为需求的变更,往往会遇见对现有组件的改造和扩展. 那么我们有什么方法对现有组件进行改造和扩展呢? 常见的我们可以使用mixins方式 下面就让我们来看一下怎么使用mixins方式对 ...

  8. JS预解释

    1.声明(declare)  var num   // 告诉浏览器在全局作用域中有一个num变量 定义(defined) num = 12 // 给我们的比变量进行赋值 2.var:在预解释时只是提前 ...

  9. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'PDT' is.......

    SpringBoot连接数据库的时候报错 java.sql.SQLException: The server time zone value 'PDT' is unrecognized or repr ...

  10. Visual Studio Code python 代码快速自动提示

    1.file --> setting->设置 搜索 python 或者auto_complete setting.json { "explorer.confirmDelete&q ...