public string CreateZip(OnZipChanged callback = null) { var targetFile = RandomFilename(); logger.Debug($"Создан временный файл: {targetFile}"); logger.Debug($"Файлов подходит по маске: {files.Count}"); WriteToFile(targetFile, pathToDirectory, files, callback); filesToDelete.Add(targetFile); logger.Info($"Архив успешно создан: {targetFile}"); if (password == null) { return(targetFile); } var encryptedFile = RandomFilename(); EncryptFile(targetFile, encryptedFile, password, callback); filesToDelete.Add(encryptedFile); return(encryptedFile); }
protected void WriteToFile(string targetFile, string pathToDirectory, List <string> files, OnZipChanged callback) { FileStream fsZip = null; FileStream fsInput = null; ZipArchive zip = null; Stream entryStream = null; try { fsZip = new FileStream(targetFile, FileMode.Create); zip = new ZipArchive(fsZip, ZipArchiveMode.Create); int total = files.Count; int index = 0; foreach (var path in files) { var filename = FileUtils.GetRelativePath(pathToDirectory, path); logger.Trace($"Архивируем файл: " + filename); callback?.Invoke(total, index, filename); var entry = zip.CreateEntry(filename); entryStream = entry.Open(); fsInput = new FileStream(path, FileMode.Open); fsInput.CopyTo(entryStream); fsInput.Dispose(); entryStream.Dispose(); index++; } } finally { zip?.Dispose(); fsZip?.Dispose(); fsInput?.Dispose(); entryStream?.Dispose(); } }
// Из-за отсутствия шифрования имён файлов в DotNetZip и во избежание проблем совместимости // зашифрованный Zip архив без сжатия через DotNetZip кладётся поверх обычного Zip архива, созданного средствами NET 4.5 protected void EncryptFile(string fileInput, string fileOutput, string password, OnZipChanged callback) { using (var zip = new ZipFile(fileOutput)) { callback(1, 1, "шифрование"); zip.CompressionLevel = CompressionLevel.None; zip.Encryption = EncryptionAlgorithm.WinZipAes256; zip.Password = password; zip.AddFile(fileInput, ""); zip.Save(); } }