下载地址: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");
}

最新文章

  1. 跟vczh看实例学编译原理——二:实现Tinymoe的词法分析
  2. mysql-7 数据检索(5)
  3. Java数据结构——双端链表
  4. Android WebView 拦截自定义协议
  5. Python入门笔记(21):Python函数(4):关于函数式编程的内建函数
  6. Android开源项目汇总【转】
  7. 简单LRU算法实现缓存
  8. Oracle下的IF EXISTS()
  9. DevExpress - cxGrid 使用方法
  10. 终于会用c#中的delegate(委托)和event(事件)了
  11. iOS开发实战-上架AppStore 通过内购和广告获得收益
  12. 花点时间顺顺Git(下)
  13. Android自动化测试之MonkeyRunner使用
  14. Jackson将对象转换为json字符串时,设置默认的时间格式
  15. 第六节,Neural Networks and Deep Learning 一书小节(下)
  16. Oracle_安装说明
  17. Could not open ServletContext resource [/WEB-INF/xxx-servlet.xml]
  18. [转]C# FTP操作类
  19. Pandas基本功能之层次化索引及层次化汇总
  20. 推荐十款java开源中文分词组件

热门文章

  1. BZOJ 3127 [Usaco2013 Open]Yin and Yang(树点分治)
  2. BZOJ 1042:[HAOI2008]硬币购物(容斥原理+DP)
  3. Android:布局实例之模仿QQ登录界面
  4. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp
  5. inno setup 脚本常用修改 转
  6. .net中的泛型
  7. Ubuntu下Wine使用教程
  8. android使用C/C++调用SO库
  9. 784 - Maze Exploration
  10. 该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。