ZipHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using SharpCompress.Archives;
  2. using SharpCompress.Archives.GZip;
  3. using SharpCompress.Archives.Zip;
  4. using SharpCompress.Common.GZip;
  5. using SharpCompress.Common;
  6. using SharpCompress.Common.SevenZip;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Diagnostics;
  14. namespace PackingPress.Common
  15. {
  16. public class ZipHelper
  17. {
  18. private ZipHelper(){ }
  19. public static void Zips(string inDirectory, string zipFile)
  20. {
  21. ArchiveEncoding archiveEncoding = new ArchiveEncoding();
  22. archiveEncoding.Default = Encoding.GetEncoding("utf-8");
  23. SharpCompress.Writers.WriterOptions options = new SharpCompress.Writers.WriterOptions(CompressionType.Deflate);
  24. options.ArchiveEncoding = archiveEncoding;
  25. options.LeaveStreamOpen = false;
  26. using (var archive = ZipArchive.Create())
  27. {
  28. archive.AddAllFromDirectory(inDirectory + "\\");
  29. archive.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestSpeed;
  30. using (FileStream fs_scratchPath = new FileStream(zipFile, FileMode.Create, FileAccess.Write))
  31. {
  32. try
  33. {
  34. archive.SaveTo(fs_scratchPath, options);
  35. }
  36. catch (Exception ex)
  37. {
  38. FileHelper.AppendWriteFile("InstallLog.txt", ex.Message + "|" + ex.StackTrace);
  39. }
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// 解压文件
  45. /// </summary>
  46. /// <param name="zipFile">解压文件路径</param>
  47. /// <param name="outDirectory">解压文件后路径</param>
  48. public static void Decompression(string zipFile, string outDirectory)
  49. {
  50. if (File.Exists(zipFile))
  51. {
  52. ArchiveEncoding archiveEncoding = new ArchiveEncoding();
  53. archiveEncoding.Default = Encoding.GetEncoding("utf-8");
  54. SharpCompress.Readers.ReaderOptions options = new SharpCompress.Readers.ReaderOptions();
  55. options.ArchiveEncoding = archiveEncoding;
  56. options.LeaveStreamOpen = false;
  57. var archive = ArchiveFactory.Open(zipFile);
  58. foreach (var entry in archive.Entries)
  59. {
  60. if (!entry.IsDirectory)
  61. {
  62. entry.WriteToDirectory(outDirectory, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
  63. }
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 解压文件7z
  69. /// </summary>
  70. /// <param name="filePath"></param>
  71. /// <param name="outPath"></param>
  72. /// <param name="waitForCompleted"></param>
  73. /// <returns></returns>
  74. public static bool DeZip(string filePath, string outPath, bool waitForCompleted = false)
  75. {
  76. using (Process ps = new Process())
  77. {
  78. ps.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\7z\\7za.exe";
  79. ps.StartInfo.Arguments = $"x \"{filePath}\" -o\"{outPath}\" -y";
  80. ps.StartInfo.UseShellExecute = false;
  81. ps.StartInfo.CreateNoWindow = true;
  82. ps.StartInfo.Verb = "runas";
  83. if (waitForCompleted)
  84. {
  85. ps.StartInfo.RedirectStandardOutput = true;
  86. ps.StartInfo.RedirectStandardInput = true;
  87. }
  88. else
  89. {
  90. ps.StartInfo.RedirectStandardOutput = false;
  91. ps.StartInfo.RedirectStandardInput = false;
  92. }
  93. ps.Start();
  94. if (waitForCompleted)
  95. {
  96. string text = ps.StandardOutput.ReadToEnd();
  97. if (text.IndexOf("Everything is Ok") > -1)
  98. {
  99. ps.StandardInput.Write("Exit");
  100. return true;
  101. }
  102. }
  103. else
  104. {
  105. ps.WaitForExit(10000);
  106. }
  107. return false;
  108. }
  109. }
  110. /// <summary>
  111. /// 压缩文件7z
  112. /// </summary>
  113. /// <param name="filePath"></param>
  114. /// <param name="zipFile"></param>
  115. /// <param name="waitForCompleted"></param>
  116. /// <returns></returns>
  117. public static bool EnZip(string filePath, string zipFile, bool waitForCompleted = false)
  118. {
  119. using (Process ps = new Process())
  120. {
  121. ps.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\7z\\7za.exe";
  122. ps.StartInfo.Arguments = $"a -t7z \"{zipFile}\" \"{filePath}\"";
  123. ps.StartInfo.UseShellExecute = false;
  124. ps.StartInfo.CreateNoWindow = true;
  125. ps.StartInfo.Verb = "runas";
  126. if (waitForCompleted)
  127. {
  128. ps.StartInfo.RedirectStandardOutput = true;
  129. ps.StartInfo.RedirectStandardInput = true;
  130. }
  131. else
  132. {
  133. ps.StartInfo.RedirectStandardOutput = false;
  134. ps.StartInfo.RedirectStandardInput = false;
  135. }
  136. ps.Start();
  137. if (waitForCompleted)
  138. {
  139. string text = ps.StandardOutput.ReadToEnd();
  140. if (text.IndexOf("Everything is Ok") > -1)
  141. {
  142. ps.StandardInput.Write("Exit");
  143. return true;
  144. }
  145. }
  146. else
  147. {
  148. ps.WaitForExit(10000);
  149. }
  150. return false;
  151. }
  152. }
  153. }
  154. }