diff --git a/src/twig.Benchmark/ArchiverBenchmark.cs b/src/twig.Benchmark/ArchiverBenchmark.cs index 09eba19..a83eda8 100644 --- a/src/twig.Benchmark/ArchiverBenchmark.cs +++ b/src/twig.Benchmark/ArchiverBenchmark.cs @@ -14,6 +14,8 @@ public class ArchiverBenchmark RandomDataHelper.TempDirectory, RandomDataHelper.FileName); + private DefaultCommand.Settings Settings { get; set; } = new (); + [ParamsSource(nameof(ValuesForLevel))] public int Level { get; set; } public IEnumerable ValuesForLevel => new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }; @@ -21,13 +23,20 @@ public class ArchiverBenchmark [Benchmark] public async Task Compress() { - await Archiver.CompressAsync(_toCompressPath, Level, true, false, false, false, Path.Combine(_toCompressPath + Level), false, false); + Settings.Path = _toCompressPath; + Settings.CompressionLevel = Level; + Settings.Overwrite = true; + Settings.OutputPath = Path.Combine(_toCompressPath + Level); + + await Archiver.CompressAsync(Settings); } [Benchmark] public async Task Decompress() { - await Archiver.DecompressAsync(Path.Combine(_toCompressPath + Level), true, false, false, "", false, false); + Settings.Path = Path.Combine(_toCompressPath + Level); + Settings.Overwrite = true; + await Archiver.DecompressAsync(Settings); } } } diff --git a/src/twig/Archiver/Archiver.cs b/src/twig/Archiver/Archiver.cs index 910e589..a9bd260 100644 --- a/src/twig/Archiver/Archiver.cs +++ b/src/twig/Archiver/Archiver.cs @@ -18,8 +18,18 @@ public static IDisposable ReportProgress(ProgressTask task) return new ProgressBarDisposable(task); } - public static async Task CompressAsync(string path, int compressionLevel, bool overwrite, bool subfolder, bool replicate, bool verbose, string output, bool remove, bool automaticMode = false, long size = 0) + public static async Task CompressAsync(DefaultCommand.Settings settings, bool automaticMode = false, long size = 0) { + var path = settings.Path; + var compressionLevel = settings.CompressionLevel; + var overwrite = settings.Overwrite; + var subfolder = settings.Subfolder; + var replicate = settings.Replicate; + var verbose = settings.Verbose; + var output = settings.OutputPath; + var remove = settings.Remove; + + using var options = new CompressionOptions(compressionLevel); using var compressor = new Compressor(options); var attr = File.GetAttributes(path); @@ -96,8 +106,15 @@ private static async Task WriteCompressedDataAsync(string path, Compressor compr } } - public static async Task DecompressAsync(string path, bool overwrite, bool subfolder, bool replicate, string output, bool remove, bool automaticMode = false, long size = 0) + public static async Task DecompressAsync(DefaultCommand.Settings settings, bool automaticMode = false, long size = 0) { + var path = settings.Path; + var overwrite = settings.Overwrite; + var subfolder = settings.Subfolder; + var replicate = settings.Replicate; + var output = settings.OutputPath; + var remove = settings.Remove; + using var decompressor = new Decompressor(); FileAttributes attr = File.GetAttributes(path); if (attr.HasFlag(FileAttributes.Directory)) @@ -173,42 +190,43 @@ public static void RemoveOriginal(string path, bool remove) } } - public static async Task RunArchiver(string path, int compressionLevel, bool overwrite, bool subfolder, bool replicate, bool verbose, string output, bool remove, ProgressTask task) + public static async Task RunArchiver(DefaultCommand.Settings settings, ProgressTask task) { - if (!File.GetAttributes(path).HasFlag(FileAttributes.Directory) && path.EndsWith(".zs")) + if (!File.GetAttributes(settings.Path).HasFlag(FileAttributes.Directory) && settings.Path.EndsWith(".zs")) { - await DecompressAsync(path, overwrite, subfolder, replicate, output, remove); + await DecompressAsync(settings); return; } - if (!File.GetAttributes(path).HasFlag(FileAttributes.Directory) && !path.EndsWith(".zs")) + if (!File.GetAttributes(settings.Path).HasFlag(FileAttributes.Directory) && !settings.Path.EndsWith(".zs")) { - await CompressAsync(path, compressionLevel, overwrite, subfolder, replicate, verbose, output, remove); + await CompressAsync(settings); return; } - var size = FileHelper.GetTotalSize(path, subfolder); + var size = FileHelper.GetTotalSize(settings.Path, settings.Subfolder); ProgressStart?.Invoke(null, new ProgressStartEventArgs(size)); - if (File.GetAttributes(path).HasFlag(FileAttributes.Directory)) + if (File.GetAttributes(settings.Path).HasFlag(FileAttributes.Directory)) { - var paths = Directory.GetFiles(path); - if (subfolder) + var paths = Directory.GetFiles(settings.Path); + if (settings.Subfolder) { - paths = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); + paths = Directory.GetFiles(settings.Path, "*.*", SearchOption.AllDirectories); } foreach (var p in paths) { - var dir = Path.GetDirectoryName(p) == path ? "" : Path.GetFileName(Path.GetDirectoryName(p)); - var outputPath = replicate ? Path.Combine(output, dir) : output; + settings.Path = p; + var dir = Path.GetDirectoryName(p) == settings.Path ? "" : Path.GetFileName(Path.GetDirectoryName(p)); + settings.OutputPath = settings.Replicate ? Path.Combine(settings.OutputPath, dir) : settings.OutputPath; if (p.EndsWith(".zs")) { - await DecompressAsync(p, overwrite, subfolder, replicate, outputPath, remove, true); + await DecompressAsync(settings, true); } if (!p.EndsWith(".zs")) { - await CompressAsync(p, compressionLevel, overwrite, subfolder, replicate, verbose, outputPath, remove, true); + await CompressAsync(settings, true); } } ProgressFinish?.Invoke(null, new ProgressFinishEventArgs()); diff --git a/src/twig/Commands/DefaultCommand.cs b/src/twig/Commands/DefaultCommand.cs index 49d4ac1..f2b80ef 100644 --- a/src/twig/Commands/DefaultCommand.cs +++ b/src/twig/Commands/DefaultCommand.cs @@ -140,57 +140,28 @@ public override async Task ExecuteAsync(CommandContext context, Settings se if (settings.IsCompressionMode) { await AnsiConsole.Progress() - .StartExecuteAsync("Compressing...", async (task) => await Archiver.CompressAsync( - settings.Path, - settings.CompressionLevel, - settings.Overwrite, - settings.Subfolder, - settings.Replicate, - settings.Verbose, - settings.OutputPath, - settings.Remove - ) + .StartExecuteAsync("Compressing...", async (task) => await Archiver.CompressAsync(settings) ); } if (settings.IsDecompressionMode) { await AnsiConsole.Progress() - .StartExecuteAsync("Decompressing...", async (task) => await Archiver.DecompressAsync( - settings.Path, - settings.Overwrite, - settings.Subfolder, - settings.Replicate, - settings.OutputPath, - settings.Remove - ) + .StartExecuteAsync("Decompressing...", async (task) => await Archiver.DecompressAsync(settings) ); } if (settings.AdviseDuration == 0 && !settings.IsCompressionMode && !settings.IsDecompressionMode) { await AnsiConsole.Progress() - .StartExecuteAsync("Processing...", async (task) => await Archiver.RunArchiver( - settings.Path, - settings.CompressionLevel, - settings.Overwrite, - settings.Subfolder, - settings.Replicate, - settings.Verbose, - settings.OutputPath, - settings.Remove, - task - ) + .StartExecuteAsync("Processing...", async (task) => await Archiver.RunArchiver(settings, task) ); } if (settings.AdviseDuration > 0 && !settings.IsCompressionMode && !settings.IsDecompressionMode) { AnsiConsole.WriteLine("Looking for the best compression level for given duration. Please wait...") ; - await AdviseLogger.CheckForBestLevel( - settings.AdviseDuration, - settings.Path - ); + await AdviseLogger.CheckForBestLevel(settings); } _console.WriteLine("Task completed."); diff --git a/src/twig/Logging/AdviseLogger.cs b/src/twig/Logging/AdviseLogger.cs index 544bd58..c72f42f 100644 --- a/src/twig/Logging/AdviseLogger.cs +++ b/src/twig/Logging/AdviseLogger.cs @@ -8,18 +8,22 @@ public static class AdviseLogger { - public static async Task CheckForBestLevel(int duration, string path) + public static async Task CheckForBestLevel(DefaultCommand.Settings settings) { var bestLevel = 1; var appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var tempDirectory = "WildGums\\twig\\temp"; - var tempPath = Path.Combine(appdataPath, tempDirectory); + settings.OutputPath = Path.Combine(appdataPath, tempDirectory); + settings.Overwrite = true; + var path = settings.Path; + var duration = settings.AdviseDuration; try { for (int level = 1; level < 22; level++) { + settings.CompressionLevel = level; var watch = Stopwatch.StartNew(); - await Archiver.CompressAsync(path, level, true, false, false, false, tempPath, false); + await Archiver.CompressAsync(settings); watch.Stop(); if (watch.ElapsedMilliseconds <= duration) { @@ -35,7 +39,7 @@ public static async Task CheckForBestLevel(int duration, string path) } finally { - Directory.Delete(tempPath, true); + Directory.Delete(settings.OutputPath, true); } AnsiConsole.MarkupLine($"[green] The best compression level for {path} and duration {duration} is: {bestLevel}. [/]");