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 void Validate(object obj) { string report = ""; ETLOptions options = obj as ETLOptions; #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 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 Report = report; }
public void Validate(object obj) { string report = ""; ETLOptions options = obj as ETLOptions; #region Moving Validation MovingOptions moving = options.MovingOptions; if (!CreateDirectoryIfNotExist(moving.SourceDirectory)) { report += "Cannot open source directory, using default. "; moving.SourceDirectory = @"C:\Users\Public\Desktop\SourceFolder"; CreateDirectoryIfNotExist(moving.SourceDirectory); } if (!CreateDirectoryIfNotExist(moving.TargetDirectory)) { report += "Cannot open target directory, using default. "; moving.TargetDirectory = @"C:\Users\Public\Desktop\TargetFolder"; CreateDirectoryIfNotExist(moving.TargetDirectory); } #endregion #region Archive Validation ArchiveOptions archive = options.ArchiveOptions; if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2) { report += "Compression level value can't be below 0 or above 2, using default value. "; archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; } #endregion #region Watcher Validation WatcherOptions watcher = options.WatcherOptions; if (watcher.Filter != "*.txt") { report += "Filter can be only \"*.txt\", using default value. "; watcher.Filter = "*.txt"; } #endregion Report = report; }
public Watcher(ETLOptions options, Logger.ILogger logger, string report) { MovingOptions movingOptions = options.MovingOptions; WatcherOptions watcherOptions = options.WatcherOptions; ArchiveOptions archiveOptions = options.ArchiveOptions; this.sourceDir = movingOptions.SourceDirectory; this.targetDir = movingOptions.TargetDirectory; Directory.CreateDirectory(sourceDir); Directory.CreateDirectory(targetDir); fileSystemWatcher = new FileSystemWatcher(sourceDir); fileSystemWatcher.Filter = watcherOptions.Filter; fileSystemWatcher.Created += OnCreated; enableLogging = movingOptions.EnableLogging; enableArchiveDirectory = movingOptions.EnableArchiveDirectory; compressionLevel = archiveOptions.CompressionLevel; this.report = report; dbLogger = logger; }
string SendFile(FileInfo file, SendingOptions sendingOptions, ILogger 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(WatcherOptions watcherOptions, MovingOptions movingOptions, ArchiveOptions archiveOptions) { WatcherOptions = watcherOptions; MovingOptions = movingOptions; ArchiveOptions = archiveOptions; }