Un7z.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using SharpCompress.Archives;
  2. using SharpCompress.Common;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Vit.ConsoleUtil;
  7. namespace FileZip.Cmd
  8. {
  9. public class Un7z
  10. {
  11. #region un7z
  12. [Command("un7z")]
  13. [Remarks("解压7z文件。参数说明:")]
  14. [Remarks("-i[--input] 待解压文件 例如 \"/data/a.7z.001\"")]
  15. [Remarks("-o[--output] 输出目录,(若不指定,则解压到压缩文件所在目录)")]
  16. [Remarks("-m[--mute] 若指定,则不输出解压的文件(夹)信息")]
  17. [Remarks("-mf[--mutefile] 若指定,则不输出解压的文件信息")]
  18. [Remarks("示例: un7z -i \"/data/a.7z\" -o \"/data/out\" --mute ")]
  19. public static void un7z(string[] args)
  20. {
  21. #region (x.1) get arg
  22. string input = ConsoleHelp.GetArg(args, "-i") ?? ConsoleHelp.GetArg(args, "--input");
  23. if (string.IsNullOrEmpty(input))
  24. {
  25. ConsoleHelp.Log("请指定待解压文件");
  26. return;
  27. }
  28. string output = ConsoleHelp.GetArg(args, "-o") ?? ConsoleHelp.GetArg(args, "--output");
  29. if (string.IsNullOrEmpty(output))
  30. {
  31. //var directory = Path.GetDirectoryName(input);
  32. //output = Path.Combine(directory,Path.GetFileNameWithoutExtension(input));
  33. //if (output == input)
  34. //{
  35. // output = directory;
  36. //}
  37. output = Path.GetDirectoryName(input);
  38. //ConsoleHelp.Log("请指定输出目录");
  39. //return;
  40. }
  41. Directory.CreateDirectory(output);
  42. bool printFile = (
  43. (ConsoleHelp.GetArg(args, "-m") == null && ConsoleHelp.GetArg(args, "--mute") == null )
  44. &&(ConsoleHelp.GetArg(args, "-mf") == null && ConsoleHelp.GetArg(args, "--mutefile") == null) );
  45. bool printDirectory = (ConsoleHelp.GetArg(args, "-m") == null && ConsoleHelp.GetArg(args, "--mute") == null);
  46. #endregion
  47. bool inputFileIsTemp = false;
  48. try
  49. {
  50. #region (x.2)若为压缩分卷文件则合并到临时文件
  51. {
  52. var extension = Path.GetExtension(input).TrimStart('.');
  53. if (int.TryParse(extension, out _))
  54. {
  55. var fileSearch = Path.Combine(Path.GetDirectoryName(input), Path.GetFileNameWithoutExtension(input) + ".*");
  56. input= Path.Combine(Path.GetDirectoryName(input),"tmp_"+ Path.GetFileNameWithoutExtension(input) + ".tmp");
  57. inputFileIsTemp = true;
  58. var cmd = new List<string>() { "marge" };
  59. cmd.AddRange(new[] { "-i", fileSearch });
  60. cmd.AddRange(new[] { "-o", input });
  61. Marge.marge(cmd.ToArray());
  62. }
  63. }
  64. #endregion
  65. #region (x.3) 开始解压
  66. ConsoleHelp.Log("开始解压");
  67. ConsoleHelp.Log("待解压文件:" + input);
  68. ConsoleHelp.Log("输出目录:" + output);
  69. using (var archive = ArchiveFactory.Open(input))
  70. {
  71. var sum = archive.Entries.Count();
  72. if (sum == 0)
  73. {
  74. ConsoleHelp.Log("压缩文件中没有文件");
  75. return;
  76. }
  77. int finished = 0;
  78. int step = sum / 100;
  79. int nextStep = step;
  80. foreach (var entry in archive.Entries)
  81. {
  82. finished++;
  83. if (finished >= nextStep)
  84. {
  85. nextStep += step;
  86. ConsoleHelp.Log($"[{ finished }/{sum} ] 已完成 { finished * 100 / sum }%");
  87. }
  88. if (entry.IsDirectory)
  89. {
  90. if (printDirectory) ConsoleHelp.Log($"[{ finished }/{sum} d] " + entry.Key);
  91. }
  92. else
  93. {
  94. if (printFile) ConsoleHelp.Log($"[{ finished }/{sum} f] " + entry.Key);
  95. }
  96. entry.WriteToDirectory(output, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
  97. }
  98. }
  99. #endregion
  100. }
  101. finally
  102. {
  103. if (inputFileIsTemp)
  104. {
  105. ConsoleHelp.Log("删除临时文件:" + input);
  106. File.Delete(input);
  107. }
  108. }
  109. ConsoleHelp.Log("文件解压成功!!!");
  110. }
  111. #endregion
  112. }
  113. }