diff --git a/CabinetManager/CabManager.cs b/CabinetManager/CabManager.cs index 1c57339..59e79bd 100644 --- a/CabinetManager/CabManager.cs +++ b/CabinetManager/CabManager.cs @@ -47,34 +47,7 @@ public void SetCancellationToken(CancellationToken? cancelToken) { /// public int PackFileSet(IEnumerable filesToPackIn) { - int nbFilesProcessed = 0; - foreach (var cabGroupedFiles in filesToPackIn.GroupBy(f => f.CabPath)) { - try { - using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) { - cfCabinet.OnProgress += OnProgressionEvent; - try { - foreach (var fileToAddInCab in cabGroupedFiles) { - if (!File.Exists(fileToAddInCab.SourcePath)) { - continue; - } - var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath(); - cfCabinet.AddExternalFile(fileToAddInCab.SourcePath, fileRelativePath); - nbFilesProcessed++; - OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath)); - } - cfCabinet.Save(_compressionType); - } finally { - cfCabinet.OnProgress -= OnProgressionEvent; - } - } - } catch (OperationCanceledException) { - throw; - } catch (Exception e) { - throw new CabException($"Failed to pack to {cabGroupedFiles.Key}.", e); - } - OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key)); - } - return nbFilesProcessed; + return DoAction(filesToPackIn, Action.Archive); } /// @@ -96,94 +69,94 @@ public IEnumerable ListFiles(string cabPath) { /// public int ExtractFileSet(IEnumerable filesToExtract) { - int nbFilesProcessed = 0; - foreach (var cabGroupedFiles in filesToExtract.GroupBy(f => f.CabPath)) { - if (!File.Exists(cabGroupedFiles.Key)) { - continue; - } - try { - // create all necessary extraction folders - foreach (var extractDirGroupedFiles in cabGroupedFiles.GroupBy(f => Path.GetDirectoryName(f.ExtractionPath))) { - if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key)) { - Directory.CreateDirectory(extractDirGroupedFiles.Key); - } - } - using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) { - cfCabinet.OnProgress += OnProgressionEvent; - try { - foreach (var fileInCabToExtract in cabGroupedFiles) { - var fileRelativePath = fileInCabToExtract.RelativePathInCab.NormalizeRelativePath(); - if (cfCabinet.ExtractToFile(fileRelativePath, fileInCabToExtract.ExtractionPath)) { - nbFilesProcessed++; - OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath)); - } - } - } finally { - cfCabinet.OnProgress -= OnProgressionEvent; - } - } - } catch (OperationCanceledException) { - throw; - } catch (Exception e) { - throw new CabException($"Failed to extract files from {cabGroupedFiles.Key}.", e); - } - OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key)); - } - return nbFilesProcessed; + return DoAction(filesToExtract, Action.Extract); } /// public int DeleteFileSet(IEnumerable filesToDeleteIn) { - int nbFilesProcessed = 0; - foreach (var cabGroupedFiles in filesToDeleteIn.GroupBy(f => f.CabPath)) { - if (!File.Exists(cabGroupedFiles.Key)) { - continue; - } - try { - using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) { - cfCabinet.OnProgress += OnProgressionEvent; - try { - foreach (var fileToAddInCab in cabGroupedFiles) { - var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath(); - if (cfCabinet.DeleteFile(fileRelativePath)) { - nbFilesProcessed++; - OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath)); - } - } - cfCabinet.Save(_compressionType); - } finally { - cfCabinet.OnProgress -= OnProgressionEvent; - } - } - } catch (OperationCanceledException) { - throw; - } catch (Exception e) { - throw new CabException($"Failed to delete files from {cabGroupedFiles.Key}.", e); - } - OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key)); - } - return nbFilesProcessed; + return DoAction(filesToDeleteIn, Action.Delete); } /// public int MoveFileSet(IEnumerable filesToMove) { + return DoAction(filesToMove, Action.Move); + } + + /// + /// Returns a string representation of a given cabinet file. + /// + /// + /// + public string ToString(string cabPath) { + if (!File.Exists(cabPath)) { + return string.Empty; + } + using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) { + return cfCabinet.GetStringFullRepresentation(); + } + } + + private int DoAction(IEnumerable filesIn, Action action) { + var files = filesIn.ToList(); + files.ForEach(f => f.Processed = false); + int nbFilesProcessed = 0; - foreach (var cabGroupedFiles in filesToMove.GroupBy(f => f.CabPath)) { - if (!File.Exists(cabGroupedFiles.Key)) { + foreach (var groupedFiles in files.GroupBy(f => f.CabPath)) { + if (action != Action.Archive && !File.Exists(groupedFiles.Key)) { continue; } - try { - using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) { + try { + if (action == Action.Extract) { + // create all necessary extraction folders + foreach (var extractDirGroupedFiles in groupedFiles.GroupBy(f => Path.GetDirectoryName(((IFileInCabToExtract) f).ExtractionPath))) { + if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key)) { + Directory.CreateDirectory(extractDirGroupedFiles.Key); + } + } + } + using (var cfCabinet = new CfCabinet(groupedFiles.Key, _cancelToken)) { cfCabinet.OnProgress += OnProgressionEvent; try { - foreach (var fileToAddInCab in cabGroupedFiles) { - var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath(); - if (cfCabinet.MoveFile(fileRelativePath, fileToAddInCab.NewRelativePathInCab.NormalizeRelativePath())) { - nbFilesProcessed++; - OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath)); + foreach (var file in groupedFiles) { + var fileRelativePath = file.RelativePathInCab.NormalizeRelativePath(); + switch (action) { + case Action.Archive: + var fileToArchive = (IFileToAddInCab) file; + if (File.Exists(fileToArchive.SourcePath)) { + cfCabinet.AddExternalFile(fileToArchive.SourcePath, fileRelativePath); + nbFilesProcessed++; + file.Processed = true; + OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath)); + } + break; + case Action.Extract: + if (cfCabinet.ExtractToFile(fileRelativePath, ((IFileInCabToExtract) file).ExtractionPath)) { + nbFilesProcessed++; + file.Processed = true; + OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath)); + } + break; + case Action.Delete: + if (cfCabinet.DeleteFile(fileRelativePath)) { + nbFilesProcessed++; + file.Processed = true; + OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath)); + } + break; + case Action.Move: + if (cfCabinet.MoveFile(fileRelativePath, ((IFileInCabToMove) file).NewRelativePathInCab.NormalizeRelativePath())) { + nbFilesProcessed++; + file.Processed = true; + OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath)); + } + break; + default: + throw new ArgumentOutOfRangeException(nameof(action), action, null); } } - cfCabinet.Save(_compressionType); + if (action != Action.Extract) { + cfCabinet.Save(_compressionType); + } } finally { cfCabinet.OnProgress -= OnProgressionEvent; } @@ -191,25 +164,18 @@ public int MoveFileSet(IEnumerable filesToMove) { } catch (OperationCanceledException) { throw; } catch (Exception e) { - throw new CabException($"Failed to move files from {cabGroupedFiles.Key}.", e); + throw new CabException($"Failed to {action} files in {groupedFiles.Key}.", e); } - OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key)); + OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(groupedFiles.Key)); } return nbFilesProcessed; } - - /// - /// Returns a string representation of a given cabinet file. - /// - /// - /// - public string ToString(string cabPath) { - if (!File.Exists(cabPath)) { - return string.Empty; - } - using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) { - return cfCabinet.GetStringFullRepresentation(); - } + + private enum Action { + Archive, + Extract, + Delete, + Move } private void OnProgressionEvent(object sender, CfSaveEventArgs e) { diff --git a/CabinetManager/ICabManager.cs b/CabinetManager/ICabManager.cs index f67009b..708a432 100644 --- a/CabinetManager/ICabManager.cs +++ b/CabinetManager/ICabManager.cs @@ -54,7 +54,7 @@ public interface ICabManager { /// /// Pack (i.e. add or replace) files into cabinets. /// Non existing source files will not throw an exception. - /// You can inspect which files are processed with the event. + /// You can inspect which files are processed with the event or using . /// Packing into an existing cabinet will update it. /// Packing existing files will update them. /// @@ -78,7 +78,7 @@ public interface ICabManager { /// Extracts the given files from cabinets. /// Requesting the extraction from a non existing cabinet will not throw an exception. /// Requesting the extraction a file that does not exist in the cabinet will not throw an exception. - /// You can inspect which files are processed with the event. + /// You can inspect which files are processed with the event or using . /// /// /// @@ -92,7 +92,7 @@ public interface ICabManager { /// Deletes the given files from cabinets. /// Requesting the deletion from a non existing cabinet will not throw an exception. /// Requesting the deletion a file that does not exist in the cabinet will not throw an exception. - /// You can inspect which files are processed with the event. + /// You can inspect which files are processed with the event or using . /// /// /// @@ -106,7 +106,7 @@ public interface ICabManager { /// Moves the given files within cabinets. /// Requesting the movement from a non existing cabinet will not throw an exception. /// Requesting the movement a file that does not exist in the cabinet will not throw an exception. - /// You can inspect which files are processed with the event. + /// You can inspect which files are processed with the event or using . /// /// /// diff --git a/CabinetManager/IFileCabBase.cs b/CabinetManager/IFileCabBase.cs index d3be5cf..b807fb0 100644 --- a/CabinetManager/IFileCabBase.cs +++ b/CabinetManager/IFileCabBase.cs @@ -38,5 +38,10 @@ public interface IFileCabBase { /// string RelativePathInCab { get; } + /// + /// Boolean set after an archiver action which indicates if this file was actually processed. + /// + bool Processed { get; set; } + } } \ No newline at end of file diff --git a/CabinetManager/internal/FileInCab.cs b/CabinetManager/internal/FileInCab.cs index 2130c01..f2346ea 100644 --- a/CabinetManager/internal/FileInCab.cs +++ b/CabinetManager/internal/FileInCab.cs @@ -25,6 +25,7 @@ namespace CabinetManager.@internal { internal class FileInCab : IFileInCab { public string CabPath { get; set; } public string RelativePathInCab { get; set; } + public bool Processed { get; set; } public ulong SizeInBytes { get; set; } public DateTime LastWriteTime { get; set; } public FileAttributes FileAttributes { get; set; } diff --git a/CabinetManagerTest/Tests/ArchiveTest.cs b/CabinetManagerTest/Tests/ArchiveTest.cs index 71b001c..42f3027 100644 --- a/CabinetManagerTest/Tests/ArchiveTest.cs +++ b/CabinetManagerTest/Tests/ArchiveTest.cs @@ -63,10 +63,12 @@ protected void CreateArchive(ICabManager archiver, List listFiles) { RelativePathInCab = "random.name" }); Assert.AreEqual(modifiedList.Count - 1, archiver.PackFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed)); // test the update of archives modifiedList = listFiles.GetRange(0, 1); Assert.AreEqual(modifiedList.Count, archiver.PackFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count, modifiedList.Count(f => f.Processed)); foreach (var archive in listFiles.GroupBy(f => f.CabPath)) { if (Directory.Exists(Path.GetDirectoryName(archive.Key))) { @@ -116,6 +118,7 @@ protected void Extract(ICabManager archiver, List listFiles) { RelativePathInCab = "random.name" }); Assert.AreEqual(modifiedList.Count - 1, archiver.ExtractFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed)); foreach (var fileToExtract in listFiles) { Assert.IsTrue(File.Exists(fileToExtract.ExtractionPath), $"Extracted file does not exist : {fileToExtract.ExtractionPath}"); @@ -154,6 +157,7 @@ protected void DeleteFilesInArchive(ICabManager archiver, List listFi RelativePathInCab = "random.name" }); Assert.AreEqual(modifiedList.Count - 1, archiver.DeleteFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed)); foreach (var groupedFiles in listFiles.GroupBy(f => f.CabPath)) { var files = archiver.ListFiles(groupedFiles.Key); @@ -193,6 +197,7 @@ protected void MoveInArchives(ICabManager archiver, List listFiles) { archiver.SetCancellationToken(null); Assert.AreEqual(modifiedList.Count - 1, archiver.MoveFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed)); archiver.OnProgress -= ArchiverOnOnProgress; @@ -208,6 +213,7 @@ protected void MoveInArchives(ICabManager archiver, List listFiles) { }); Assert.AreEqual(modifiedList.Count - 1, archiver.MoveFileSet(modifiedList)); + Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed)); modifiedList.ForEach(f => { f.RelativePathInCab = f.NewRelativePathInCab; diff --git a/CabinetManagerTest/Tests/FileInCab.cs b/CabinetManagerTest/Tests/FileInCab.cs index bdf32ba..1af47b0 100644 --- a/CabinetManagerTest/Tests/FileInCab.cs +++ b/CabinetManagerTest/Tests/FileInCab.cs @@ -25,6 +25,7 @@ namespace CabinetManagerTest.Tests { public class FileInCab : IFileInCabToDelete, IFileInCabToExtract, IFileToAddInCab, IFileInCabToMove { public string CabPath { get; set; } public string RelativePathInCab { get; set; } + public bool Processed { get; set; } public ulong SizeInBytes { get; set; } public DateTime LastWriteTime { get; set; } public string ExtractionPath { get; set; } diff --git a/CabinetManagerTest/UsageExample.cs b/CabinetManagerTest/UsageExample.cs index 76449ef..480277b 100644 --- a/CabinetManagerTest/UsageExample.cs +++ b/CabinetManagerTest/UsageExample.cs @@ -75,6 +75,11 @@ public void Main() { Console.WriteLine($" -> {nbProcessed} files were moved within the cabinet."); + Console.WriteLine("Listing files that were processed."); + foreach (var file in filesToMove.Where(f => f.Processed)) { + Console.WriteLine($" * {file.RelativePathInCab}"); + } + // Delete files in a cabinet Console.WriteLine("Delete file in cabinet."); nbProcessed = cabManager.DeleteFileSet(filesToMove.Select(f => CabFile.NewToDelete(f.CabPath, f.NewRelativePathInCab))); @@ -101,6 +106,7 @@ private class CabFile : IFileInCabToDelete, IFileInCabToExtract, IFileToAddInCab public string CabPath { get; private set; } public string RelativePathInCab { get; private set; } + public bool Processed { get; set; } public string ExtractionPath { get; private set; } public string SourcePath { get; private set; } public string NewRelativePathInCab { get; private set; } diff --git a/docs/index.md b/docs/index.md index a86ae82..10b9ca5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -41,7 +41,7 @@ namespace CabinetManagerTest { File.WriteAllText(@"my_source_file.txt", @"my content"); var cabManager = CabManager.New(); - + cabManager.SetCompressionLevel(CabCompressionLevel.None); cabManager.SetCancellationToken(null); cabManager.OnProgress += CabManagerOnProgress; @@ -69,7 +69,7 @@ namespace CabinetManagerTest { }); Console.WriteLine($" -> {nbProcessed} files were extracted from a cabinet."); - + // Move files within a cabinet var filesToMove = filesInCab.Select(f => CabFile.NewToMove(f.CabPath, f.RelativePathInCab, $"subfolder/{f.RelativePathInCab}")).ToList(); Console.WriteLine("Moving files within a cabinet."); @@ -77,6 +77,11 @@ namespace CabinetManagerTest { Console.WriteLine($" -> {nbProcessed} files were moved within the cabinet."); + Console.WriteLine("Listing files that were processed."); + foreach (var file in filesToMove.Where(f => f.Processed)) { + Console.WriteLine($" * {file.RelativePathInCab}"); + } + // Delete files in a cabinet Console.WriteLine("Delete file in cabinet."); nbProcessed = cabManager.DeleteFileSet(filesToMove.Select(f => CabFile.NewToDelete(f.CabPath, f.NewRelativePathInCab))); @@ -103,6 +108,7 @@ namespace CabinetManagerTest { public string CabPath { get; private set; } public string RelativePathInCab { get; private set; } + public bool Processed { get; set; } public string ExtractionPath { get; private set; } public string SourcePath { get; private set; } public string NewRelativePathInCab { get; private set; }