using System; using System.IO; using System.IO.Compression; using System.Configuration; namespace BackupFile { class AppConfig { public string BackupPath { get; } public string ApplicationPath { get; } public AppConfig() { BackupPath = ConfigurationManager.AppSettings["backupPath"]; ApplicationPath = ConfigurationManager.AppSettings["application_path"]; if (string.IsNullOrEmpty(BackupPath) || string.IsNullOrEmpty(ApplicationPath)) { throw new Exception("Please set backupPath and applicationPath in the app.config file."); } } } class Compressor { private readonly string _applicationPath; private readonly string _destinationFolder; public Compressor(string applicationPath, string destinationFolder) { _applicationPath = applicationPath; _destinationFolder = destinationFolder; } public void Compress(string target) { string sourcePath = Path.Combine(_applicationPath, target); string destinationPath = Path.Combine(_destinationFolder, target + ".zip"); if (File.Exists(sourcePath)) { Console.WriteLine($"Compressing file: {target}"); using FileStream fs = new FileStream(destinationPath, FileMode.Create); using ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Create); archive.CreateEntryFromFile(sourcePath, Path.GetFileName(sourcePath)); } else if (Directory.Exists(sourcePath)) { Console.WriteLine($"Compressing folder: {target}"); ZipFile.CreateFromDirectory(sourcePath, destinationPath); } else { Console.WriteLine($"Target does not exist: {target}"); } } } class BackupManager { private readonly AppConfig _config; private string _ticketNumber; private string _folderPath; public BackupManager(AppConfig config) { _config = config; } public void Run() { Console.WriteLine("backupPath: " + _config.BackupPath); Console.WriteLine("applicationPath: " + _config.ApplicationPath); Console.WriteLine("What is the ticket number?"); _ticketNumber = Console.ReadLine(); _folderPath = Path.Combine(_config.BackupPath, _ticketNumber); if (!Directory.Exists(_folderPath)) { Directory.CreateDirectory(_folderPath); } var compressor = new Compressor(_config.ApplicationPath, _folderPath); compressor.Compress("api-gw"); compressor.Compress("e5.exe"); compressor.Compress("nothing"); } } class Program { static void Mains(string[] args) { try { var config = new AppConfig(); var manager = new BackupManager(config); manager.Run(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Environment.Exit(1); } } } }