public static void Combine(string zipPath, string resultPath, int maxDepth, string issuer, string comment) { if (!File.Exists(zipPath)) throw new ArgumentException("Source directory not found."); if (!Directory.Exists(Path.GetDirectoryName(Path.GetFullPath(resultPath)))) throw new ArgumentException("Destination directory not found."); if (File.Exists(resultPath)) throw new ArgumentException("Destination file already exists."); var backupStatus = new BackupStatus(); backupStatus.ReadFiles(zipPath, true, maxDepth); var groups = from x in backupStatus.Root.Children group x by x.ArchivePath into archives select archives; using (var file = ZipFile.Open(resultPath, ZipArchiveMode.Create)) { var informationFile = file.CreateEntry("info.xml"); using (var stream = informationFile.Open()) { var information = new BackupInformation { DeletedFiles = backupStatus.LastDeletedFiles, ParentName = backupStatus.Root.Information.ParentName, CreationDate = DateTime.Now, Issuer = issuer, Comment = comment }; information.Save(stream); } foreach (var group in groups) { using (var zipFile = ZipFile.Open(group.Key, ZipArchiveMode.Read)) { foreach (var backupFile in group) { var relativeName = "data" + backupFile.VirtualPath.Substring(1); var entry = file.CreateEntry(relativeName + "." + backupFile.FileHash); using (var stream = entry.Open()) { using (var fileStream = zipFile.GetEntry(entry.FullName).Open()) { fileStream.CopyTo(stream); } } } } } } }
private void SaveBackup(string fileName, string parentHash, IEnumerable<BackupFile> removedFiles, IEnumerable<BackupFile> addedFiles, string comment = null, string issuer = null) { using (var file = ZipFile.Open(fileName, ZipArchiveMode.Create)) { var informationFile = file.CreateEntry("info.xml"); using (var stream = informationFile.Open ()) { var information = new BackupInformation { DeletedFiles = new HashSet<string>( removedFiles.Select(a => a.VirtualPath)), ParentName = parentHash, CreationDate = DateTime.Now, Comment = comment, Issuer = issuer }; information.Save(stream); } foreach (var backupFile in addedFiles) { var relativeName = "data" + backupFile.VirtualPath.Substring(1); var entry = file.CreateEntry(relativeName + "." + backupFile.FileHash); using (var stream = entry.Open ()) { using ( var fileStream = new FileStream(Path.Combine(WorkingDirectoryPath, backupFile.VirtualPath.Substring(2)), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fileStream.CopyTo(stream); } } } } }