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进行文件的压缩与解压的更多相关文章

  1. C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩

    ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...

  2. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  3. Asp.net中文件的压缩与解压

    这里笔者为大家介绍在asp.net中使用文件的压缩与解压.在asp.net中使用压缩给大家带来的好处是显而易见的,首先是减小了服务器端文件存储的空间,其次下载时候下载的是压缩文件想必也会有效果吧,特别 ...

  4. HDFS中文件的压缩与解压

    HDFS中文件的压缩与解压 文件的压缩有两大好处:1.可以减少存储文件所需要的磁盘空间:2.可以加速数据在网络和磁盘上的传输.尤其是在处理大数据时,这两大好处是相当重要的. 下面是一个使用gzip工具 ...

  5. C# -- 文件的压缩与解压(GZipStream)

    文件的压缩与解压 需引入 System.IO.Compression; 1.C#代码(入门案例) Console.WriteLine("压缩文件..............."); ...

  6. C#调用7z实现文件的压缩与解压

    1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...

  7. linux下tar gz bz2 tgz z等众多压缩文件的压缩与解压方法

    Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...

  8. 通过SharpZipLib实现文件夹压缩以及解压

    代码说明 基于SharpZipLib实现Zip压缩解压,扩展实现文件夹级别压缩解压: 项目源码:MasterChief.DotNet.Infrastructure.Zip Install-Packag ...

  9. 文件的压缩与解压XZip,XUnzip

    参考http://www.codeproject.com/KB/cpp/xzipunzip.aspx CreateZip() –创建一个空的 zip 文件 HZIP CreateZip(void *z ...

随机推荐

  1. OAuth密码模式说明(resource owner password credentials)

    用户向客户端(third party application)提供用户名和密码. 客户端将用户名和密码发给认证服务器(Authorization server),向后者请求令牌(token). 认证服 ...

  2. 静态修改url,不跳转

    history.replaceState(null,document.title,'www.baidu.com');

  3. 如何将一个已有的项目托管到github或是码云上?git的配置

    场景一:已有的一个项目,要把它托管到Git上去,步骤和方法如下: 方法一: ①在工程的路径下 : git init 建一个裸仓库. ②远程仓库地址 :将本地的仓库和远程仓库关联 git remote ...

  4. S-HR快速查看shr日志

    http://localhost:6888/shr/appData.do?method=getApplicationLog&logFile=apusic.log.0&instance= ...

  5. jquery判断字符串中是否存在某个的字符串

    有两种方式: 1)test 2)indexOf $(function(){ var str="sunny,woo"; var sear=new RegExp(','); if(se ...

  6. 07.网络编程-3.TCP

    1.tcp相关介绍 TCP协议,传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC ...

  7. 【ACM】hdu_1094_A+BVI_201307261731

    A+B for Input-Output Practice (VI)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  8. mysql 服务器监控系列-黄杉 mysqldba

    http://blog.csdn.net/mchdba/article/category/2220809

  9. Ajax json 数据格式

    ajax : 是么是同步 什么事异步 同步现象:客户端发送请求到服务端,当服务端返回响应之前,客户端都处于等待卡死状态. 异步现象:客户端发送请求到服务器端,无论服务器是否返回,客户端都可以随意做其他 ...

  10. POJ 2079

    呃,不知道我用的算不算卡壳,总有点枚举的意思. 先求凸包,然后,枚举其中一点,再枚举另一点作为结尾,这个向量旋转一周后,求出最大值面积.这里面用的是旋转卡壳判断的那个式子. PS:下一篇和这题是一样题 ...