From 4f7e4c7ade3b9b19e30ce4abc5b3317efacadd6e Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Tue, 21 Mar 2023 13:34:43 +0100 Subject: [PATCH 1/6] Archieve Creator Progress --- .../Filesystem/Archive/ArchiveCreator.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index 502c26c6219c..2ce03c11a39b 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -11,6 +11,10 @@ namespace Files.App.Filesystem.Archive { public class ArchiveCreator : IArchiveCreator { + private int itemsAmount = 1; + private int processedItems = 0; + private int oldPercentage = 0; + private string archivePath = string.Empty; public string ArchivePath => archivePath; @@ -104,6 +108,8 @@ public async Task RunCreationAsync() var files = sources.Where(source => File.Exists(source)).ToArray(); var directories = sources.Where(source => System.IO.Directory.Exists(source)); + itemsAmount = files.Length + directories.Count(); + foreach (string directory in directories) { await compressor.CompressDirectoryAsync(directory, path, Password); @@ -131,13 +137,22 @@ public async Task RunCreationAsync() private void Compressor_CompressionFinished(object? sender, EventArgs e) { - fsProgress.Percentage = null; - fsProgress.ReportStatus(Shared.Enums.FileSystemStatusCode.Success); + if (++processedItems == itemsAmount) + { + fsProgress.Percentage = null; + fsProgress.ReportStatus(Shared.Enums.FileSystemStatusCode.Success); + } + else + { + oldPercentage = processedItems * 100 / itemsAmount + fsProgress.Percentage = oldPercentage; + fsProgress.Report(); + } } private void Compressor_Compressing(object? _, ProgressEventArgs e) { - fsProgress.Percentage = e.PercentDone; + fsProgress.Percentage = oldPercentage + e.PercentDone / itemsAmount; fsProgress.Report(); } } From 3508e5c346d3e329636aa0b41648f89044dd5200 Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Tue, 21 Mar 2023 13:35:13 +0100 Subject: [PATCH 2/6] Missing ; --- src/Files.App/Filesystem/Archive/ArchiveCreator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index 2ce03c11a39b..38d7e28ebe66 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -144,7 +144,7 @@ private void Compressor_CompressionFinished(object? sender, EventArgs e) } else { - oldPercentage = processedItems * 100 / itemsAmount + oldPercentage = processedItems * 100 / itemsAmount; fsProgress.Percentage = oldPercentage; fsProgress.Report(); } From ada9e97b1167b1eadbddc3ee88715e03839bd1e6 Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Tue, 21 Mar 2023 13:40:11 +0100 Subject: [PATCH 3/6] Use PercentageDelta --- src/Files.App/Filesystem/Archive/ArchiveCreator.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index 38d7e28ebe66..f8803b46ced6 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -13,7 +13,6 @@ public class ArchiveCreator : IArchiveCreator { private int itemsAmount = 1; private int processedItems = 0; - private int oldPercentage = 0; private string archivePath = string.Empty; public string ArchivePath => archivePath; @@ -144,15 +143,14 @@ private void Compressor_CompressionFinished(object? sender, EventArgs e) } else { - oldPercentage = processedItems * 100 / itemsAmount; - fsProgress.Percentage = oldPercentage; + fsProgress.Percentage = processedItems * 100 / itemsAmount; fsProgress.Report(); } } private void Compressor_Compressing(object? _, ProgressEventArgs e) { - fsProgress.Percentage = oldPercentage + e.PercentDone / itemsAmount; + fsProgress.Percentage += e.PercentDelta / itemsAmount; fsProgress.Report(); } } From c826fc2f1712cfba71bb14c98cbc85f7996a939f Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Wed, 22 Mar 2023 23:24:12 +0100 Subject: [PATCH 4/6] Archieve Name in banner & progress report --- .../Filesystem/Archive/ArchiveCreator.cs | 47 ++++++++++++------- .../Filesystem/Archive/IArchiveCreator.cs | 4 +- src/Files.App/Helpers/ArchiveHelpers.cs | 7 ++- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index f8803b46ced6..ca5fd766f878 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.NetworkInformation; using System.Threading.Tasks; namespace Files.App.Filesystem.Archive @@ -15,7 +16,11 @@ public class ArchiveCreator : IArchiveCreator private int processedItems = 0; private string archivePath = string.Empty; - public string ArchivePath => archivePath; + public string ArchivePath + { + get => archivePath; + set => archivePath = value; + } public string Directory { get; init; } = string.Empty; public string FileName { get; init; } = string.Empty; @@ -27,13 +32,24 @@ public class ArchiveCreator : IArchiveCreator public ArchiveCompressionLevels CompressionLevel { get; init; } = ArchiveCompressionLevels.Normal; public ArchiveSplittingSizes SplittingSize { get; init; } = ArchiveSplittingSizes.None; - public IProgress Progress { get; set; } = new Progress(); - private readonly FileSystemProgress fsProgress; + private IProgress progress = new Progress(); + public IProgress Progress + { + get => progress; + set + { + progress = value; + fsProgress = new(Progress, true, Shared.Enums.FileSystemStatusCode.InProgress); + fsProgress.Report(0); + } + } + + private FileSystemProgress fsProgress; public ArchiveCreator() { fsProgress = new(Progress, true, Shared.Enums.FileSystemStatusCode.InProgress); - fsProgress.Report(); + fsProgress.Report(0); } private string ArchiveExtension => FileFormat switch @@ -78,16 +94,15 @@ public ArchiveCreator() _ => throw new ArgumentOutOfRangeException(nameof(SplittingSize)), }; + public string GetArchivePath(string suffix = "") + { + return Path.Combine(Directory, $"{FileName}{suffix}{ArchiveExtension}"); + } + public async Task RunCreationAsync() { - var path = Path.Combine(Directory, FileName + ArchiveExtension); string[] sources = Sources.ToArray(); - int index = 1; - while (File.Exists(path) || System.IO.Directory.Exists(path)) - path = Path.Combine(Directory, $"{FileName} ({++index}){ArchiveExtension}"); - archivePath = path; - var compressor = new SevenZipCompressor { ArchiveFormat = SevenZipArchiveFormat, @@ -111,16 +126,16 @@ public async Task RunCreationAsync() foreach (string directory in directories) { - await compressor.CompressDirectoryAsync(directory, path, Password); + await compressor.CompressDirectoryAsync(directory, archivePath, Password); compressor.CompressionMode = CompressionMode.Append; } if (files.Any()) { if (string.IsNullOrEmpty(Password)) - await compressor.CompressFilesAsync(path, files); + await compressor.CompressFilesAsync(archivePath, files); else - await compressor.CompressFilesEncryptedAsync(path, Password, files); + await compressor.CompressFilesEncryptedAsync(archivePath, Password, files); } return true; @@ -128,7 +143,7 @@ public async Task RunCreationAsync() catch (Exception ex) { var logger = Ioc.Default.GetService(); - logger?.Warn(ex, $"Error compressing folder: {path}"); + logger?.Warn(ex, $"Error compressing folder: {archivePath}"); return false; } @@ -144,14 +159,14 @@ private void Compressor_CompressionFinished(object? sender, EventArgs e) else { fsProgress.Percentage = processedItems * 100 / itemsAmount; - fsProgress.Report(); + fsProgress.Report(fsProgress.Percentage); } } private void Compressor_Compressing(object? _, ProgressEventArgs e) { fsProgress.Percentage += e.PercentDelta / itemsAmount; - fsProgress.Report(); + fsProgress.Report(fsProgress.Percentage); } } } diff --git a/src/Files.App/Filesystem/Archive/IArchiveCreator.cs b/src/Files.App/Filesystem/Archive/IArchiveCreator.cs index fe55a88857d5..587758ecc4b6 100644 --- a/src/Files.App/Filesystem/Archive/IArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/IArchiveCreator.cs @@ -6,7 +6,7 @@ namespace Files.App.Filesystem.Archive { public interface IArchiveCreator { - string ArchivePath { get; } + string ArchivePath { get; set; } string Directory { get; } string FileName { get; } @@ -20,6 +20,8 @@ public interface IArchiveCreator IProgress Progress { get; set; } + string GetArchivePath(string suffix = ""); + Task RunCreationAsync(); } } diff --git a/src/Files.App/Helpers/ArchiveHelpers.cs b/src/Files.App/Helpers/ArchiveHelpers.cs index 2cda2b2909b0..38e4e9a8ccdc 100644 --- a/src/Files.App/Helpers/ArchiveHelpers.cs +++ b/src/Files.App/Helpers/ArchiveHelpers.cs @@ -67,7 +67,12 @@ public static (string[] Sources, string directory, string fileName) GetCompressD public static async Task CompressArchiveAsync(IArchiveCreator creator) { - var archivePath = creator.ArchivePath; + var archivePath = creator.GetArchivePath(); + + int index = 1; + while (File.Exists(archivePath) || System.IO.Directory.Exists(archivePath)) + archivePath = creator.GetArchivePath($" ({++index})"); + creator.ArchivePath = archivePath; CancellationTokenSource compressionToken = new(); PostedStatusBanner banner = App.OngoingTasksViewModel.PostOperationBanner From 889aa7b2aa5962d608fb14c924a8de0ed3c0757c Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Wed, 22 Mar 2023 23:30:56 +0100 Subject: [PATCH 5/6] Usings --- src/Files.App/Filesystem/Archive/ArchiveCreator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index ca5fd766f878..a2da3a2d299a 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net.NetworkInformation; using System.Threading.Tasks; namespace Files.App.Filesystem.Archive From cf86b357794b3230189cc8d964797a4ee769803f Mon Sep 17 00:00:00 2001 From: Filippo Ferrario Date: Fri, 24 Mar 2023 11:34:49 +0100 Subject: [PATCH 6/6] Added variable explanation --- src/Files.App/Filesystem/Archive/ArchiveCreator.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs index a2da3a2d299a..afae2d7581df 100644 --- a/src/Files.App/Filesystem/Archive/ArchiveCreator.cs +++ b/src/Files.App/Filesystem/Archive/ArchiveCreator.cs @@ -11,6 +11,9 @@ namespace Files.App.Filesystem.Archive { public class ArchiveCreator : IArchiveCreator { + // Represents the total number of items to be processed. + // It is used to calculate a weighted progress with this formula: + // Progress = [OldProgress + (ProgressDelta / ItemsAmount)] private int itemsAmount = 1; private int processedItems = 0;