使用SharpZIpLib写的压缩解压操作类
使用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写的压缩解压操作类的更多相关文章
- Linux 压缩解压操作
Linux 压缩解压操作 Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的 ...
- [No0000DF]C# ZipFileHelper ZIP类型操作,压缩解压 ZIP 类封装
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- GZip 压缩解压 工具类 [ GZipUtil ]
片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...
- huffman压缩解压文件【代码】
距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...
- (转载)C#压缩解压zip 文件
转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...
- 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 重点: 实现多级子目录的压缩, ...
- .NET使用ICSharpCode.SharpZipLib压缩/解压文件
SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...
- 通过SharpZipLib来压缩解压文件
在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...
随机推荐
- Javaweb实训-宠物医院-社区宠物医院的页面样式
/* CSS Document */ /* 对于CSS来说 每一个元素默认的margin和padding就是0px.但是不同的浏览器会有一个默认的浏览器样式修改默认的marg ...
- qt嵌入式html和本地c++通信方式
前沿:我们在做qt项目的时候,通常会把某个html网页直接显示到应用程序中.比如绘图.直接把html形式的图标嵌入到应用程序中 但是我们需要把数据从后台c++端传到html端,实现显示.qt实现了相关 ...
- Git小结---So far.......
基本的: 1. 在配置了SSH Key的情况下,clone项目时使用:git clone git@github.com/用户名/仓库名.git 使用这种方式而不使用https的方式的好处在于,在pu ...
- javaScript运动框架之匀速运动
运动框架 1.在开始运动时,关闭已有定时器 2.把运动和停止隔开(if/else) 匀速运动的停止条件 运动终止条件:距离足够近 Demo代码 <!DOCTYPE html> <ht ...
- 【Java】 Java常用的几个设计模式实例
一.单例模式 public class SingletonDemo { public static void main(String[] args) { // } } class User1{//饿汉 ...
- 2019-11-29-C#-很少人知道的科技
title author date CreateTime categories C# 很少人知道的科技 lindexi 2019-11-29 10:12:43 +0800 2018-03-16 08: ...
- SpringBoot打包成jar运行脚本
#!/bin/bash #这里可替换为你自己的执行程序,其他代码无需更改 APP_NAME=csadmin.jar #使用说明,用来提示输入参数 usage(){ echo "Usage: ...
- HBASE工作原理
如上图所示:首先我们需要知道 HBase 的集群是通过 Zookeeper 来进行机器之前的协调,也就是说 HBase Master 与 Region Server 之间的关系是依赖 Zookeepe ...
- 十二、S3C2440 裸机 — SDRAM
12.1 SDRAM 介绍 12.1.1 SDRAM 定义 SDRAM(Synchronous Dynamic Random Access Memory):同步动态随机存储器-内存条 同步是指内存工作 ...
- Codeforces Round #605 (Div. 3) 比赛总结
比赛情况 2h才刀了A,B,C,D.E题的套路做的少,不过ygt大佬给我讲完思路后赛后2min就AC了这题. 比赛总结 比赛时不用担心"时间短,要做多快",这样会匆匆忙忙,反而会做 ...