【C#】使用DotNetZip壓縮與解壓縮
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
DotnetZip是一個開源類庫,支持.NET的任何語言,可很方便的創(chuàng)建,讀取,和更新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("數(shù)據(jù)庫文檔.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; //設(shè)置壓縮率 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; //和上面的結(jié)果一樣 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) { //修改文件夾的內(nèi)容 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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |