Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var banPrefix = new List<string>();
  21. string password = null;
  22. var inputFiles = new List<string>();
  23. foreach (var arg in args)
  24. {
  25. if (arg.StartsWith('/') && arg.Contains(':') && arg[^1] != ':')
  26. {
  27. var kv = arg.Substring(1).Split(":", 2);
  28. switch (kv[0].ToLower())
  29. {
  30. case "out-dir":
  31. outDir = kv[1];
  32. if (false == Directory.Exists(outDir))
  33. {
  34. Console.Error.WriteLine($"Arg ERROR. out dir not found: {outDir}");
  35. return -1;
  36. }
  37. break;
  38. case "ban-prefix":
  39. banPrefix.Add(kv[1]);
  40. break;
  41. case "pass":
  42. password = kv[1];
  43. break;
  44. }
  45. continue;
  46. }
  47. inputFiles.Add(arg);
  48. }
  49. foreach (var inputFilePath in inputFiles)
  50. {
  51. Console.WriteLine("Process start");
  52. if (!File.Exists(inputFilePath))
  53. {
  54. Console.WriteLine($"ERR, Input file not found: {inputFilePath}");
  55. continue;
  56. }
  57. var outputFilePath = Path.ChangeExtension(inputFilePath, ".zip");
  58. if (outDir != null)
  59. {
  60. outputFilePath = Path.Combine(outDir, Path.GetFileName(outputFilePath));
  61. }
  62. if (File.Exists(outputFilePath))
  63. {
  64. Console.WriteLine($"SKIP, Output file already exist: {outputFilePath}");
  65. continue;
  66. }
  67. Console.WriteLine($" In: {inputFilePath}");
  68. Console.WriteLine($" Out: {outputFilePath}");
  69. using var inputArchive = password == null
  70. ? new SevenZipExtractor(inputFilePath)
  71. : new SevenZipExtractor(inputFilePath, password);
  72. var inputItems = inputArchive.ArchiveFileData.OrderBy(p => p.Index).ToArray();
  73. var streamDict = new Dictionary<string, Stream>();
  74. foreach (var item in inputItems)
  75. {
  76. if (item.IsDirectory) continue;
  77. var ban = false;
  78. foreach (var prefix in banPrefix)
  79. {
  80. if (item.FileName.StartsWith(prefix))
  81. {
  82. ban = true;
  83. break;
  84. }
  85. }
  86. if (ban)
  87. {
  88. Console.WriteLine($"Banned: {item.FileName}");
  89. continue;
  90. }
  91. streamDict.Add(item.FileName, new BridgeStream(1, (long?)item.Size, inputArchive, item.Index));
  92. }
  93. ////////////////////
  94. var outputArchive = new SevenZipCompressor
  95. {
  96. ArchiveFormat = OutArchiveFormat.Zip,
  97. CompressionMethod = CompressionMethod.Copy,
  98. CompressionMode = CompressionMode.Create,
  99. };
  100. outputArchive.FileCompressionStarted += (_, eventArgs) => Console.WriteLine($"EntryStarted:{eventArgs.FileName} ");
  101. outputArchive.Compressing += (_, eventArgs) => Console.Write($"{eventArgs.PercentDone}% ");
  102. outputArchive.FileCompressionFinished += (_, _) => Console.WriteLine($"EntryFinished");
  103. outputArchive.CompressStreamDictionary(streamDict, outputFilePath);
  104. Console.WriteLine("Process Finished");
  105. }
  106. return 0;
  107. }
  108. }
  109. }