使用DotNetZip压缩与解压缩
下载地址:http://dotnetzip.codeplex.com/
解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文件拷贝到C盘下
接着打开Visual Studio Command Prompt (2010),然后定位到C盘根目录,依次输入gacutil -i Ionic.Zip.dll 和regasm Ionic.Zip.dll ,然后拷贝C盘下的这个Ionic.Zip.dll到您的应用程序中,添加引用即可!代码例子如下:
[csharp] view plaincopy .using System;
.using System.Collections.Generic;
.using System.ComponentModel;
.using System.Data;
.using System.Drawing;
.using System.Linq;
.using System.Text;
.using System.Windows.Forms;
.using Ionic.Zip;
.namespace WindowsFormsZIP
.{
. public partial class Form1 : Form
. {
. public Form1()
. {
. InitializeComponent();
. }
.
. /// <summary>
. /// 压缩带中文的文件名.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button1_Click(object sender, EventArgs e)
. {
.
. using (ZipFile zip = new ZipFile("苏志.zip",Encoding.Default))
. {
. zip.AddFile("数据库文档.doc");
. zip.Save();
. }
.
. }
.
. /// <summary>
. /// 用密码加密ZIP文件.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button2_Click(object sender, EventArgs e)
. {
.
. using (ZipFile zip = new ZipFile())
. {
. zip.Password = "123456!";
. zip.AddFile("WPF 4 Unleashed.pdf");
. zip.Save("WPF 4 Unleashed.zip");
. }
.
. }
.
. /// <summary>
. /// 压缩文件夹
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button3_Click(object sender, EventArgs e)
. {
. using (ZipFile zip = new ZipFile())
. {
. zip.AddDirectory(@"E:\suzhi");
. zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
. zip.Save("test.zip");
. }
. }
.
. /// <summary>
. /// 抽取ZIP中的文件到指定的文件夹.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button4_Click(object sender, EventArgs e)
. {
. using (ZipFile zip = ZipFile.Read("test.zip"))
. {
. foreach (ZipEntry z in zip)
. {
. z.Extract(@"F:\kk");
. }
. }
.
. }
.
. }
.}
具体的例子:
//获取压缩包路径
string filename = material.CreationTime.ToyyyyMMdd().Replace("-", ""); //string zippath = AppDomain.CurrentDomain.BaseDirectory + "upload\\Zipped\\" + filename;
//和上面的结果一样
string zippath = material.FilePath;
string decomPath = "";
string nowpath = "";
if (material.FilePath.Substring(material.FilePath.Length - , ).Equals("zip"))
{ decomPath = material.FilePath.Replace(".zip", ""); }
if (material.FilePath.Substring(material.FilePath.Length - , ).Equals("rar"))
{ decomPath = material.FilePath.Replace(".rar", ""); }
//查询此目录下是否存在需要解压过的同名的文件夹名称
DirectoryInfo direInfo = new DirectoryInfo(decomPath);
if (direInfo.Exists)
{
DirectoryInfo[] childs = direInfo.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
}
direInfo.Delete(true);
} //解压文件夹
using (ZipFile zip = ZipFile.Read(zippath))
{ foreach (ZipEntry z in zip)
{
//z.Extract(material.FilePath.Replace(".zip", ""));
z.Extract(zippath.Replace(".zip", ""));
}
} direInfo = new DirectoryInfo(zippath.Replace(".zip", ""));
if (direInfo.Exists)
{
DirectoryInfo[] dirchild = direInfo.GetDirectories();
if (dirchild.Count() == )
{
nowpath = decomPath + "\\" + dirchild[].Name;
direInfo = new DirectoryInfo(nowpath);
DirectoryInfo[] dirchild2 = direInfo.GetDirectories();
foreach (DirectoryInfo childrens in dirchild2)
{ //修改文件夹的内容
FileStream fs = new FileStream(nowpath +"\\"+ childrens.Name + "\\index.html", FileMode.OpenOrCreate, FileAccess.ReadWrite);
string fileText = "";
GameServer server = GameServerService.GetGameServerWithGame(materialLog.ServerId);
Game game = server.Game;
using (StreamReader sr = new StreamReader(fs))
{
fileText = sr.ReadToEnd();
fileText = fileText.Replace("#GameCode#", game.Code);
fileText = fileText.Replace("#ServerCode#", server.Code);
fileText = fileText.Replace("#ServerId#", Convert.ToString(server.ServerId));
}
if (fileText != "")
{
using (StreamWriter sw = new StreamWriter(nowpath + "\\" + childrens.Name + "\\index.html", false, Encoding.Default))
{
sw.Write(fileText);
}
} }
} } //重新压缩 using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(decomPath);
zip.Save(nowpath+".zip");
}
使用DotNetZip压缩与解压缩的更多相关文章
- Java 基础【12】 压缩与解压缩
Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...
- 菜鸟学Linux命令:tar命令 压缩与解压缩
tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件. tar最初被用来在磁带上创建档案,现在,用户可以 ...
- Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控
一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...
- gzip [选项] 压缩(解压缩)
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
- PclZip:强大的PHP压缩与解压缩zip类
PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...
- zip格式压缩、解压缩(C#)
压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...
- Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- Deflater与Inflater的压缩与解压缩
原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...
随机推荐
- BZOJ 1711:[Usaco2007 Open]Dining吃饭(最大流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1711 [题目大意] 每头牛都有一些喜欢的饮料和食物, 现在有一些食物和饮料,但是每样只 ...
- 【容斥原理】【分解质因数】poj1091 跳蚤
题意转化为求一个线性组合a1*x1+a2*x2+...+an*xn+m*xn+1=1在什么时候可以有解.(ai在1~m的范围内任取) 易得当且仅当gcd(a1,a2,...,an)=1时可能有解. 然 ...
- 求图的强连通分量--tarjan算法
一:tarjan算法详解 ◦思想: ◦ ◦做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间 ...
- web安全开发指南--权限管理
2.1 访问控制安全规则 1 访问控制必须只能在服务器端执行. 2 只通过session来判定用户的真实身份,避免使用其它数据域的参数(比如来自cookie.hidden域 ...
- Android内存优化2 了解java内存分配 2
JVM内存模型 Java虚拟机(Java Virtual Machine=JVM)的内存空间分为五个部分,分别是: 1. 程序计数器 2. Java虚拟机栈 3. 本地方法栈 4. 堆 5. 方法区. ...
- .net 4.5如何使用Async和Await进行异步编程
通过使用异步编程,可避免出现性能瓶颈,并提高应用程序的整体响应.然而,技术编写异步应用程序的传统方法过于复杂,这使得异步程序难以编写,调试和维护. Visual Studio2012引入了一个简单的开 ...
- 【转载】Java 的开发效率究竟比 C++ 高在哪里?
哈哈哈,太好笑了 https://www.zhihu.com/people/ze.ran ze ran编程话题优秀回答者 less is more 人赞同 C++是面向内存编程,Java是面向数据结构 ...
- javascript上传多张图片并预览
直接上代码 html代码 <div> <label>封面</label> <input type="file" id="cove ...
- mac office 设置默认视图显示比例
1.打开word 2.fn+option+F11,选中Normal,右键插入模块,复制以下脚本到编辑器中 Sub AutoOpen() ActiveWindow.ActivePane.View.Zoo ...
- BS中保存参数
开发中经常需要将值存起来,当点击某一项时以便知道点击了哪一项. 一:应用JS页面跳转(牛腩中讲到) HTML: <td class="txt c"><a href ...