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. Challenge–response authentication 挑战(询问)应答机制

    In computer security, challenge–response authentication is a family of protocols in which one party ...

  2. Apex语言(八)类和对象

    1.类和对象 一切皆对象,是客观存在的万物,有标识.属性和行为.一个人,一台电脑,一辆轿车都是对象 类是创建对象的模板或蓝图,是对象的抽象,是对象的类型. 一个对象是一个类的一个实例,是一个类的变量. ...

  3. esp32(M5STACK)程序烧写(Ubuntu)

    由于我们的开发环境在Ubuntu上,所以介绍一下如何在Ubuntu上烧写esp32的程序 首先下载esptools   pip install esptool           擦除 sudo es ...

  4. Doxyfile中插入图片

    下面讲一下如何在doxyfile中插入图片 在查看别人写的文档的过程中,看到可以在doxyfile中插入图片,对此十分的好奇,所以拿出来研究一下 那么这是如何实现的? 根据代码,可以看到如下的注释 @ ...

  5. HDU1029 - Ignatius and the Princess IV【水题】

    给你n个数字,请你找出出现至少(n+1)/2次的数字. 输入 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行 ...

  6. Linux挂载NAS共享文件夹

    [root@ftp:/mnt] > mount -o username=user01,password=1234567890 //192.168.31.20/share /mnt/nas [ro ...

  7. Git 基础教程 之 删除文件

    ① 手动或命令 rm删除工作区的问价:       git checkout -- readme.txt 可恢复       checkout 实际上是用版本库里的替换工作区的版本 ② 删除了工作区文 ...

  8. js对对象的校验技巧,随时更新

    js中,字符串长度用length. 若不确定一个Map里,是否存在某个对象,则用underfind 去校验

  9. 0804SHOW ENGINE INNODB STATUS

    转自http://blog.csdn.net/github_26672553/article/details/52931263 innodb存储引擎在show engine innodb status ...

  10. 优化系列 | DELETE子查询改写优化

    0.导读 有个采用子查询的DELETE执行得非常慢,改写成SELECT后执行却很快,最后把这个子查询DELETE改写成JOIN优化过程 1.问题描述 朋友遇到一个怪事,一个用子查询的DELETE,执行 ...