Exemplo n.º 1
0
        public static int Main(string[] args)
        {
            //args = new string[3];
            //args[0] = @"compress";
            //args[1] = @"2.txt";
            //args[2] = @"2.gz";

            using (var mutex = new Mutex(false, "GZipTest|Cryoland"))
            {
                if (!mutex.WaitOne(TimeSpan.FromSeconds(3), false))
                {
                    Console.WriteLine("Another application instance is running. Terminating..");
                    Thread.Sleep(1000);
                    return(1);
                }

                Console.CancelKeyPress += new ConsoleCancelEventHandler((s, e) =>
                {
                    if (e.SpecialKey == ConsoleSpecialKey.ControlC)
                    {
                        Console.WriteLine("\nInterruption..");
                        e.Cancel = true;
                        gzip?.Interrupt();
                    }
                });

                try
                {
                    ArgsValidator.Check(args);

                    if (args[0].Equals($"compress"))
                    {
                        gzip = new GZipCompressor(args[1], args[2]);
                    }

                    if (args[0].Equals($"decompress"))
                    {
                        gzip = new GZipDecompressor(args[1], args[2]);
                    }

                    gzip.Run();
                    return(gzip.State());
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                    return(1);
                }
            }
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            ArgsValidator argsValidator = new ArgsValidator();
            Logger        logger        = LogManager.GetCurrentClassLogger();

            try
            {
                if (argsValidator.IsArgsValid(args, out string validationMessage))
                {
                    string      actionType = args[0], inFile = args[1], outFile = args[2];
                    int         chunkSize   = Environment.SystemPageSize * 1024;
                    GZipManager gzipManager = new GZipManager(inFile, new FileChunkReader(inFile), new FileChunkWriter(outFile),
                                                              new FileSplitterFactory(), new CompressorFactory(), new TaskFactory(), new ErrorLogs());
                    Stopwatch stopWatch = new Stopwatch();

                    stopWatch.Start();
                    bool isOpSuccess = gzipManager.Execute(actionType, Environment.ProcessorCount, chunkSize, out List <Exception> exceptions);
                    stopWatch.Stop();

                    if (isOpSuccess)
                    {
                        IOManager.OpSuccess(stopWatch.Elapsed.TotalSeconds);
                        return(0);
                    }

                    foreach (Exception ex in exceptions)
                    {
                        logger.Error(ex);
                    }
                    IOManager.OpError(exceptions);

                    return(1);
                }

                IOManager.ValidationError(validationMessage);
                return(1);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                IOManager.OpError(ex);

                return(1);
            }
        }