2025-01-11 09:12:21 +03:00
|
|
|
using LootDumpProcessor.Process.Reader.PreProcess;
|
2023-08-12 19:08:38 +01:00
|
|
|
using SevenZip;
|
2025-01-11 09:12:21 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
public class SevenZipPreProcessReader : AbstractPreProcessReader
|
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
private readonly ILogger<SevenZipPreProcessReader> _logger;
|
|
|
|
|
|
|
|
public SevenZipPreProcessReader(ILogger<SevenZipPreProcessReader> logger) : base(logger)
|
|
|
|
{
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:08:38 +01:00
|
|
|
public override string GetHandleExtension() => "7z";
|
|
|
|
|
|
|
|
static SevenZipPreProcessReader()
|
|
|
|
{
|
|
|
|
SevenZipBase.SetLibraryPath("./x64/7z.dll");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool TryPreProcess(string file, out List<string> files, out List<string> directories)
|
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
files = new List<string>();
|
|
|
|
directories = new List<string>();
|
|
|
|
|
2023-08-12 19:08:38 +01:00
|
|
|
var fileRaw = Path.GetFileNameWithoutExtension(file);
|
2025-01-11 09:12:21 +03:00
|
|
|
// SevenZip library doesn't handle forward slashes properly
|
2023-08-12 19:08:38 +01:00
|
|
|
var outPath = $"{_tempFolder}\\{fileRaw}".Replace("/", "\\");
|
2025-01-11 09:12:21 +03:00
|
|
|
|
|
|
|
_logger.LogInformation("Unzipping {File} into temp path {OutPath}, this may take a while...", file, outPath);
|
|
|
|
|
2023-08-12 19:08:38 +01:00
|
|
|
var extractor = new SevenZipExtractor(file);
|
2025-01-11 09:12:21 +03:00
|
|
|
|
|
|
|
// Log progress in debug mode
|
|
|
|
extractor.Extracting += (_, args) =>
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
if (args.PercentDone % 10 == 0)
|
2024-04-16 18:29:40 +00:00
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
_logger.LogDebug("Unzip progress: {PercentDone}%", args.PercentDone);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-12 19:08:38 +01:00
|
|
|
extractor.ExtractArchive(outPath);
|
2025-01-11 09:12:21 +03:00
|
|
|
_logger.LogInformation("Finished unzipping {File} into temp path {OutPath}", file, outPath);
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
files = Directory.GetFiles(outPath).ToList();
|
|
|
|
directories = Directory.GetDirectories(outPath).ToList();
|
2025-01-11 09:12:21 +03:00
|
|
|
|
2023-08-12 19:08:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|