private void AddBooks(IEnumerable <FileInfo> files, ZipArchive archive, BackupMeta backup, ref int counter) { foreach (var fileInfo in files) { var book = new VocabularyBook(); if (VocabularyFile.ReadVhfFile(fileInfo.FullName, book)) { var vhr = VocabularyFile.ReadVhrFile(book); var success = TryAddFile(fileInfo.FullName, archive, $"vhf\\{counter}.vhf"); var success2 = false; if (success && vhr && CbSaveResults.Checked) { success2 = TryAddFile(Path.Combine(Settings.Default.VhrPath, book.VhrCode + ".vhr"), archive, $"vhr\\{book.VhrCode}.vhr"); } if (success) { backup.Books.Add(new BackupMeta.BookMeta(counter, BackupMeta.ShrinkPath(fileInfo.FullName), success2 ? book.VhrCode : "")); counter++; if (success2) { backup.Results.Add(book.VhrCode + ".vhr"); } } } } }
private void ExecuteBackup(string path, bool allBooks, IEnumerable <FileInfo> additionalFiles) { Cursor.Current = Cursors.WaitCursor; using (var saveStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) using (var archive = new ZipArchive(saveStream, ZipArchiveMode.Create)) { var backup = new BackupMeta(); var booksInfo = new DirectoryInfo(Settings.Default.VhfPath); var counter = 0; if (allBooks) { AddBooks(booksInfo.EnumerateFiles("*.vhf", SearchOption.AllDirectories), archive, backup, ref counter); } additionalFiles = additionalFiles .Where(info => !allBooks || !info.FullName.StartsWith(Settings.Default.VhfPath, StringComparison.OrdinalIgnoreCase)); AddBooks(additionalFiles, archive, backup, ref counter); AddSpecialChars(ListSpecialChars.CheckedItems.Cast <string>(), archive, backup); backup.Write(archive); } Cursor.Current = Cursors.Default; }
private void Form_Shown(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; ListBooks.BeginUpdate(); ListSpecialChars.BeginUpdate(); if (TryOpen(path, out archive) && BackupMeta.TryRead(archive, out meta)) { foreach (var book in meta.Books) { var fullName = BackupMeta.ExpandPath(book.VhfPath); ListBooks.Items.Add(CbAbsolutePath.Checked ? fullName : Path.GetFileNameWithoutExtension(fullName), true); } foreach (var specialChar in meta.SpecialChars) { ListSpecialChars.Items.Add(specialChar, true); } } else { DialogResult = DialogResult.Abort; Close(); } ListBooks.EndUpdate(); ListSpecialChars.EndUpdate(); Cursor.Current = Cursors.Default; }
private void CbAbsolutePath_CheckedChanged(object sender, EventArgs e) { for (var i = 0; i < meta.Books.Count; i++) { var fullName = BackupMeta.ExpandPath(meta.Books[i].VhfPath); ListBooks.Items[i] = CbAbsolutePath.Checked ? fullName : Path.GetFileNameWithoutExtension(fullName); } }
private void AddSpecialChars(IEnumerable <string> files, ZipArchive archive, BackupMeta backup) { foreach (var fileInfo in files.Select(path => new FileInfo(Path.Combine(AppInfo.SpecialCharDirectory, path + ".txt")))) { if (TryAddFile(fileInfo.FullName, archive, "chars\\" + fileInfo.Name)) { backup.SpecialChars.Add(fileInfo.Name); } } }
private void BtnRestore_Click(object sender, EventArgs e) { int restored = 0, skipped = 0, failed = 0; void stats(RestoreResult result) { switch (result) { case RestoreResult.Success: restored++; break; case RestoreResult.Skipped: skipped++; break; case RestoreResult.Error: failed++; break; } } for (var i = 0; i < meta.Books.Count; i++) { if (!ListBooks.GetItemChecked(i)) { continue; } var item = meta.Books[i]; var destination = new FileInfo(BackupMeta.ExpandPath(item.VhfPath)); var result = Restore(archive, $"vhf\\{item.FileId}.vhf", destination, GetOverrideMode()); stats(result); if (result == RestoreResult.Success && RbRestoreAssociatedResults.Checked && !string.IsNullOrWhiteSpace(item.VhrCode)) { var resultDestination = new FileInfo(Path.Combine(Settings.Default.VhrPath, item.VhrCode)); stats(Restore(archive, $"vhr\\{item.VhrCode}.vhr", resultDestination, GetOverrideMode())); } } if (RbRestoreAllResults.Checked) { for (var i = 0; i < meta.Results.Count; i++) { var destination = new FileInfo(Path.Combine(Settings.Default.VhrPath, meta.Results[i])); stats(Restore(archive, "vhr\\" + meta.Results[i], destination, GetOverrideMode())); } } for (var i = 0; i < meta.SpecialChars.Count; i++) { if (!ListSpecialChars.GetItemChecked(i)) { continue; } var destination = new FileInfo(Path.Combine(AppInfo.SpecialCharDirectory, meta.SpecialChars[i])); stats(Restore(archive, "chars\\" + meta.SpecialChars[i], destination, GetOverrideMode())); } MessageBox.Show(string.Format(Messages.VdpRestored, restored, skipped, failed), Messages.VdpRestoredT, MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); }