// создание файлов private void Watcher_Created(object sender, FileSystemEventArgs e) { string fileEvent = "created"; string filePath = e.FullPath; string path = filePath; string compressedFile = Path.ChangeExtension(path, "gz"); string data; byte[] dataProcesed; byte[] dataDeProcesed; string deData; if (filePath.EndsWith(".txt") || filePath.EndsWith(".xml")) { if (filePath.EndsWith(".xml")) { flagXml = 1; } //try //{ try { StreamReader sr = new StreamReader(path); data = sr.ReadToEnd(); sr.Close(); } catch (Exception exc) { return; } if (data.Length != 0) { if (flagEnc) { dataProcesed = Crypto.EncryptStringToBytes_Aes(data, key, iv); using (FileStream fstream = new FileStream(path, FileMode.OpenOrCreate)) { fstream.Write(dataProcesed, 0, dataProcesed.Length); } } } if (arcFlag) { string res = Archive.MyArchive(path, compressedFile); string PreviousSize = res.Split(' ')[0]; string compressedSize = res.Split(' ')[1]; RecordEntryForAction(String.Format( "Compression of file {0} is comleted. Previous size: {1} compressed size: {2}.", path, PreviousSize, compressedSize)); } //} //catch (Exception ee) //{ // RecordEntryForAction(ee.Message); //} } else { if (filePath.EndsWith(".gz")) { //try //{ string[] paths = { targetPathFromStorage, Path.GetFileName(compressedFile) }; string targetPath = Path.Combine(paths); Console.WriteLine(targetPath); // Ensure that the target does not exist. if (File.Exists(targetPath)) { File.Delete(targetPath); } // Move the file. File.Move(compressedFile, targetPath); RecordEntryForAction(String.Format("{0} was moved to {1}.", compressedFile, targetPath)); string targetPathDecompressed; //decompressed if (flagXml == 1) { targetPathDecompressed = Path.ChangeExtension(targetPath, "xml"); } else { targetPathDecompressed = Path.ChangeExtension(targetPath, "txt"); } //class if (DeArcFlag || compressFlag) { Dearchive.DeArchieves(targetPath, targetPathDecompressed); flagXml = 0; RecordEntryForAction(String.Format("File recovered: {0}", targetPathDecompressed)); } string[] pathsDir = { targetPathFromStorage, @"Archieve/", Convert.ToString(DateTime.Now.Year) }; string a = targetPathFromStorage + @"Archieve/" + Convert.ToString(DateTime.Now.Year) + @"/" + Convert.ToString(DateTime.Now.Month) + @"/" + Convert.ToString(Convert.ToString(DateTime.Now.Date).Split(' ')[0]) + @"/" + Convert.ToString(DateTime.Now.Hour) + @"/" + Convert.ToString(DateTime.Now.Minute) + @"/" + Convert.ToString(DateTime.Now.Second) + @"/" + Convert.ToString(DateTime.Now.Millisecond); string pathDir = Path.Combine(pathsDir); Console.WriteLine(a); DirectoryInfo di = Directory.CreateDirectory(a); string[] paths1 = { a, Path.GetFileName(targetPath) }; string targetPathArchive = Path.Combine(paths1); // Ensure that the target does not exist. if (File.Exists(targetPathArchive)) { File.Delete(targetPathArchive); } File.Move(targetPath, targetPathArchive); RecordEntryForAction(String.Format("{0} was moved to {1}.", targetPath, targetPathArchive)); using (FileStream sr = File.OpenRead(targetPathDecompressed)) { dataDeProcesed = new byte[sr.Length]; sr.Read(dataDeProcesed, 0, dataDeProcesed.Length); } if (flagDecrip) { deData = Decrypto.DecryptStringFromBytes_Aes(dataDeProcesed, key, iv); using (StreamWriter sw = new StreamWriter(targetPathDecompressed, false, System.Text.Encoding.Default)) { sw.Write(deData); } } //} //catch (Exception ee) //{ // RecordEntryForAction(ee.Message); //} } } RecordEntry(fileEvent, filePath); }
private void OnCreated(object sender, FileSystemEventArgs e) { string path = e.FullPath; try { FileInfo fileInfo = new FileInfo(path); DateTime dateTime = fileInfo.LastWriteTime; string year = dateTime.ToString("yyyy"); string month = dateTime.ToString("MM"); string day = dateTime.ToString("dd"); byte[] key, iv; (key, iv) = Encryptor.CreateKeyAndIV(); string name = Path.GetFileNameWithoutExtension(path); string extension = Path.GetExtension(path); string fileName = Path.GetFileName(path); string gzPath = Path.Combine(targetDir, name + ".gz"); string newPath = Path.Combine(targetDir, name + extension); //encrypting byte [] content = File.ReadAllBytes(path); content = Encryptor.Encrypt(content, key, iv); File.WriteAllBytes(path, content); //compressing and copying to targetDir Archive.Compress(path, gzPath); if (File.Exists(path)) { File.Delete(path); } if (enableArchiveDirectory) { //copying to additional archive char sep = Path.DirectorySeparatorChar; string archiveDir = Path.Combine(targetDir, $"archive{sep}year {year}{sep}month {month}{sep}day {day}"); string archiveName = name + '_' + dateTime.ToString("yyyy_MM_dd_HH_mm_ss") + ".gz"; string archivePath = Path.Combine(archiveDir, archiveName); Directory.CreateDirectory(archiveDir); File.Copy(gzPath, archivePath); } //decompressing Archive.Decompress(gzPath, newPath); if (File.Exists(gzPath)) { File.Delete(gzPath); } //decrypting content = File.ReadAllBytes(newPath); string decrypted = Encryptor.Decrypt(content, key, iv); File.WriteAllText(newPath, decrypted); report += $"File \"{name}\" is moved from \"{sourceDir}\" to \"{targetDir}\" successfully!"; dbLogger.Log(report); } catch (Exception exception) { report += $"A following problem occured: {exception}"; dbLogger.Log(report); } }