下载地址:http://dotnetzip.codeplex.com/
解压后找到\DotNetZipLib-DevKit-v1.9zip-v1.9Release下的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 01.using System; 02.using System.Collections.Generic; 03.using System.ComponentModel; 04.using System.Data; 05.using System.Drawing; 06.using System.Linq; 07.using System.Text; 08.using System.Windows.Forms; 09.using Ionic.Zip; 10.namespace WindowsFormsZIP 11.{ 12. public partial class Form1 : Form 13. { 14. public Form1() 15. { 16. InitializeComponent(); 17. } 18. 19. /// <summary> 20. /// 压缩带中文的文件名. 21. /// </summary> 22. /// <param name="sender">The source of the event.</param> 23. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 24. private void button1_Click(object sender, EventArgs e) 25. { 26. 27. using (ZipFile zip = new ZipFile("苏志.zip",Encoding.Default)) 28. { 29. zip.AddFile("数据库文档.doc"); 30. zip.Save(); 31. } 32. 33. } 34. 35. /// <summary> 36. /// 用密码加密ZIP文件. 37. /// </summary> 38. /// <param name="sender">The source of the event.</param> 39. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 40. private void button2_Click(object sender, EventArgs e) 41. { 42. 43. using (ZipFile zip = new ZipFile()) 44. { 45. zip.Password = "123456!"; 46. zip.AddFile("WPF 4 Unleashed.pdf"); 47. zip.Save("WPF 4 Unleashed.zip"); 48. } 49. 50. } 51. 52. /// <summary> 53. /// 压缩文件夹 54. /// </summary> 55. /// <param name="sender">The source of the event.</param> 56. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 57. private void button3_Click(object sender, EventArgs e) 58. { 59. using (ZipFile zip = new ZipFile()) 60. { 61. zip.AddDirectory(@"E:suzhi"); 62. zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G"); 63. zip.Save("test.zip"); 64. } 65. } 66. 67. /// <summary> 68. /// 抽取ZIP中的文件到指定的文件夹. 69. /// </summary> 70. /// <param name="sender">The source of the event.</param> 71. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 72. private void button4_Click(object sender, EventArgs e) 73. { 74. using (ZipFile zip = ZipFile.Read("test.zip")) 75. { 76. foreach (ZipEntry z in zip) 77. { 78. z.Extract(@"F:kk"); 79. } 80. } 81. 82. } 83. 84. } 85.}
具体的例子:
//获取压缩包路径 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 - 3, 3).Equals("zip")) { decomPath = material.FilePath.Replace(".zip", ""); } if (material.FilePath.Substring(material.FilePath.Length - 3, 3).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() == 1) { nowpath = decomPath + "\" + dirchild[0].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"); }