public bool RestoreBackup(string path, DirectoryInfo destination, BackgroundWorker worker) { StringBuilder summary = new StringBuilder(); FileInfo tempFile = Decompress(path, worker); if (tempFile == null) { return false; } int itemCount; header = new byte[8]; try { using (FileStream reader = new FileStream(tempFile.FullName, FileMode.Open, FileAccess.Read)) { reader.Read(header, 0, 8); itemCount = BitConverter.ToInt32(header, 0); manifest = new byte[BitConverter.ToInt32(header, 4)]; reader.Read(manifest, 0, manifest.Length); for (int i = 0; i < itemCount; i++) { BackupFile f = new BackupFile(); summary.AppendLine(f.restore(reader, destination)); worker.ReportProgress((int)(((double)i / (double)itemCount) * 100)); } reader.Close(); summary.AppendLine(); summary.AppendLine("Number of Files: " + itemCount); summary.AppendFormat("Size of all restored Files: {0} KB", (tempFile.Length / 1024)); } tempFile.Delete(); } catch (Exception ex) { Logger.Log("Error while restoring complete Backup. Message: " + ex.Message, Logger.Level.ERROR); return false; } RestoreController ctrl = RestoreController.getInstance(); ctrl.Summary = summary.ToString(); return true; }
private void BuildByteFiles(List<BackupFile> byteFiles, List<FileInfo> toRemove) { foreach (FileInfo file in FilesToBackup) { BackupFile f = new BackupFile(); if (f.BuildFile(file)) { byteFiles.Add(f); } else { toRemove.Add(file); } } foreach (FileInfo file in toRemove) { FilesToBackup.Remove(file); } }
internal bool RestoreSelectedFiles(FileInfo BackupFile, DirectoryInfo RestoreDestination, Dictionary<FileInfo, long> SelectedFiles, BackgroundWorker worker) { //Create Dictorary which holds the offset for every File Dictionary<FileInfo, long> index = createIndex(BackupFile.FullName); Dictionary<string, long> offsetIndex = new Dictionary<string, long>(); StringBuilder summary = new StringBuilder(); long offset = 0; foreach (KeyValuePair<FileInfo, long> item in index) { offsetIndex.Add(item.Key.Name, offset); offset += item.Value; } FileInfo tempFile = Decompress(BackupFile.FullName, worker); if (tempFile == null) { return false; } header = new byte[8]; try { using (FileStream reader = new FileStream(tempFile.FullName, FileMode.Open, FileAccess.Read)) { //Read Header and Manifest reader.Read(header, 0, 8); manifest = new byte[BitConverter.ToInt32(header, 4)]; reader.Read(manifest, 0, manifest.Length); //Read and Restore each selected File int it = 0; foreach (KeyValuePair<FileInfo, long> file in SelectedFiles) { long pos = offsetIndex[file.Key.Name]; reader.Seek((header.Length + manifest.Length + pos), SeekOrigin.Begin); BackupFile f = new BackupFile(); f.restore(reader, RestoreDestination); summary.AppendLine(file.Key.FullName); worker.ReportProgress((int)(100 * ((double)it / (double)SelectedFiles.Count))); it++; } reader.Close(); summary.AppendLine(); summary.AppendFormat("Number of restored Files: {0}", SelectedFiles.Count); } tempFile.Delete(); RestoreController ctrl = RestoreController.getInstance(); ctrl.Summary = summary.ToString(); } catch (Exception ex) { Logger.Log("Error while restoring selected Files. Message: " + ex.Message, Logger.Level.ERROR); return false; } return true; }