SevenZipUtil.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SevenZip
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. public sealed class SevenZipUtil
  16. {
  17. /// <summary>
  18. /// 压缩单文件夹中指定类型的文件
  19. /// </summary>
  20. /// <param name="sourceFolder">源文件目录</param>
  21. /// <param name="outZipFile">要输出的压缩文件</param>
  22. /// <param name="filter">文件类型(*.txt等)</param>
  23. /// <param name="delSrcFiles">压缩完后是否删除原文件</param>
  24. public static void ZipLargeFiles(string sourceFolder, string outZipFile, string filter = "*", bool delSrcFiles = true)
  25. {
  26. string cmdFormat;
  27. if (delSrcFiles)
  28. cmdFormat = "{0} a {1} {2} -sdel";
  29. else
  30. cmdFormat = "{0} a {1} {2}";
  31. ZipUsing7zip(cmdFormat, sourceFolder, outZipFile, new string[] { filter });
  32. if (delSrcFiles)
  33. {
  34. var files = Directory.GetFiles(sourceFolder);
  35. foreach (var file in files)
  36. {
  37. if (file.ToUpper().EndsWith(filter.Replace("*", "").ToUpper()))
  38. {
  39. File.Delete(file);
  40. }
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 压缩单文件夹中指定类型的文件
  46. /// </summary>
  47. /// <param name="sourceFolder">源文件目录</param>
  48. /// <param name="outZipFile">要输出的压缩文件</param>
  49. /// <param name="filter">文件类型(*.txt等)</param>
  50. /// <param name="delSrcFiles">压缩完后是否删除原文件</param>
  51. public static void ZipLargeFiles(string sourceFolder, string outZipFile, string[] filters, bool delSrcFiles = true)
  52. {
  53. string cmdFormat;
  54. if (delSrcFiles)
  55. cmdFormat = "a {0} {1} -sdel";
  56. else
  57. cmdFormat = "a {0} {1}";
  58. ZipUsing7zip(cmdFormat, sourceFolder, outZipFile, filters);
  59. if (delSrcFiles)
  60. {
  61. var files = Directory.GetFiles(sourceFolder);
  62. foreach (var file in files)
  63. {
  64. if (EndsWith(file, filters))
  65. {
  66. File.Delete(file);
  67. }
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 解压指定的文件到压缩文件所在目录(返回目录路径)
  73. /// </summary>
  74. /// <param name="file"></param>
  75. /// <exception cref="Exception"></exception>
  76. public static string UnZipFile(string file,string filter="*")
  77. {
  78. try
  79. {
  80. string errorMsg = "";
  81. string outputMsg = "";
  82. string dir = Path.GetDirectoryName(file);
  83. bool cmdResult = ExecuteCmdCommand($"e {file} -o{dir} -y {filter}", ref errorMsg, ref outputMsg);
  84. if (!cmdResult)
  85. {
  86. string exceptionMsg = $"解压文件{file}时发生错误";
  87. if (!string.IsNullOrWhiteSpace(errorMsg)) exceptionMsg = string.Format("{0} [{1}]", exceptionMsg, errorMsg);
  88. throw new Exception(exceptionMsg);
  89. }
  90. return dir;
  91. }
  92. catch (Exception ex)
  93. {
  94. throw new Exception($"解压文件{file}时发生错误", ex);
  95. }
  96. }
  97. #region private
  98. private static bool ExecuteCmdCommand(string commandLine, ref string errorMsg, ref string outputMsg)
  99. {
  100. bool Flag = false;
  101. Process proc = new Process();
  102. try
  103. {
  104. // 配置CMD窗口的启动信息
  105. proc.StartInfo.FileName = "7z";
  106. proc.StartInfo.UseShellExecute = false;
  107. proc.StartInfo.Arguments = commandLine;
  108. proc.StartInfo.RedirectStandardOutput = true; // 开启重定向输出
  109. proc.StartInfo.RedirectStandardError = true;// 开启重定向错误输出
  110. proc.StartInfo.CreateNoWindow = true;
  111. // 不创建窗口
  112. proc.Start();
  113. proc.WaitForExit();
  114. errorMsg = proc.StandardError.ReadToEnd();
  115. outputMsg = proc.StandardOutput.ReadToEnd();
  116. if (string.IsNullOrWhiteSpace(errorMsg))
  117. Flag = true;
  118. else
  119. Flag = false;
  120. }
  121. catch (Exception ex)
  122. {
  123. errorMsg = ex.Message;
  124. Flag = false;
  125. }
  126. finally
  127. {
  128. proc.Close();
  129. proc.Dispose();
  130. }
  131. return Flag;
  132. }
  133. private static bool EndsWith(string txt, string[] filters)
  134. {
  135. foreach (var filter in filters)
  136. {
  137. if (txt.ToUpper().EndsWith(filter.Replace("*", "").ToUpper()))
  138. return true;
  139. }
  140. return false;
  141. }
  142. private static void ZipUsing7zip(string cmdFormat, string sourceFolder, string outZipFile, string[] filters)
  143. {
  144. try
  145. {
  146. // 获取要压缩的文件
  147. string filesPath = string.Join(" ", filters.Select(p => Path.Combine(sourceFolder, p)));
  148. // 格式化命令
  149. string cmd = string.Format(cmdFormat, outZipFile, filesPath);
  150. // 调用 7-Zip 执行命令
  151. string errorMsg = "";
  152. string outputMsg = "";
  153. bool cmdResult = ExecuteCmdCommand(cmd, ref errorMsg, ref outputMsg);
  154. if (!cmdResult)
  155. {
  156. string exceptionMsg = "压缩文件时发生错误";
  157. if (!string.IsNullOrWhiteSpace(errorMsg)) exceptionMsg = string.Format("{0} [{1}]", exceptionMsg, errorMsg);
  158. throw new Exception(exceptionMsg);
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. throw new Exception("压缩文件时发生错误", ex);
  164. }
  165. }
  166. #endregion
  167. }
  168. }