【C#】使用DotNetZip压缩与解压缩
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。
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.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; //设置压缩率 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"); } 该文章在 2024/1/19 22:48:33 编辑过 |
关键字查询
相关文章
正在查询... |