Example #1
0
        /// <param name="args">Command-line args</param>
        /// <param name="processorCount">Number of processors on the current machine.</param>
        /// <param name="memory">Size of RAM on the current machine.</param>
        public static void Run(string[] args, int processorCount, ulong memory)
        {
            var compressor = new FileCompressor(processorCount, GetUsedMemory(memory));

            try
            {
                var command = CommandParser.Parse(args);
                switch (command)
                {
                case CompressCommand compressCommand:
                    compressor.Compress(compressCommand.InputFilePath, compressCommand.OutputFilePath);
                    break;

                case DecompressCommand decompressCommand:
                    compressor.Decompress(decompressCommand.InputFilePath, decompressCommand.OutputFilePath);
                    break;

                default:
                    throw new InvalidOperationException(Messages.CommandNotSupported);
                }
                System.Console.WriteLine(Messages.SuccessOperation);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }

            System.Console.ReadKey();
        }
        public void CompressAndDecompressCanHandleSubdirectories()
        {
            var targetDirectory = k_SourceDirectory + "2";
            var success         = FileCompressor.Compress(k_SourceDirectory, "artifacts.sbpGz");

            Assert.IsTrue(success);

            success = FileCompressor.Decompress("artifacts.sbpGz", targetDirectory);
            Assert.IsTrue(success);

            for (int i = 0; i < k_SourceFiles.Length; i++)
            {
                var sourcePath = NormalizePath(k_SourceDirectory + k_SourceFiles[i]);
                var targetPath = NormalizePath(targetDirectory + k_SourceFiles[i]);
                FileAssert.Exists(targetPath);
                FileAssert.AreEqual(sourcePath, targetPath);
            }

            File.Delete("artifacts.sbpGz");
            Directory.Delete(targetDirectory, true);
        }
Example #3
0
        /// <summary>
        /// Decompress the received file or folder
        /// <para>Returns the path of the folder or file.</para>
        /// </summary>
        /// <param name="path"></param>
        public string Decompress(string path)
        {
            FileInfo info = new FileInfo(path);

            if (info.Extension == FileCompressor.Extension)
            {
                FileInfo decompressedFile = FileCompressor.Decompress(info);
                File.Delete(info.FullName);
                return(decompressedFile.Name);
            }

            if (info.Extension == FolderCompressor.Extension)
            {
                DirectoryInfo extractedFolder = new DirectoryInfo(info.FullName.Remove(info.FullName.Length - info.Extension.Length));
                FolderCompressor.Extract(info.FullName, extractedFolder.FullName);
                File.Delete(info.FullName);
                return(extractedFolder.Name);
            }

            return(info.Name);
        }