public void AddNoFilesToArchiveFails() { string zipPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".zip"); Assert.IsFalse(ZipUtilities.AddFilesToArchive(zipPath, null)); Assert.IsFalse(ZipUtilities.AddFilesToArchive(zipPath, new List <string>())); }
public void AddSingleFileToNonExistingZipSuccessTest() { string ticks = $"{DateTime.Now.Ticks}"; string zipName = $"arc{ticks}.zip"; string zipPath = $"{Path.Combine(Path.GetTempPath(), zipName)}"; // Create file with some data string fileName = $"file{ticks}.txt"; string filePath = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(filePath, $"{this.GetType()}"); Assert.IsTrue(File.Exists(filePath)); // Add the file to the zip bool result = ZipUtilities.AddFilesToArchive(zipPath, new List <string>() { filePath }); Assert.IsTrue(result); Assert.IsTrue(File.Exists(zipPath)); // Cleanup File.Delete(filePath); File.Delete(zipPath); }
public void ExtractArchiveToDirectoryTest() { string ticks = $"{DateTime.Now.Ticks}"; string directoryName = $"dir{ticks}"; string directoryPath = Path.Combine(Path.GetTempPath(), directoryName); string zipPath = $"{directoryPath}.zip"; // Create empty directory Directory.CreateDirectory(directoryPath); Assert.IsTrue(Directory.Exists(directoryPath)); // Zip empty directory ZipFile.CreateFromDirectory(directoryPath, zipPath); Assert.IsTrue(File.Exists(zipPath)); // Create file with some data string fileName = $"file{ticks}.txt"; string filePath = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(filePath, $"{this.GetType()}"); Assert.IsTrue(File.Exists(filePath)); // Get initial zip size FileInfo fileInfo = new FileInfo(zipPath); var initialZipSize = fileInfo.Length; // Add the file to the zip bool result = ZipUtilities.AddFilesToArchive(zipPath, new List <string>() { filePath }); Assert.IsTrue(result); // Get new zip size fileInfo.Refresh(); var newZipSize = fileInfo.Length; // Expect new size is bigger than initial zip size Assert.IsTrue(newZipSize > initialZipSize); string outputDir = Path.Combine(Path.GetTempPath(), $"output{ticks}"); var extractionResult = ZipUtilities.ExtractArchive(zipPath, outputDir, false); Assert.IsTrue(extractionResult); Assert.IsTrue(Directory.Exists(outputDir)); extractionResult = ZipUtilities.ExtractArchive(zipPath, outputDir, true); Assert.IsTrue(extractionResult); // Cleanup Directory.Delete(outputDir, true); Directory.Delete(directoryPath); File.Delete(filePath); File.Delete(zipPath); }
public ActionResult Execute(ArgumentCollection arguments) { List <string> files = null; try { if (arguments.HasArgument(ZipFilesActionExecutionArgs.FilesPaths)) { if (!(arguments[ZipFilesActionExecutionArgs.FilesPaths] is List <string> filePaths)) { throw new ArgumentsValidationException( $"unable to cast argument value into list of string. key({ZipFilesActionExecutionArgs.FilesPaths})"); } files = filePaths; // arguments.GetValue<string[]>(ZipFilesActionExecutionArgs.FilesPaths); } else if (arguments.HasArgument(ZipFilesActionExecutionArgs.FilesInDirectoryPath)) { var directory = arguments.GetValue <string>(ZipFilesActionExecutionArgs.FilesInDirectoryPath); if (Directory.Exists(directory)) { files = Directory.GetFiles(directory).ToList(); } } if (files == null) { if (!string.IsNullOrWhiteSpace(FilePath)) { files = new List <string>() { FilePath }; } else if (!string.IsNullOrWhiteSpace(FilesInDirectoryPath) && Directory.Exists(FilesInDirectoryPath)) { if (Directory.Exists(FilesInDirectoryPath)) { files = Directory.GetFiles(FilesInDirectoryPath).ToList(); } } } if (files == null) { throw new MissingArgumentsException($"Not source files to zip are defined. " + $"at least one of the following arguments must be set [{ZipFilesActionInstanceArgs.FilePath}, {ZipFilesActionInstanceArgs.FilesInDirectoryPath}, {ZipFilesActionExecutionArgs.FilesInDirectoryPath}, {ZipFilesActionExecutionArgs.FilesPaths} ]"); } if (!files.Any()) { LoggingService.Warn($"No files found to zip"); return(ActionResult.Succeeded()); } var missingFiles = files.Where(f => !File.Exists(f)).ToList(); if (!IgnoreMissingFiles && missingFiles.Any()) { throw new Exception( $"Cannot execute action because the following files are not found : ({string.Join(",", missingFiles)})"); } else if (IgnoreMissingFiles && files.Count == 1 && missingFiles.Any()) { throw new Exception( $"Cannot execute action because the unique file to zip is not found ({files.Single()})"); } string zipPath = null; if (arguments.HasArgument(ZipFilesActionExecutionArgs.ArchivePath)) { zipPath = arguments.GetValue <string>(ZipFilesActionExecutionArgs.ArchivePath); } else if (!string.IsNullOrWhiteSpace(ArchivePath)) { zipPath = ArchivePath; } if (string.IsNullOrWhiteSpace(zipPath)) { throw new MissingArgumentsException( $"No archive path was specified in instance or executing arguments." + $" at least one of this arguments must be set Instance[{ZipFilesActionInstanceArgs.ArchivePath}] Execution[{ZipFilesActionExecutionArgs.ArchivePath}]"); } var results = ZipUtilities.AddFilesToArchive(zipPath, files); if (results) { if (DeleteZippedFiles) { LoggingService.Debug($"Deleting ({files.Count}) Zipped files"); foreach (var file in files) { try { LoggingService.Debug($"Deleting Zipped File {file}"); File.Delete(file); LoggingService.Debug($"Zipped File {file} deleted successfully"); } catch (Exception exception) { LoggingService.Error(exception); } } } else if (MoveZippedFiles && !string.IsNullOrWhiteSpace(MoveZippedFilesToPath)) { LoggingService.Debug($"Moving ({files.Count}) Zipped files to path {MoveZippedFilesToPath}"); if (!Directory.Exists(MoveZippedFilesToPath)) { Directory.CreateDirectory(MoveZippedFilesToPath); } foreach (var file in files) { try { var destinationFilePath = Path.Combine(MoveZippedFilesToPath, Path.GetFileName(file)); LoggingService.Debug($"Moving file {file} to {destinationFilePath}"); File.Move(file, destinationFilePath); LoggingService.Debug( $"Zipped file {Path.GetFileName(file)} moved successfully to {MoveZippedFilesToPath}"); } catch (Exception exception) { LoggingService.Error(exception); } } } } return(results? ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files) ) : ActionResult.Failed() .WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files ?? new List <string>()) ) ); } catch (Exception exception) { return(ActionResult.Failed().WithException(exception) .WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files ?? new List <string>()) ) ); } }