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 ...
随机推荐
- OAuth四种模式
授权码模式(authorization code)----适用于网站服务端去oauth服务端申请授权 简化模式(implicit)----没有服务端,js+html页面去oauth服务端申请授权 密码 ...
- NOPI读取Word模板并保存
安装NPOI 可以在 程序包管理器控制台中输入 PM> Install-Package NPOI 会下载最新版本NPOI ----------------------------引用了NPOI- ...
- Ceph 文件系统的安装
yum install -y wget wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87 ...
- MVC 返回404,返回图片,流到数组,apk信息
return HttpNotFound(); byte[] buffer0 = QRCode(); return File(buffer0, @"image/jpeg"); // ...
- js手机移动端选择插件 mobileSelect.js
一.mobileSelect获取方法 mobileSelect支持单选.多级联动.自定义回调函数.二次渲染.最新版本下载地址[2017-09-21更新]: https://github.com/onl ...
- javaee IO流作业
package Zy; import java.io.Serializable; public class Student implements Serializable{ private stati ...
- Git创建本地分支并关联远程分支(二)
创建本地分支git branch 分支名 例如:git branch dev,这条命令是基于当前分支创建的本地分支,假设当前分支是master(远程分支),则是基于master分支创建的本地分支dev ...
- QT5.4.1在ARM开发板上不能显示汉字
在linux下正常的程序,移植到ARM上,中文不能显示.网上好多介绍,一头雾水.查看其中话题是关于中文显示的(http://www.qtcn.org/bbs/simple/?t55852.html). ...
- 从命令行配置 Windows 防火墙
从命令行配置 Windows 防火墙 高级用户可以使用命令行来配置 Windows 防火墙.您可以使用 netsh 命令行工具来进行配置. 下表中的 netsh 命令可用于 Microsoft Win ...
- chrome js 获取css
var myDiv = document.getElementById("chooseRect"); var computedStyle = document.defaultVie ...