string SendFile(FileInfo file, SendingOptions sendingOptions, Logger logger, ArchiveOptions archiveOptions) { string targetDirectory = $"{sendingOptions.TargetDirectory}\\{file.LastWriteTime:yyyy\\\\MM\\\\dd}"; Validator.CreateDirectoryIfNotExist(targetDirectory); string newName = $"{Path.GetFileNameWithoutExtension(file.FullName)}_{file.LastWriteTime:yyyy_MM_dd_HH_mm_ss}"; int i = 0; while (File.Exists($"{targetDirectory}\\{newName}.txt")) { i++; newName = $"{Path.GetFileNameWithoutExtension(file.FullName)}({i})_{file.LastWriteTime:yyyy_MM_dd_HH_mm_ss}"; } string newPath = $"{targetDirectory}\\{newName}"; Archive.Compress(file.FullName, newPath + ".gz", archiveOptions); if (sendingOptions.EnableArchiveDirectory) { Validator.CreateDirectoryIfNotExist(sendingOptions.ArchiveDirectory); Archive.Compress(file.FullName, $"{sendingOptions.ArchiveDirectory}\\{newName}.gz", archiveOptions); } Archive.Decompress(newPath + ".gz", newPath + ".txt"); File.Delete(file.FullName); File.Delete(newPath + ".gz"); logger.Log($"File {newName} sent successfully"); return(newPath + ".txt"); }
public ETLOptions(SendingOptions sendingOptions, EncryptionOptions encryptionOptions, LoggingOptions loggingOptions, ArchiveOptions archiveOptions) { SendingOptions = sendingOptions; EncryptionOptions = encryptionOptions; LoggingOptions = loggingOptions; ArchiveOptions = archiveOptions; }
public static void Compress(string source, string target, ArchiveOptions archiveOptions) { using (FileStream ss = new FileStream(source, FileMode.Open)) { using (FileStream ts = File.Create(target)) { using (GZipStream zs = new GZipStream(ts, archiveOptions.CompressionLevel)) { ss.CopyTo(zs); } } } }
public static string Validate(ETLOptions options) { string report = ""; #region Sending Validation SendingOptions sending = options.SendingOptions; if (!CreateDirectoryIfNotExist(sending.SourceDirectory)) { sending.SourceDirectory = "C:\\FileWatcher\\source"; CreateDirectoryIfNotExist(sending.SourceDirectory); report += "Cannot open source directory, using default. "; } if (!CreateDirectoryIfNotExist(sending.TargetDirectory)) { sending.TargetDirectory = "C:\\FileWatcher\\target"; report += "Cannot open target directory, using default. "; CreateDirectoryIfNotExist(sending.TargetDirectory); } if (!CreateDirectoryIfNotExist(sending.ArchiveDirectory)) { sending.ArchiveDirectory = "C:\\FileWatcher\\target\\archive"; report += "Cannot open source archive, using default. "; CreateDirectoryIfNotExist(sending.ArchiveDirectory); } #endregion #region Logging Validation LoggingOptions logging = options.LoggingOptions; if (!CreateFileIfNotExist(logging.LogPath)) { logging.LogPath = "C:\\FileWatcher\\target\\log.txt"; report += "Cannot open source log file, using default. "; CreateFileIfNotExist(logging.LogPath); } #endregion #region Encryption Validation EncryptionOptions encryption = options.EncryptionOptions; if (!encryption.RandomKey && encryption.Key.Length != 16) { report += "Encryption key's length must be 16, using random key. "; } #endregion #region Archive Validation ArchiveOptions archive = options.ArchiveOptions; if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2) { archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; report += "Compression level value can't be below zero and abowe 2, using default value. "; } #endregion return(report); }