使用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. WebDriverWait类以及类常用的方法

    WebDriverWait类提供了显式等待和隐式等待,显式等待的等待时间是固定的,固定了10s就必须等待10s,隐式等待的等待时间是个范围,例如最大10s,那么如果在3s的时候程序达到预期的结果,那么 ...

  2. ImportError: Could not import PIL.Image.

    pip install pillow

  3. 11.jQuery之淡入淡出效果

    知识点:fadeIn   fadeOut  fadeToggle  fadeTo <style> div { width: 150px; height: 300px; background ...

  4. java 实现傅立叶变换算法 及复数的运算

    最近项目需求,需要把python中的算法移植到java上,其中有一部分需要用到复数的运算和傅立叶变换算法,废话不多说 如下: package qrs; /** * 复数的运算 * */ public ...

  5. 基于新版 node 的 vue 脚手架搭建

    1. node 安装版本 9+ 2. 命令行 创建方式   vue create project 3. 可视化 创建方式  vue ui 4. 扩展 goole 下 vue 调试工具安装 git 资源 ...

  6. MHA ssh检查,repl复制检查和在线切换日志分析

    一.SSh 检查日志分析 执行过程及对应的日志: 1.读取MHA manger 节点上的配置文件 2.根据配置文件,得到各个主机的信息,逐一进行SSH检查 3.每个主机都通过SSH连接除了自己以外的其 ...

  7. linux 桥接模式下 固定ip 设置

    DEVICE=eht0   #网卡名称BOOTPROTO=none #关闭自动获取  dhcp  IPADDR=192.168.0.178   #ip地址GATEWAY=192.168.0.1  DN ...

  8. framebuffer测试程序

    /* framebuffer简单测试程序 网上转载 很多次 的程序 :-) */ #include <stdio.h> #include <stdlib.h> #include ...

  9. mysql5.7二进制包进行多实例安装

    一.需求 在一台服务器上安装mysql5.7,并且部署两个实例:3306用于本机主库,3307用于其他MYSQL服务器的从库 二.下载mysql二进制包 [root@push-- src]# -lin ...

  10. #1055 ... sql_mode=only_full_group_by

    sql_mode=only_full_group_by 版权声明:本文为参考多篇博主文章,略作测试修改. 参考文章: 猿医生 的<5分钟学会MySQL-this is incompatible ...