使用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. Linux中如何添加/删除FTP用户并设置权限

    在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被设置用户名为test.被限制路径为/home/test 2.创建建用户:在root用户下:   user ...

  2. Coursera机器学习笔记(一) - 监督学习vs无监督学习

    转载 http://daniellaah.github.io/2016/Machine-Learning-Andrew-Ng-My-Notes-Week-1-Introduction.html 一. ...

  3. vue-cli解决兼容ie的es6+api问题

    官网:https://cli.vuejs.org/zh/guide/browser-compatibility.html#usebuiltins-usage https://github.com/vu ...

  4. Python线程学习

    Python3 通过两个标准库 _thread 和 threading 提供对线程的支持. _thread 提供了低级别的.原始的线程以及一个简单的锁,它相比于 threading 模块的功能还是比较 ...

  5. 关于sharekey 与Open system+wep

    Open_system+wep与open_system的区别在于: 对于开放系统认证,在设置时启用WEP,此时,WEP用于在传输数据时加密,对于认证没有任何作用. 抓包open_system+wep: ...

  6. [uboot] (第三章)uboot流程——uboot-spl代码流程(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ...

  7. Python之网路编程之死锁,递归锁,信号量,Event事件,线程Queue

    一.死锁现象与递归锁 进程也是有死锁的 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用, 它们都将无法推进下去.此时称系统处于死锁状态或系统 ...

  8. 【leetcode】1229.Meeting Scheduler

    题目如下: 你是一名行政助理,手里有两位客户的空闲时间表:slots1 和 slots2,以及会议的预计持续时间 duration,请你为他们安排合适的会议时间. 「会议时间」是两位客户都有空参加,并 ...

  9. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  10. chrome scrollTop 获取失败问题及解决方案

    https://blog.csdn.net/h357650113/article/details/78384621