C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Security.Cryptography; namespace zip压缩与解压
{
public class ZipHelper
{
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">需压缩的文件名</param>
/// <param name="zipFile">压缩后的文件名(文件名都是绝对路径)</param>
/// <param name="level">压缩等级(0-9)</param>
/// <param name="password">压缩密码(解压是需要的密码)</param>
public static void ZipFile(string fileToZip, string zipFile, int level = , string password = "")
{
if (!File.Exists(fileToZip))
throw new FileNotFoundException("压缩文件" + fileToZip + "不存在"); using (FileStream fs = File.OpenRead(fileToZip))
{
fs.Position = ;//设置流的起始位置
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, , buffer.Length);//读取的时候设置Position,写入的时候不需要设置
fs.Close();
using (FileStream zfstram = File.Create(zipFile))
{
using (ZipOutputStream zipstream = new ZipOutputStream(zfstram))
{
zipstream.Password = md5(password);//设置属性的时候在PutNextEntry函数之前
zipstream.SetLevel(level);
string fileName = fileToZip.Substring(fileToZip.LastIndexOf('\\') + );
ZipEntry entry = new ZipEntry(fileName);
zipstream.PutNextEntry(entry);
zipstream.Write(buffer, , buffer.Length);
}
} }
} /// <summary>
/// 压缩多个文件目录
/// </summary>
/// <param name="dirname">需要压缩的目录</param>
/// <param name="zipFile">压缩后的文件名</param>
/// <param name="level">压缩等级</param>
/// <param name="password">密码</param>
public static void ZipDir(string dirname, string zipFile, int level = , string password = "")
{
ZipOutputStream zos = new ZipOutputStream(File.Create(zipFile));
zos.Password = md5(password);
zos.SetLevel(level);
addZipEntry(dirname, zos, dirname);
zos.Finish();
zos.Close(); }
/// <summary>
/// 往压缩文件里面添加Entry
/// </summary>
/// <param name="PathStr">文件路径</param>
/// <param name="zos">ZipOutputStream</param>
/// <param name="BaseDirName">基础目录</param>
private static void addZipEntry(string PathStr, ZipOutputStream zos, string BaseDirName)
{
DirectoryInfo dir = new DirectoryInfo(PathStr);
foreach (FileSystemInfo item in dir.GetFileSystemInfos())
{
if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)//如果是文件夹继续递归
{
addZipEntry(item.FullName, zos, BaseDirName);
}
else
{
FileInfo f_item = (FileInfo)item;
using (FileStream fs = f_item.OpenRead())
{
byte[] buffer = new byte[(int)fs.Length];
fs.Position = ;
fs.Read(buffer, , buffer.Length);
fs.Close();
ZipEntry z_entry = new ZipEntry(item.FullName.Replace(BaseDirName, ""));
zos.PutNextEntry(z_entry);
zos.Write(buffer, , buffer.Length);
}
}
} } /// <summary>
/// 解压多个文件目录
/// </summary>
/// <param name="zfile">压缩文件绝对路径</param>
/// <param name="dirname">解压文件目录</param>
/// <param name="password">密码</param>
public static void UnZip(string zfile, string dirname, string password)
{
if (!Directory.Exists(dirname)) Directory.CreateDirectory(dirname); using (ZipInputStream zis = new ZipInputStream(File.OpenRead(zfile)))
{
zis.Password = md5(password);
ZipEntry entry;
while ((entry = zis.GetNextEntry()) != null)
{
var strArr = entry.Name.Split('\\');//这边判断压缩文件里面是否存在目录,存在的话先创建目录后继续解压
if (strArr.Length > )
Directory.CreateDirectory(dirname + @"\" + strArr[]); using (FileStream dir_fs = File.Create(dirname + entry.Name))
{
int size = * ;
byte[] buffer = new byte[size];
while (true)
{
size = zis.Read(buffer, , buffer.Length);
if (size > )
dir_fs.Write(buffer, , size);
else
break;
}
}
}
}
} private static string md5(string pwd)
{
var res = "";
MD5 md = MD5.Create();
byte[] s = md.ComputeHash(Encoding.Default.GetBytes(pwd));
for (int i = ; i < s.Length; i++)
res = res + s[i].ToString("X"); return res;
}
}
}
调用函数如下:
static void Main(string[] args)
{ var str = @"\学籍导入模板.xls";
//var arr=str.Split('\\'); var filePath = @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压\File\学籍导入模板.xls";
//ZipHelper.ZipFile(filePath, @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压\File\test.zip", 6, "123");
var dirPath = @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压";
//ZipHelper.ZipDir(dirPath + @"\File", dirPath + @"\File.zip", 6, "huage"); ZipHelper.UnZip(dirPath + @"\File.zip", dirPath + @"\test", "huage"); Console.ReadKey();
}
效果图如下:

C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压的更多相关文章
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- Asp.net中文件的压缩与解压
这里笔者为大家介绍在asp.net中使用文件的压缩与解压.在asp.net中使用压缩给大家带来的好处是显而易见的,首先是减小了服务器端文件存储的空间,其次下载时候下载的是压缩文件想必也会有效果吧,特别 ...
- HDFS中文件的压缩与解压
HDFS中文件的压缩与解压 文件的压缩有两大好处:1.可以减少存储文件所需要的磁盘空间:2.可以加速数据在网络和磁盘上的传输.尤其是在处理大数据时,这两大好处是相当重要的. 下面是一个使用gzip工具 ...
- C# -- 文件的压缩与解压(GZipStream)
文件的压缩与解压 需引入 System.IO.Compression; 1.C#代码(入门案例) Console.WriteLine("压缩文件..............."); ...
- C#调用7z实现文件的压缩与解压
1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...
- linux下tar gz bz2 tgz z等众多压缩文件的压缩与解压方法
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- 通过SharpZipLib实现文件夹压缩以及解压
代码说明 基于SharpZipLib实现Zip压缩解压,扩展实现文件夹级别压缩解压: 项目源码:MasterChief.DotNet.Infrastructure.Zip Install-Packag ...
- 文件的压缩与解压XZip,XUnzip
参考http://www.codeproject.com/KB/cpp/xzipunzip.aspx CreateZip() –创建一个空的 zip 文件 HZIP CreateZip(void *z ...
随机推荐
- AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)
一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...
- BZOJ 5466: [Noip2018]保卫王国 动态DP
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define ll long long #define lson (now ...
- HTTP 状态码 301 和 302 详解及区别——辛酸的探索之路
转自:http://blog.csdn.net/grandpang/article/details/47448395 一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种资料了 ...
- Django解决跨域俩方案
方案一:一套干掉全部Primary 首先你的pip下载一个第三方库贼厉害的: pip install corsheaders 然后在项目的setting.py里面引入第三方库,如下: INSTALLE ...
- Windows下Jupyter notebook 修改默认打开(工作、保存)文件夹(路径)
今天晚上兴致一起突然想看看我写了那么多的ipynb文件都去哪了 首先查了一下,应该是都默认保存到 C:\Users\芩溪儿 路径下了 然后我就想,我是不是得改改啊,总在那跟别的文件夹在一起总 ...
- [pytorch学习]2. 官网60分钟教程摘要
https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 1. Pytorch的基本单元,tensor,本质上和num ...
- java中Date与String转化 string转float
这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: Date date=new Date("2017-02-01"); 方法2: ...
- UVALive 6177 The King's Ups and Downs
The King's Ups and Downs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UV ...
- JQuery dom 操作总结
DOM 操作之获取值 获得内容 - text():设置或返回所选元素的文本内容 $("#btn1").click(function(){ alert("Text: &qu ...
- 2015多校联合训练赛 hdu 5308 I Wanna Become A 24-Point Master 2015 Multi-University Training Contest 2 构造题
I Wanna Become A 24-Point Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 ...