private static int Main(string[] args) { if (args.Length != 3) { return(HandleArgumentsError()); } var s = args[0].Trim(); ParallelFileProcessor fileProcessor; if (string.Equals(s, "decompress", StringComparison.OrdinalIgnoreCase)) { fileProcessor = new GZipDecompressor(); } else if (string.Equals(s, "compress", StringComparison.OrdinalIgnoreCase)) { fileProcessor = new GZipCompressor(); } else { return(HandleArgumentsError()); } try { fileProcessor.Run(args[1], args[2]); } catch (Exception e) { Console.WriteLine($"Произошла ошибка {e.Message}"); Console.WriteLine("Для продолжения нажмите Enter"); Console.ReadLine(); return(1); } return(0); }
static void Main(string[] args) { if (args != null && args.Length > 0) { CompressionProcessor processor; try { switch (args[0]) { case "compress": processor = new GZipCompressor(); break; case "decompress": processor = new GZipDecompressor(); break; default: throw new UnrecognizedCommandException(args[0]); } if (args.Length != 3) { throw new UnrecognizedCommandException($"Invalid command arguments.", args[0]); } if (!File.Exists(args[1])) { throw new FileNotFoundException("File not found!", args[1]); } if (File.Exists(args[2]) && !AskYesNo($"File {args[2]} is exists. Overwrite?")) { return; } processor.ProgressChanged += Processor_ProgressChanged; processor.ProcessingFinished += Processor_ProcessingFinished; processor.Run(() => File.Open(args[1], FileMode.Open, FileAccess.Read), () => File.Open(args[2], FileMode.Create, FileAccess.Write), 1 << 20); Console.WriteLine("Press [ESC] to cancel process.."); while (processor.IsRunning && Console.ReadKey(true).Key != ConsoleKey.Escape) { Thread.Sleep(10); } if (processor.IsRunning) { processor.Cancel(); } } catch (UnrecognizedCommandException ex) { PrintError(ex.Message); PrintHelp(); } catch (Exception ex) { PrintError(ex.Message); } } else { PrintHelp(); } }