0

我在 C# 中有这个备份和恢复代码,使用 MS Access 作为它的数据库。我完成了 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");
        }
4

1 回答 1

0

根据此处的 MSDN 文档,您应该能够将 zip 文件直接提取到目标文件夹。您现有的“恢复”代码只是将文件复制到CurrentDatabasePath,所以人们会期望这样的语句......

System.IO.Compression.ZipFile.ExtractToDirectory(pathToZipFile, CurrentDatabasePath);

......将是所有需要的。(我会自己测试它,但ZipFile显然是在 .NET 4.5 中添加的,而且我的 Visual Studio 副本太旧了。)

于 2013-06-16T14:19:24.180 回答