Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using SevenZip;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace SevenRepacker
  7. {
  8. internal class Program
  9. {
  10. private static int Main(string[] args)
  11. {
  12. #if DEBUG
  13. SevenZipBase.SetLibraryPath(Environment.Is64BitProcess
  14. ? "x64/7z.dll"
  15. : "x86/7z.dll");
  16. #else
  17. SevenZipBase.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll"));
  18. #endif
  19. string outDir = null;
  20. string password = null;
  21. var banPrefix = new List<string>();
  22. var banSuffix = new List<string>();
  23. var inputFiles = new List<string>();
  24. foreach (var arg in args)
  25. {
  26. if (arg.StartsWith('/') && arg.Contains(':') && arg[^1] != ':')
  27. {
  28. var kv = arg.Substring(1).Split(":", 2);
  29. switch (kv[0].ToLower())
  30. {
  31. case "out-dir":
  32. outDir = kv[1];
  33. if (false == Directory.Exists(outDir))
  34. {
  35. Console.Error.WriteLine($"Arg ERROR. out dir not found: {outDir}");
  36. return -1;
  37. }
  38. break;
  39. case "ban-prefix":
  40. banPrefix.Add(kv[1]);
  41. break;
  42. case "ban-suffix":
  43. banSuffix.Add(kv[1]);
  44. break;
  45. case "pass":
  46. password = kv[1];
  47. break;
  48. }
  49. continue;
  50. }
  51. inputFiles.Add(arg);
  52. }
  53. foreach (var inputFilePath in inputFiles)
  54. {
  55. Console.WriteLine("Process start");
  56. if (!File.Exists(inputFilePath))
  57. {
  58. Console.WriteLine($"ERR, Input file not found: {inputFilePath}");
  59. continue;
  60. }
  61. var outputFilePath = Path.ChangeExtension(inputFilePath, ".zip");
  62. if (outDir != null)
  63. {
  64. outputFilePath = Path.Combine(outDir, Path.GetFileName(outputFilePath));
  65. }
  66. if (File.Exists(outputFilePath))
  67. {
  68. Console.WriteLine($"SKIP, Output file already exist: {outputFilePath}");
  69. continue;
  70. }
  71. Console.WriteLine($" In: {inputFilePath}");
  72. Console.WriteLine($" Out: {outputFilePath}");
  73. using var inputArchive = password == null
  74. ? new SevenZipExtractor(inputFilePath)
  75. : new SevenZipExtractor(inputFilePath, password);
  76. var inputItems = inputArchive.ArchiveFileData.OrderBy(p => p.Index).ToArray();
  77. var streamDict = new Dictionary<string, Stream>();
  78. foreach (var item in inputItems)
  79. {
  80. if (item.IsDirectory) continue;
  81. var ban = false;
  82. foreach (var prefix in banPrefix)
  83. {
  84. if (item.FileName.StartsWith(prefix))
  85. {
  86. ban = true;
  87. break;
  88. }
  89. }
  90. if (!ban)
  91. {
  92. foreach (var suffix in banSuffix)
  93. {
  94. if (item.FileName.EndsWith(suffix))
  95. {
  96. ban = true;
  97. break;
  98. }
  99. }
  100. }
  101. if (ban)
  102. {
  103. Console.WriteLine($"Banned: {item.FileName}");
  104. continue;
  105. }
  106. streamDict.Add(item.FileName, new BridgeStream(1, (long?)item.Size, inputArchive, item.Index));
  107. }
  108. ////////////////////
  109. var outputArchive = new SevenZipCompressor
  110. {
  111. ArchiveFormat = OutArchiveFormat.Zip,
  112. CompressionMethod = CompressionMethod.Copy,
  113. CompressionMode = CompressionMode.Create,
  114. };
  115. outputArchive.FileCompressionStarted += (_, eventArgs) => Console.WriteLine($"EntryStarted:{eventArgs.FileName} ");
  116. outputArchive.Compressing += (_, eventArgs) => Console.Write($"{eventArgs.PercentDone}% ");
  117. outputArchive.FileCompressionFinished += (_, _) => Console.WriteLine($"EntryFinished");
  118. outputArchive.CompressStreamDictionary(streamDict, outputFilePath);
  119. Console.WriteLine("Process Finished");
  120. }
  121. return 0;
  122. }
  123. }
  124. }