Exemplo n.º 1
0
        public IProcessor CreateProcessor(ProcessMode mode)
        {
            IProcessor processor;

            switch (mode)
            {
            case ProcessMode.compress:
            {
                processor = new Compressor();
                break;
            }

            case ProcessMode.decompress:
            {
                processor = new Decompressor();
                break;
            }

            default:
            {
                throw new Exception("Неверный режим работы программы!");
            }
            }
            return(processor);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                //Проверяем полученные из командной строки аргументы, если все в порядке - переходим к сжатию/расжатию файла
                Validation.Check(args);

                switch (args[0].ToLower())
                {
                case "compress":
                    Compressor gz1 = new Compressor(args[1], args[2]);
                    gz1.Launch();
                    break;

                case "decompress":
                    Decompressor gz2 = new Decompressor(args[1], args[2]);
                    gz2.Launch();
                    break;
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error is occured!\n Method: {0}\n Error description: {1}", ex.TargetSite, ex.Message + "\n");
                Console.WriteLine("This description haven`t enough info to solve the problem? Please contact with developer using this e-mail: [email protected] and tell about this error \n");
                Console.WriteLine("Press any key or Enter to close programm");
                Console.Read();
            }
        }
Exemplo n.º 3
0
        static int Main(string[] args)
        {
            try
            {
                Validator.ArgumentValidation(args);
                ICommand command;

                if (args[0].ToLower() == "compress")
                {
                    command = new Compressor(new CompressReader(args[1]), new CompressWriter(args[2]));
                }
                else
                {
                    command = new Decompressor(new DecompressReader(args[1]), new DecompressWriter(args[2]));
                }

                command.Run();

                return(command.Success);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Обнаружена ошибка!\n Метод: {0}\n Описание ошибки: {1}", ex.TargetSite.Name, ex.Message);
                return(1);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string inFile;
            string outFile;

            try
            {
                if (args[0].ToLower() == "compress")
                {
                    inFile  = args[1];
                    outFile = args[2];
                    Compressor.Compress(inFile, outFile);
                }
                else if (args[0].ToLower() == "decompress")
                {
                    inFile  = args[1];
                    outFile = args[2];
                    Decompressor.Decompress(inFile, outFile);
                }
                else
                {
                    Console.WriteLine("Incorrect input! Command example: \nGZipTest.exe [compress/decompress] [path to source file] [path to result file]");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            try
            {
                ArgsAnalyzer.InitialValidation(args);
                switch (args[0])
                {
                case "compress":
                    var compressor = new Compressor(args[1], args[2]);
                    compressor.Start();
                    break;

                case "decompress":
                    var decompressor = new Decompressor(args[1], args[2]);
                    decompressor.Start();
                    break;

                default:
                    Console.WriteLine("The first argument should be \"compress\" or \"decompress\"!");
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\nPlease fix it and try again");
            }
        }
Exemplo n.º 6
0
        public void ExecuteDecompress(ReaderForDecompress Reader, Decompressor Decompressor, WriterForDecompress Writer)
        {
            Thread reader = new Thread(new ThreadStart(Reader.Read));

            reader.Start();

            Thread[] decompressor = new Thread[compressionThreads];
            for (int i = 0; i < compressionThreads; i++)
            {
                decompressor[i] = new Thread(new ParameterizedThreadStart(Decompressor.Decompress));
                decompressor[i].Start(i);
            }

            Thread writer = new Thread(new ThreadStart(Writer.Write));

            writer.Start();

            reader.Join();
            for (int i = 0; i < compressionThreads; i++)
            {
                decompressor[i].Join();
            }

            writeBuffer.Close();
            writer.Join();
        }
Exemplo n.º 7
0
        public void Decompress()
        {
            ReaderForDecompress Reader       = new ReaderForDecompress(inputFile, readBuffer, bufferSize, canceler);
            Decompressor        Decompressor = new Decompressor(readBuffer, writeBuffer, bufferSize, canceler);
            WriterForDecompress Writer       = new WriterForDecompress(outputFile, writeBuffer, canceler);

            ExecuteDecompress(Reader, Decompressor, Writer);
        }
Exemplo n.º 8
0
        private static int Decompress(DecompressConfiguration configuration)
        {
            if (!File.Exists(configuration.Input))
            {
                Console.WriteLine("Входной файл не существует");
                return(1);
            }

            var decompressor = new Decompressor();

            decompressor.Decompress(configuration.Input, configuration.Output, ChunkSize);

            return(0);
        }
Exemplo n.º 9
0
Arquivo: Program.cs Projeto: RkeyQQ/GZ
        /// <summary>
        /// Console starting point to compress or decompress file.
        /// Determines chunk size and memory buffer to limit RAM usage.
        /// </summary>
        /// <param name="args">Must comply GZipTest.exe compress/decompress [initial file name][restul file name]</param>
        /// <returns>Returns 0 if success or 1 if error happened</returns>
        static int Main(string[] args)
        {
            int chunkSize     = 1024 * 1024;
            int memBufferSize = 3;      // max chunks in memory buffer per each buffer

            if (ParameterValidator.Validate(args) == false)
            {
                return(1);
            }
            string fileToProcess = args[1];
            string fileResult    = args[2];

            try
            {
                long length = new FileInfo(fileToProcess).Length;
                if (length == 0)
                {
                    File.Create(fileResult);
                    return(0);
                }

                if (length < chunkSize)                                 // File is smaller than chunk size
                {
                    chunkSize = (int)length;                            // file size would be used instead
                }
                IGzipOperations job = null;
                if (args[0] == "compress")
                {
                    job = new Compressor(chunkSize);
                }
                else if (args[0] == "decompress")
                {
                    job = new Decompressor();
                }

                var actionManager = new ActionManager(fileToProcess, fileResult, memBufferSize, job);
                var threadWorker  = new ThreadWorker(actionManager);
                threadWorker.StartThreads();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                return(1);
            }
            return(0);
        }
Exemplo n.º 10
0
        public IArchiverProcessor Create()
        {
            IArchiverProcessor processor = null;

            switch (_parms.Operation)
            {
                case OperationType.Compress:
                    processor = new Compressor(_parms.SourceFile, _parms.DestinationFile, _blockSize);
                    break;

                case OperationType.Decompress:
                    processor = new Decompressor(_parms.SourceFile, _parms.DestinationFile);
                    break;
            }

            return processor;
        }
Exemplo n.º 11
0
 /// <summary>
 /// Choose necessary mode and start process
 /// </summary>
 private static void LaunchZip()
 {
     if (mode == CompressionMode.Compress)
     {
         using (var compressor = new Compressor())
         {
             compressor.Compress(pathFileOriginal, pathFileResult);
         }
     }
     else
     {
         using (var decompressor = new Decompressor())
         {
             decompressor.Decompress(pathFileOriginal, pathFileResult);
         }
     }
 }
Exemplo n.º 12
0
 private static int Decompress(string compressFile, string compressedFile)
 {
     try
     {
         using (IZipProcessor d = new Decompressor(compressFile, compressedFile))
             if (d.Process())
             {
                 return(0);
             }
             else
             {
                 return(1);
             }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(1);
     }
 }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                printUsage();
                return;
            }

            CompressionMode mode = CompressionMode.Compress;

            if (string.Compare(args[0], decompress, true) == 0)
            {
                mode = CompressionMode.Decompress;
            }
            else if (string.Compare(args[0], compress, true) != 0)
            {
                printUsage();
                return;
            }


            IGZipProcessor proc = null;

            if (mode == CompressionMode.Compress)
            {
                proc = new Compressor();
            }
            else
            {
                proc = new Decompressor();
            }

            try
            {
                proc.Process(args[1], args[2], mode);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
        }
        static void Main(string[] args)
        {
            try
            {
                int result = 1;

                if (IsArgumentsValid(args))
                {
                    if (File.Exists(args[2]))
                    {
                        File.Delete(args[2]);
                    }

                    switch (args[0])
                    {
                    case "compress":
                        var compressor = new Compressor();
                        result = compressor.Compute(args[1], args[2]);
                        break;

                    case "decompress":
                        var decompressor = new Decompressor();
                        result = decompressor.Compute(args[1], args[2]);
                        break;

                    default:
                        throw new Exception("Нераспознан режим работы. Выберете compress или decompress");
                    }
                }

                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                WriteError(ex.Message);
                Console.WriteLine(1);
            }
        }
Exemplo n.º 15
0
        static int Main(string[] args)
        {
            int result = -1;

            ConsoleArgumentValidator.Validate(args);

            GZipArchiver archiver;

            try
            {
                switch (args[0].ToLower())
                {
                case "compress":
                    archiver = new Compressor(args[1], args[2]);
                    break;

                case "decompress":
                    archiver = new Decompressor(args[1], args[2]);
                    break;

                default: throw new ArgumentException("Action has not been provided");
                }

                archiver.Process();

                result = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error has been occurred: {ex.Message}");

                result = 1;
            }

            return(result);
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            ParseArgs options;

            try
            {
                options = new ParseArgs(args);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            ICommand Command = null;

            ProgressReport progress;

            if (options.Command == Operation.Compress)
            {
                Command  = new Compressor();
                progress = new ProgressReport("Compress");
            }
            else
            {
                Command  = new Decompressor();
                progress = new ProgressReport("Decompress");
            }

            _readerTaskPool = new TaskPool(_cores);
            _writerTaskPool = new TaskPool(_cores);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            Command.ShowProgress += progress.ShowProgress;
            Command.Terminate    += _readerTaskPool.Terminate;
            Command.Terminate    += _writerTaskPool.Terminate;

            _reader = new Thread(delegate() { Command.Reader(options.Source, ref _readerTaskPool); });

            for (int i = 0; i < _cores; i++)
            {
                _handlers[i] = new Thread(delegate() { Command.Handler(ref _readerTaskPool, ref _writerTaskPool); });
            }

            _writer = new Thread(delegate() { Command.Writer(options.Destination, ref _writerTaskPool); });

            _reader.Start();
            foreach (Thread handler in _handlers)
            {
                handler.Start();
            }
            _writer.Start();

            _writer.Join();
            foreach (Thread handler in _handlers)
            {
                handler.Join();
            }
            _reader.Join();

            sw.Stop();
            progress.Done(sw.Elapsed);

            Command.Terminate    -= _writerTaskPool.Terminate;
            Command.Terminate    -= _readerTaskPool.Terminate;
            Command.ShowProgress -= progress.ShowProgress;
        }
Exemplo n.º 17
0
        static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Invalid argument count.");
                Console.WriteLine(Usage);
                return(1);
            }
            var inputFilePath = args[1];

            if (!File.Exists(inputFilePath))
            {
                Console.WriteLine("Input file does not exist.");
                return(1);
            }

            var outFilePath = args[2];

            try
            {
                Path.GetFileName(outFilePath);
            }
            catch
            {
                Console.WriteLine("Invalid output file name.");
                return(1);
            }

            if (File.Exists(outFilePath))
            {
                Console.WriteLine("Output file already exists. Specify a new file name.");
                return(1);
            }

            switch (args[0])
            {
            case "compress":
                var compressor = new Compressor(inputFilePath, FilePartitioner.GetRecommendedChunkSize(inputFilePath), outFilePath);
                try
                {
                    compressor.Compress();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Console.WriteLine($"Unexpected error occured: {e.Message}.");
                    return(1);
                }
                break;

            case "decompress":
                var decompressor = new Decompressor(inputFilePath, outFilePath);
                try
                {
                    decompressor.Decompress();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Console.WriteLine($"Unexpected error occured: {e.Message}.");
                    return(1);
                }

                break;

            default:
                Console.WriteLine("Cannot parse arguments.");
                Console.WriteLine(Usage);
                return(1);
            }

            return(0);
        }