我使用MS Access作为数据库在C#中备份和恢复代码。我完成了zip格式的备份,现在我想恢复Zipped文件。任何帮助将不胜感激。
public void BackupDatabase(string dateToday)
{
string dbFileName = "dbCPS.accdb";
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory , dbFileName);
string backTimeStamp = Path.GetFileNameWithoutExtension(dbFileName) + "_" + dateToday + ".zip";// +Path.GetExtension(dbFileName);
string destFileName = backTimeStamp;// +dbFileName;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
string PathtobackUp = fbd.SelectedPath.ToString();
destFileName = Path.Combine(PathtobackUp, destFileName);
//File.Copy(CurrentDatabasePath, destFileName, true);
using (var zip = new ZipFile())
{
zip.AddFile(dbFileName);
zip.Save(destFileName);
}
MessageBox.Show("Backup successful! ");
}
}
private void backupToolStripMenuItem1_Click(object sender, EventArgs e)
{
BackupDatabase(DateTime.Now.ToString("ddMMMyyyy_HH.mm"));
}
public void RestoreDatabase(string restoreFile)
{
string dbFileName = "dbCPS.accdb";
string pathBackup = restoreFile;
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
File.Copy(pathBackup, CurrentDatabasePath, true);
MessageBox.Show("Restore successful! ");
}
private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
openFileDialogBackUp.FileName = "dbCPS";
openFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
if (openFileDialogBackUp.ShowDialog() == DialogResult.OK)
RestoreDatabase(openFileDialogBackUp.FileName);
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
此代码提取压缩文件,但我不知道如何同时进行恢复。
using (ZipFile zip = ZipFile.Read(restoreFile))
{
zip.ExtractAll(@"C:\Users\Desktop\backup");
}
答案 0 :(得分:0)
根据MSDN文档here,您应该能够将zip文件直接解压缩到目标文件夹。您现有的“恢复”代码只是将文件副本复制到CurrentDatabasePath
,因此人们可以期待像...这样的语句。
System.IO.Compression.ZipFile.ExtractToDirectory(pathToZipFile, CurrentDatabasePath);
......就是这么多。 (我会自己测试一下,但显然在.NET 4.5中添加了ZipFile
,而我的Visual Studio副本太旧了。)