public static void Compress(string sourceFile, string compressedFile) { try { string encrFileName = Crypt.EncryptedFilePath(sourceFile, "D:\\Target\\crypt"); using (FileStream sourceStream = new FileStream(sourceFile, FileMode.OpenOrCreate)) { using (FileStream targetStream = File.Create(compressedFile)) { using (FileStream encryptStream = File.Create(encrFileName)) { Crypt.Encrypt(sourceStream, encryptStream); using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress)) { using (FileStream finalStream = new FileStream(encrFileName, FileMode.OpenOrCreate)) { finalStream.CopyTo(compressionStream); } } } } } } catch (Exception ex) { using (StreamWriter writer = new StreamWriter("D:\\Target\\sourcelog.txt", true)) { writer.WriteLine("Ошибка архивации данных:{0}", ex.Message); writer.Flush(); } } }
public static void Compress(string sourceFile, string compressedFile, bool needToEncrypt) { try { string encrFileName = Crypt.EncryptedFilePath(sourceFile, Path.Combine(Path.GetDirectoryName(compressedFile), "crypt")); using (FileStream sourceStream = new FileStream(sourceFile, FileMode.OpenOrCreate)) { using (FileStream targetStream = File.Create(compressedFile)) { using (FileStream encryptStream = File.Create(encrFileName)) { if (needToEncrypt) { Crypt.Encrypt(sourceStream, encryptStream); } else { sourceStream.CopyTo(encryptStream); } using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress)) { using (FileStream finalStream = new FileStream(encrFileName, FileMode.OpenOrCreate)) { finalStream.CopyTo(compressionStream); } } } } } } catch (Exception ex) { using (StreamWriter writer = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sourcelog.txt"), true)) { writer.WriteLine("Ошибка архивации данных:{0}", ex.Message); writer.Flush(); } } }