ZipHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.IO.Packaging;
  7. using Ionic.Zip;
  8. namespace Bowin.Common
  9. {
  10. public class ZIPHelper
  11. {
  12. public static IList<string> ExtractFile(string zipFilePath, string targetPath)
  13. {
  14. IList<string> result = new List<string>();
  15. using (Package zip = Package.Open(zipFilePath, FileMode.OpenOrCreate))
  16. {
  17. var parts = zip.GetParts();
  18. foreach (var part in parts)
  19. {
  20. var stream = part.GetStream();
  21. byte[] buffer = new byte[stream.Length + 100];
  22. long total = 0;
  23. for (int offset = 0; ; )
  24. {
  25. int bytesRead = stream.Read(buffer, offset, 100);
  26. if (bytesRead == 0) break;
  27. offset += bytesRead;
  28. total += bytesRead;
  29. }
  30. string filePath = targetPath + @"\" + Path.GetFileName(part.Uri.LocalPath);
  31. var fileStream = File.Create(filePath);
  32. for (int offset = 0; ; )
  33. {
  34. if (total - (offset + 1) > 100)
  35. {
  36. fileStream.Write(buffer, offset, 100);
  37. offset += 100;
  38. }
  39. else
  40. {
  41. fileStream.Write(buffer, offset, Convert.ToInt32(total - (offset + 1)));
  42. fileStream.Flush();
  43. break;
  44. }
  45. }
  46. result.Add(filePath);
  47. }
  48. }
  49. return result;
  50. }
  51. /// <summary>
  52. /// 解压缩.z文件
  53. /// </summary>
  54. /// <param name="rarFilePath"></param>
  55. /// <returns></returns>
  56. public static IList<string> ExtractZFile(string zFilePath, string outPutPath)
  57. {
  58. IList<string> result = new List<string>();
  59. System.Diagnostics.Process proc = new System.Diagnostics.Process();
  60. string pathOf7Zip = System.Configuration.ConfigurationManager.AppSettings["PathOf7Zip"];
  61. if (System.Web.HttpContext.Current != null)
  62. {
  63. pathOf7Zip = System.Web.HttpContext.Current.Server.MapPath("~/") + pathOf7Zip;
  64. }
  65. else
  66. {
  67. pathOf7Zip = System.AppDomain.CurrentDomain.BaseDirectory + pathOf7Zip;
  68. }
  69. proc.StartInfo.FileName = pathOf7Zip;
  70. proc.StartInfo.CreateNoWindow = true;
  71. proc.StartInfo.UseShellExecute = false;
  72. proc.StartInfo.RedirectStandardOutput = true;
  73. proc.StartInfo.RedirectStandardInput = true;
  74. proc.StartInfo.Arguments = "l " + zFilePath;
  75. bool currentIsFile = false;
  76. try
  77. {
  78. proc.Start();
  79. do
  80. {
  81. if (proc.StandardOutput.EndOfStream)
  82. {
  83. break;
  84. }
  85. string buffer = proc.StandardOutput.ReadLine();
  86. if (buffer.StartsWith("----"))
  87. {
  88. currentIsFile = !currentIsFile;
  89. }
  90. else
  91. {
  92. if (currentIsFile)
  93. {
  94. result.Add(outPutPath + @"\" + buffer.Split(' ').Last());
  95. }
  96. }
  97. }
  98. while (true);
  99. proc.WaitForExit();
  100. //if (!proc.WaitForExit(3600000))
  101. //{
  102. //}
  103. proc = new System.Diagnostics.Process();
  104. proc.StartInfo.FileName = pathOf7Zip;
  105. proc.StartInfo.CreateNoWindow = true;
  106. proc.StartInfo.UseShellExecute = false;
  107. proc.StartInfo.RedirectStandardOutput = false;
  108. proc.StartInfo.RedirectStandardInput = false;
  109. proc.EnableRaisingEvents = true;
  110. proc.StartInfo.Arguments = "e -y -o" + outPutPath + " " + zFilePath;
  111. proc.Start();
  112. proc.WaitForExit();
  113. proc.Close();
  114. }
  115. catch (Exception ex)
  116. {
  117. Exception MyEx = new Exception("解压缩出错!" + ex.Message);
  118. }
  119. proc.Dispose();
  120. return result;
  121. }
  122. public static string PackageDirectory(string path)
  123. {
  124. using (ZipFile zipNew = new ZipFile())
  125. {
  126. var pathName = Path.GetFileName(path.TrimEnd('\\'));
  127. var fileInfos = Directory.GetFiles(path);
  128. zipNew.UseUnicodeAsNecessary = true;
  129. //zipNew.
  130. // add this map file into the "images" directory in the zip archive
  131. //zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
  132. //// add the report into a different directory in the archive
  133. //zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
  134. foreach (var fileInfo in fileInfos)
  135. {
  136. zipNew.AlternateEncodingUsage = ZipOption.AsNecessary;
  137. zipNew.AlternateEncoding = Encoding.Unicode;
  138. zipNew.AddFile(fileInfo, @"\");
  139. }
  140. zipNew.Save(pathName + ".zip");
  141. return path.TrimEnd('\\') + "\\" + pathName + ".zip";
  142. }
  143. }
  144. }
  145. }