There is a method that implements file compression with the creation of an archive at the specified path:
public int Action(string typeJob,string fileInput, string fileOut) { if (FileExistCheck(fileInput, fileOut)) { using (FileStream sourceStream = new FileStream(fileInput, FileMode.Open)) { using (FileStream targetStream = File.Create(fileOut)) { switch (typeJob) { case "compress": using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress)) { sourceStream.CopyTo(compressionStream); return 0; } case "decompress": using (GZipStream decompressionStream = new GZipStream(sourceStream, CompressionMode.Decompress)) { try { decompressionStream.CopyTo(targetStream); return 0; } catch (InvalidDataException) { Console.WriteLine("Возможно путь к архиву указывает на файл иного типа"); File.Delete(fileOut); return 1; } } default: Console.WriteLine("Первый аргумент указан неверно"); Console.WriteLine("Следует выбрать compress или decompress"); File.Delete(fileOut); return 1; } } } } else return 1; } How can you stop this process by calling any method by pressing a key / key combination? I saw a lot of information about how to stop the eternal cycle, but I did not find about FileStream .