C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)
需要下载ICSharpCode.SharpZipLib.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip; namespace ZYOfficeWrap
{ /// <summary>
/// 压缩文件格式
/// </summary> public enum fileType
{
Zip = ,
RAR = ,
Tar =
}
/// <summary>
/// 文件解压缩操作类库
/// </summary>
public class FileZipOpr
{
/// <summary>
/// 构造函数
/// </summary>
public FileZipOpr()
{ } /// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要压缩的文件</param>
/// <param name="zipedFile">压缩后的文件</param>
/// <param name="compressionLevel">压缩等级</param>
/// <param name="blockSize">每次写入大小</param>
public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
{
//如果文件没有找到,则报错
if (!File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (FileStream ZipFile = File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (FileStream StreamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + );
ZipEntry ZipEntry = new ZipEntry(fileName);
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>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要进行压缩的文件名</param>
/// <param name="zipedFile">压缩后生成的压缩文件名</param>
public void ZipFile(string fileToZip, string zipedFile)
{
//如果文件没有找到,则报错
if (!File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (FileStream fs = File.OpenRead(fileToZip))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
fs.Close(); using (FileStream ZipFile = File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + );
ZipEntry ZipEntry = new ZipEntry(fileName);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(); ZipStream.Write(buffer, , buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
}
}
} /// <summary>
/// 压缩多层目录
/// </summary>
/// <param name="strDirectory">要进行压缩的文件夹</param>
/// <param name="zipedFile">压缩后生成的压缩文件名</param>
public void ZipFileDirectory(string strDirectory, string zipedFile)
{
using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
ZipSetp(strDirectory, s, "");
}
}
} /// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strDirectory">文件夹名称</param>
/// <param name="s">The ZipOutputStream Object.</param>
/// <param name="parentPath"></param>
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);
}
}
}
} /// <summary>
/// 解压缩一个 zip 文件。
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="password">zip 文件的密码。</param>
/// <param name="overWrite">是否覆盖已存在的文件。</param>
public void UnZipFile(string zipedFile, string strDirectory, string password, bool overWrite)
{ if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
s.Password = password;
ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name; if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length); if (size > )
streamWriter.Write(data, , size);
else
break;
}
streamWriter.Close();
}
}
}
} s.Close();
}
} }
}
C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)的更多相关文章
- C#利用SharpZipLib解压或压缩文件夹实例操作
最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode. ...
- java解压多层目录中多个压缩文件和处理压缩文件中有内层目录的情况
代码: package com.xiaobai; import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...
- python用模块zlib压缩与解压字符串和文件的方法
摘自:http://www.jb51.net/article/100218.htm Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等. python中 ...
- .Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件
using System; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; us ...
- java利用zip解压slpk文件
public static void main(String[] args) { File file = new File("C:\\Users\\Administrator\\Deskto ...
- [Linux] 解压tar.gz文件,解压部分文件
遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了 数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...
- Linux中下载、解压、安装文件
一.将解压包发送到linux服务器上: 1.在windos上下载好压缩包文件后,通过winscp等SFTP客户端传送给linux 2.在linux中通过wget命令直接下载 #wget [选项] [下 ...
- C# 上传RAR文件 解压 获取解压后的文件名称
此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...
- 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法
解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...
随机推荐
- 查看 Laravel 的 SQL 语句的方法
在使用 Laravel 的 Eloquent 进行数据查询的时候,很多小伙伴都想看到背后执行的 SQL 语句到底是什么样的,这小笔录就是解决这个小问题的: 在 Providers/AppService ...
- AC日记——Roma and Poker codeforces 803e
803E - Roma and Poker 思路: 赢或输或者平的序列: 赢和平的差的绝对值不得超过k: 结束时差的绝对值必须为k: 当“?”时可以自己决定为什么状态: 输出最终序列或者NO: dp( ...
- (33)C#正则表达式
正则表达式:专门用于字符串处理的语言,用来描述字符串特征的表达式 元字符 . 之间可以出现任意单个字符(除了\n 换行) 例如: a.b 意思是这个表达式必须是三个字符,第一个字符是a,第三个字符 ...
- Python的程序结构[0] -> 属性/Property[0] -> 类属性、实例属性和私有属性
类属性.实例属性和私有属性 Python中类的属性主要包括类属性,实例属性和私有属性,下面是对三种属性的简单介绍 类属性 / Class Property 类属性在__init__()之外初始化,在外 ...
- Buffer源码深入分析
博客园对MarkDown显示的层次感不是很好,大家可以看这里:Buffeer. 本机环境: Linux 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33 ...
- 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...
- 【强连通分量】tarjan算法及kosaraju算法+例题
阅读前请确保自己知道强连通分量是什么,本文不做赘述. Tarjan算法 一.算法简介 Tarjan算法是一种由Robert Tarjan提出的求有向图强连通分量的时间复杂度为O(n)的算法. 首先我们 ...
- bean装配--注解
1,Dao层 package com.songyan.zhujie; public interface UserDao { public void say(); } package com.songy ...
- c#异步线程:同步调用,异步调用,异步回调
定义一个异步线程类: public class AsyEventClass { private static ILog logger = LogManager.GetLogger(MethodBase ...
- centos7 安装LNMP(php7)之 nginx php-fpm yum安装以及配置文件修改
PHP7.1.6整体参考 https://www.zhihu.com/question/50615606/answer/145699091 http://www.bubuko.com/infodeta ...