Example #1
0
        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);
        }
Example #2
0
        public ActionResult Execute(ArgumentCollection arguments)
        {
            string outputDirPath = null;
            string sourceZipPath = null;

            try
            {
                if (arguments.HasArgument(UnzipArchiveActionExecutionArgs.OutputDirPath))
                {
                    outputDirPath = arguments.GetValue <string>(UnzipArchiveActionExecutionArgs.OutputDirPath);
                }

                if (arguments.HasArgument(UnzipArchiveActionExecutionArgs.SourceZipPath))
                {
                    sourceZipPath = arguments.GetValue <string>(UnzipArchiveActionExecutionArgs.SourceZipPath);
                }

                if (string.IsNullOrWhiteSpace(outputDirPath))
                {
                    if (!string.IsNullOrWhiteSpace(OutputDirPath))
                    {
                        outputDirPath = OutputDirPath;
                    }
                }

                if (string.IsNullOrWhiteSpace(sourceZipPath))
                {
                    if (!string.IsNullOrWhiteSpace(SourceZipPath))
                    {
                        sourceZipPath = SourceZipPath;
                    }
                }

                if (string.IsNullOrWhiteSpace(sourceZipPath))
                {
                    throw new MissingArgumentException(UnzipArchiveActionExecutionArgs.SourceZipPath);
                }

                if (string.IsNullOrWhiteSpace(outputDirPath))
                {
                    throw new MissingArgumentException(UnzipArchiveActionExecutionArgs.OutputDirPath);
                }

                var result = ZipUtilities.ExtractArchive(sourceZipPath, outputDirPath, OverrideFiles);

                return(result
                    ? ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New()
                                                                       .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath)
                                                                       .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath)
                                                                       )
                    : ActionResult.Failed().WithAdditionInformation(ArgumentCollection.New()
                                                                    .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath)
                                                                    .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath)));
            }
            catch (Exception exception)
            {
                return(ActionResult.Failed().WithException(exception).WithAdditionInformation(ArgumentCollection.New()
                                                                                              .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath)
                                                                                              .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath)));
            }
        }