コード例 #1
0
        public void NoOverwrite_ThrowsAnException()
        {
            // Arrange - create a pre-existing 'output' file
            var expectedZipFile = Path.Combine(DefaultOutputDir, nameof(NoOverwrite_ThrowsAnException)) + Compressor.OutputExtension;

            CreateZipFile(expectedZipFile);
            Assert.True(File.Exists(expectedZipFile));

            // Act
            var options = new ZipOptions()
            {
                OverWrite = false,
                Input     = InputDir,
                Output    = expectedZipFile
            };

            var exitCode = new Compressor(options).Run();

            // Assert - final file is initial file
            Assert.Equal(1, exitCode);
            Assert.True(File.Exists(expectedZipFile));

            using var fs         = new FileStream(expectedZipFile, FileMode.Open);
            using ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read);

            Assert.Empty(zip.Entries);
        }
コード例 #2
0
        public void ExcludeDir()
        {
            // Arrange
            var          expectedZipFile = Path.Combine(DefaultOutputDir, nameof(ExcludeDir)) + Compressor.OutputExtension;
            const string excludedDir     = "dir1/";

            // Act
            var options = new ZipOptions()
            {
                ExcludedItems = new[] { excludedDir },
                Extensions    = new[] { ".cs", ".txt" },
                Output        = expectedZipFile,
                OverWrite     = true,
                Input         = InputDir,
            };

            var exitCode = new Compressor(options).Run();

            // Assert - excluded file is not included in the zip
            Assert.Equal(0, exitCode);
            Assert.True(File.Exists(expectedZipFile));

            using var fs         = new FileStream(expectedZipFile, FileMode.Open);
            using ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read);

            Assert.Equal(TotalCsFiles + TotalTxtFiles - 2, zip.Entries.Count);
            Assert.DoesNotContain(zip.Entries, e => Path.GetDirectoryName(e.Name) !.Contains(Path.TrimEndingDirectorySeparator(excludedDir), StringComparison.OrdinalIgnoreCase));
        }
コード例 #3
0
        public void OverWrite_CreatesANewFile()
        {
            // Arrange - create a pre-existing 'output' file
            var expectedZipFile = Path.Combine(DefaultOutputDir, nameof(OverWrite_CreatesANewFile)) + Compressor.OutputExtension;

            CreateZipFile(expectedZipFile);
            Assert.True(File.Exists(expectedZipFile));

            // Act
            var options = new ZipOptions()
            {
                OverWrite = true,
                Input     = InputDir,
                Output    = expectedZipFile
            };

            var exitCode = new Compressor(options).Run();

            // Assert - final file doesn't include original file's content
            Assert.Equal(0, exitCode);
            Assert.True(File.Exists(expectedZipFile));

            using var fs         = new FileStream(expectedZipFile, FileMode.Open);
            using ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read);

            Assert.NotEmpty(zip.Entries);
        }
コード例 #4
0
        public void NoOutputFileName_GeneratesUniqueFileName(string outputDir)
        {
            // Arrange
            var timeBefore = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());

            // Act
            var options = new ZipOptions()
            {
                Output    = outputDir,
                OverWrite = true,
                Input     = InputDir
            };

            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);
            var existingFiles = Directory.GetFiles(outputDir);

            var timeAfter = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());
            var zipFiles  = existingFiles.Where(f =>
            {
                if (Path.GetExtension(f) == Compressor.OutputExtension)
                {
                    var fileDate = ParseDateTimeFromFileName(Path.GetFileNameWithoutExtension(f));
                    return(fileDate is not null && fileDate >= timeBefore && fileDate <= timeAfter);
                }

                return(false);
            });

            Assert.Single(zipFiles);
        }
コード例 #5
0
        public void OutputPrefixSuffix(string output)
        {
            // Arrange
            var prefix = $"preprefix-{output.Replace("\\", "-")}-";
            var suffix = $"-{output.Replace("\\", "-")}-suffixfix";

            // Act
            var options = new ZipOptions()
            {
                Prefix    = prefix,
                Suffix    = suffix,
                Output    = output,
                OverWrite = true,
                Input     = InputDir
            };

            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);
            var path = string.IsNullOrEmpty(Path.GetDirectoryName(output))
                ? InputDir
                : Path.GetDirectoryName(output) !;
            var existingFiles = Directory.GetFiles(path);

            var zipFiles = existingFiles.Where(f =>
                                               Path.GetFileNameWithoutExtension(f).StartsWith(prefix) &&
                                               Path.GetFileNameWithoutExtension(f).EndsWith(suffix) &&
                                               Path.GetExtension(f) == Compressor.OutputExtension);

            Assert.Single(zipFiles);
        }
コード例 #6
0
        public void info(string options)
        {
            // bring options
            Debug.WriteLine("raw options for info: " + options);
            ZipOptions zipOptions = JSON.JsonHelper.Deserialize <ZipOptions>(options);

            string source = zipOptions.source;

            Debug.WriteLine("selected source for info:" + source);

            // ZipFile.Count not working for me, larger process needed
            long count = 0;

            count = this.getTotalOfEntries(source);

            Debug.WriteLine("Count: " + count);

            // extract last item from path (filename)
            string[] fileParts = source.Split('/');
            Debug.WriteLine("length: " + fileParts.Length);
            string name = fileParts[fileParts.Length - 1];

            // prepare return for plugin to js, communication using JSON format
            string       zipInfo = "{\"entries\":\"" + count + "\", \"fullPath\":\"" + source + "\", \"name\":\"" + name + "\"}";
            PluginResult result  = new PluginResult(PluginResult.Status.OK, zipInfo);

            result.KeepCallback = true;
            this.DispatchCommandResult(result);
        }
コード例 #7
0
    public void NoInput_ShouldFail()
    {
        // Arrange
        var options = new ZipOptions();

        // Act and assert
        Assert.Equal(1, new Compressor(options).Run());
    }
コード例 #8
0
    public void NonExistingFileInput_ShouldFail()
    {
        // Arrange
        var options = new ZipOptions()
        {
            Input = "./___non_existing_file___"
        };

        // Act and assert
        Assert.Equal(1, new Compressor(options).Run());
    }
コード例 #9
0
        public void NoOutputDir_UsesInputDirAndOutputFileName(string fileName, string extension)
        {
            // Act
            var options = new ZipOptions()
            {
                Output    = fileName + extension,
                OverWrite = true,
                Input     = InputDir
            };

            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);
            var existingFiles = Directory.GetFiles(InputDir);

            var zipFiles = existingFiles.Where(f => Path.GetFileNameWithoutExtension(f) == fileName && Path.GetExtension(f) == Compressor.OutputExtension);

            Assert.Single(zipFiles);
        }
コード例 #10
0
        public void NoOutputAndInputFile()
        {
            // Arrange
            var options = new ZipOptions()
            {
                Input     = $"{InputDir}/cs1.cs",
                Prefix    = nameof(NoOutputAndInputFile),
                OverWrite = true,
            };

            // Act
            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);

            var existingFiles = Directory.GetFiles(InputDir);

            var zipFiles = existingFiles.Where(f => Path.GetExtension(f) == Compressor.OutputExtension &&
                                               Path.GetFileNameWithoutExtension(f).Contains(options.Prefix));

            Assert.NotEmpty(zipFiles);
        }
コード例 #11
0
        public void OutPut(string output)
        {
            // Act
            var options = new ZipOptions()
            {
                Output    = output,
                OverWrite = true,
                Input     = InputDir
            };

            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);
            var path = string.IsNullOrEmpty(Path.GetDirectoryName(output))
                ? InputDir
                : Path.GetDirectoryName(output) !;
            var existingFiles = Directory.GetFiles(path);

            var zipFiles = existingFiles.Where(f => Path.GetFileNameWithoutExtension(f) == "OutPutfilename" && Path.GetExtension(f) == Compressor.OutputExtension);

            Assert.Single(zipFiles);
        }
コード例 #12
0
    public void SupportedExtensionFormats(string extension)
    {
        var outputPath = Path.Combine(DefaultOutputDir, nameof(SupportedExtensionFormats) + Compressor.OutputExtension);

        // Act
        var options = new ZipOptions()
        {
            Output     = outputPath,
            OverWrite  = true,
            Input      = InputDir,
            Extensions = new[] { extension }
        };

        var exitCode = new Compressor(options).Run();

        // Assert
        Assert.Equal(0, exitCode);
        Assert.True(File.Exists(outputPath));

        using var fs         = new FileStream(outputPath, FileMode.Open);
        using ZipArchive zip = new(fs, ZipArchiveMode.Read);

        Assert.Equal(TotalCsFiles, zip.Entries.Count);
    }
コード例 #13
0
        public void uncompress(string options)
        {
            // bring options
            Debug.WriteLine("raw options for uncompress: " + options);
            ZipOptions zipOptions = JSON.JsonHelper.Deserialize <ZipOptions>(options);

            Debug.WriteLine("selected source for uncompress:" + zipOptions.source);
            Debug.WriteLine("selected target for uncompress:" + zipOptions.target);

            // prepare file handlers for SL 4
            IsolatedStorageFile infile    = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFile outfile   = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFile path      = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFile directory = IsolatedStorageFile.GetUserStoreForApplication();

            // direct access to targetPath
            string targetPath = zipOptions.target;

            string lastMsg;

            // get total of entries
            long count = 0;

            count = this.getTotalOfEntries(zipOptions.source);

            // open zip file
            using (ZipInputStream decompressor = new ZipInputStream(infile.OpenFile(zipOptions.source, FileMode.Open)))
            {
                ZipEntry entry;

                // iterate through entries of the zip file
                while ((entry = decompressor.GetNextEntry()) != null)
                {
                    string filePath      = Path.Combine(targetPath, entry.Name);
                    string directoryPath = Path.GetDirectoryName(filePath);

                    // create directory if not exists
                    if (!string.IsNullOrEmpty(directoryPath) && !directory.FileExists(directoryPath))
                    {
                        directory.CreateDirectory(directoryPath);
                    }

                    this.processedEntities.Add(filePath);

                    // don't consume cycles to write files if it's a directory
                    if (entry.IsDirectory)
                    {
                        continue;
                    }

                    // unzip and create file
                    byte[] data = new byte[2048];
                    using (FileStream streamWriter = outfile.CreateFile(filePath))
                    {
                        int bytesRead;
                        while ((bytesRead = decompressor.Read(data, 0, data.Length)) > 0)
                        {
                            streamWriter.Write(data, 0, bytesRead);
                        }
                    }

                    lastMsg = this.publish(filePath, count);
                }
            }
        }
コード例 #14
0
 public Compressor(ZipOptions options)
 {
     _options = options;
 }