public GZipProcessManager(string pathFromName, string pathToName, GZipOperation operation)
        {
            this.pathFrom      = pathFromName;
            this.pathTo        = pathToName;
            this.allWorkers    = new List <IWorker>();
            this.producerQueue = new BlockingQueue();

            if (operation == GZipOperation.Compress)
            {
                this.consumerCollection = new BlockingQueue();
                this.blockGZipper       = new BlockGZipCompressor();
                var fileReader = new SimpleFileFactory(pathFrom, BLOCK_SIZE).GetFileReader();
                this.blocksProducer = new BlocksProducer(fileReader,
                                                         this.producerQueue);
                this.blocksConsumer = new CompressBlocksConsumer(
                    new CompressedFileFactory(pathTo, fileReader.NumberOfBlocks).GetFileWriter(),
                    this.consumerCollection);
            }
            else
            {
                this.blockGZipper       = new BlockGZipDecompressor();
                this.consumerCollection = new BlockingDictionary();
                this.blocksProducer     = new BlocksProducer(
                    new CompressedFileFactory(pathFrom).GetFileReader(),
                    this.producerQueue);
                this.blocksConsumer = new DecompressBlocksConsumer(
                    new SimpleFileFactory(pathTo).GetFileWriter(),
                    this.consumerCollection);
            }

            this.End = new AutoResetEvent(false);
        }
Exemple #2
0
        static bool CheckExtensions(string pathFrom, string pathTo, GZipOperation operation)
        {
            FileInfo fileFrom = new FileInfo(pathFrom);
            FileInfo fileTo   = new FileInfo(pathTo);

            if (operation == GZipOperation.Compress)
            {
                if (fileFrom.Extension != ".gz" && fileTo.Extension == ".gz")
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (fileFrom.Extension == ".gz" && fileTo.Extension != ".gz")
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemple #3
0
        static int Main(string[] args)
        {
            logger.Info("Hello!");
            string        pathFrom  = "";
            string        pathTo    = "";
            GZipOperation operation = 0;

            string[]        instructions   = args;
            IProcessManager processManager = null;

            if (instructions.Length == 3 &&
                (instructions[0] == "compress" || instructions[0] == "decompress"))
            {
                if (instructions[0] == "compress")
                {
                    operation = GZipOperation.Compress;
                }
                else
                {
                    operation = GZipOperation.Decompress;
                }
                pathFrom = instructions[1];
                pathTo   = instructions[2];
            }
            else
            {
                logger.Error("Wrong input file format");
                return(-1);
            }
            if (CheckExtensions(pathFrom, pathTo, operation))
            {
                try
                {
                    processManager = new GZipProcessManager(pathFrom, pathTo, operation);
                    logger.Info("Process started");
                    processManager.StartProcess().WaitOne();
                    if (processManager.Exception == null)
                    {
                        logger.Info("Successful");
                        return(0);
                    }
                    else
                    {
                        throw processManager.Exception;
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                    return(-1);
                }
            }
            else
            {
                logger.Error("Wrong input/output file format");
                return(-1);
            }
        }
        private void Decompress(string source, string target)
        {
            try
            {
                using var sourceStream = File.OpenRead(source);
                using var targetStream = File.Open(target, FileMode.Create);

                var operation = new GZipOperation(new DecompressorDataTransform());
                var processor = new DecompressApplicationProcessor(operation);

                processor.Process(sourceStream, targetStream);
            }
            catch (Exception e)
            {
                _logger.Exception(e);
            }
        }