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 ...
随机推荐
- 【sqli-labs】 less24 POST- Second Order Injections *Real treat* -Stored Injections (POST型二阶注入 *真的好玩?* 存储注入)
简单登陆浏览一遍后,发现是一个登陆注册修改密码的应用 审查一下代码 登陆页面的username,password使用了转义 注册页面的参数也进行了转义处理 但是在修改password的页面,直接从se ...
- OpenWRT 常用软件安装
root@Jack:/tmp/opkg-lists# opkg--help opkg:unrecognized option `--help' opkgmust have one sub-comman ...
- linux下怎么退出telnet
在运维过程中,常常会telnet某个ip端口,如果 能telnet通,怎么退出呢 ? 1.telnet 63.172.25.18 6463 回车 Trying 63.172.25.18... Conn ...
- Java JPA通过hql语句查询数据
import javax.persistence.PersistenceContext; import javax.persistence.Query; public class StudentSer ...
- python学习之小小爬虫
学习python一段时间了,写了一个图片的小小爬虫,分享下,不喜勿喷! #coding=utf-8 ''' Created on 2015-5-22 @author: 悦文 ''' import re ...
- [CodeForces]1059C Sequence Transformation
构造题. 我递归构造的,发现如果N>3的话就优先删奇数,然后就把删完的提取一个公约数2,再重复操作即可. 具体原因我觉得是因为对于一个长度大于3的序列,2的倍数总是最多,要令字典序最大,所以就把 ...
- Hexo系列(一) 搭建博客网站
写在前面的话:本系列文章主要参考 Hexo官方说明文档,同时结合自己在使用过程中的一些心得体会,撷取下来,和大家分享分享.好,下面闲话不多说,马上开始我们的 Hexo 之旅吧 温馨提醒:博主使用的操作 ...
- 继续聊WPF——Expander控件(1)
这个控件最实用的地方,就是做导航栏. <StackPanel Margin="20,20" Width="100" Height="460&qu ...
- js获取数组中任意一项
1.获取数组任一项 在一些实际应用场景中,会要求实现一个随机的需求,随机获取某一项,来展示出来,都知道要通过javaScript的Math.random()方法来实现,这里我在Array的原型上添加了 ...
- MySQL 存储过程的异常处理
mysql> mysql> delimiter $$ mysql> mysql> CREATE PROCEDURE myProc -> (p_first_ ...