下载地址: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压缩与解压缩的更多相关文章

  1. Java 基础【12】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  2. 菜鸟学Linux命令:tar命令 压缩与解压缩

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件. tar最初被用来在磁带上创建档案,现在,用户可以 ...

  3. Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控

    一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...

  4. gzip [选项] 压缩(解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...

  5. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  6. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  7. zip格式压缩、解压缩(C#)

    压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...

  8. Tar打包、压缩与解压缩到指定目录的方法

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...

  9. Deflater与Inflater的压缩与解压缩

    原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...

随机推荐

  1. [漏洞检测]Proxpy Web Scan设计与实现(未完待续)

    Proxpy Web Scan设计与实现 1.简介:          Proxpy Web Scan是基于开源的python漏洞扫描框架wapiti改造的web漏洞扫描器,其主要解决以下几个问题而生 ...

  2. js中getBoundingClientRect()的用法介绍

    平时经常获取一个div的位置,用了定位,取位置还好,在不用定位的情况下,计算一个div在页面的距离,就可以用到getBoundingClientRect()方法. getBoundingClientR ...

  3. (救星啊)im-switch -s ibus错误:Error: no configuration file "ibus" exists.

    转自:http://www.cnblogs.com/csulennon/p/4194902.html 在虚拟机上安装Ubuntu14.04 后安装ibus输入法,万万没想到在切换输入法的时候居然出错了 ...

  4. java访问ad域

    1.活动目录(AD) Active Directory 是用于 Windows Server 的目录服务.它存储着网络上各种对象的有关信息,并使该信息易于管理员和用户查找及使用.Active Dire ...

  5. Android-25种开源炫酷动画框架

    前言 忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮助,此博文介绍的都是UI上面的框架,接下来会有 ...

  6. JS isNaN()函数

    1.作用 isNaN() 函数用于检查其参数是否是非数字值. 2.JS <script> document.write(isNaN(123)); document.write(isNaN( ...

  7. iOS:2015年07月最新苹果IOS上架App Store商店步骤

    苹果官方在2015年05-06月开发者中心进行了改版,网上的APP Store上架大部分都不一样了,自己研究总结一下,一个最新的上架教程以备后用. 原文地址:http://www.16css.com/ ...

  8. iOS:分段控件UISegmentedControl的详细使用

    分段控件:UISegmentedControl   功能:分段的控制.页面的切换等.   介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮 ...

  9. c#中的数组、ArrayList、List区别

    首先说明C#中的Array类:Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义.Array 类提供了各种用于数组的属性和方法.关于Array类的一些属性及方法详见博文:C ...

  10. 在Oracle 11.2.0.1.0下dbms_stats.gather_table_stats收集直方图不准

    SQL> select * from v$version; BANNER ------------------------------------------------------------ ...