如何在C#、WinForms中覆盖提取的zip文件

本文关键字:提取 覆盖 zip 文件 WinForms | 更新日期: 2023-09-27 18:19:29

我有这样的代码,使用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(CurrentDatabasePath);
        }

如何在C#、WinForms中覆盖提取的zip文件

我做到了!对于那些需要代码的人,这里是:

using (ZipFile zip = ZipFile.Read(pathBackup))
        {
            zip.ExtractAll(Environment.CurrentDirectory, ExtractExistingFileAction.OverwriteSilently);                
        }

在积极使用数据库时,不能直接覆盖数据库。
我所说的主动是指您在该数据库上打开了一个OleDbConnection

从上面的代码中,不可能理解您是否处于这种情况,因此要做的第一件事是搜索OleDbConnection的所有事件,并检查它们是否正确关闭。如果你有一个全局OleDbConnection,它在你的应用程序的整个生命周期内都保持打开状态(这是一种非常糟糕的做法),那么你需要在尝试覆盖accdb文件之前关闭它

public void RestoreDatabase(string restoreFile)
{
    string dbFileName = "dbCPS.accdb";
    string extractedFile = Path.GetTempFileName();
    string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
    using (ZipFile zip = ZipFile.Read(restoreFile))
    {
        // Extract to a temporary name built by the Framework for you....
        zip.ExtractAll(extractedFile);
    }
    // Now, before copying over the accdb file, you need to be sure that there is no
    // open OleDbConnection to this file, otherwise the IOException occurs because you
    // cannot change that file while it is actively used by a OleDbConnection
    // something like global_conn.Close(); global_conn.Dispose();
    File.Copy(extractedFile, CurrentDatabasePath, true);
    MessageBox.Show("Restore successful! "); 
    // and then reopen the connection
}
private void btn_UodateMembers_Click(object sender, EventArgs e)
{
    if (!bwUpdateMembers.IsBusy)
    {
        bwUpdateMembers.RunWorkerAsync();
    }
}
private string ExtractZip(FileInfo fi)
{
    string extractTo = Path.Combine(fi.DirectoryName, Guid.NewGuid().ToString());
    using (ZipFile zip = ZipFile.Read(fi.FullName))
    {
        foreach (ZipEntry ze in zip)
        {
            ze.Extract(extractTo, ExtractExistingFileAction.OverwriteSilently);
        }
    }
    return extractTo;
}
public FileInfo GetLatestFile(DirectoryInfo di)
{
    FileInfo fi = di.GetFiles()
        .OrderByDescending(d => d.CreationTime)
        .FirstOrDefault();
    return fi;
}
private void bwUpdateMembers_DoWork(object sender, DoWorkEventArgs e)
{
    string path = "C:''Users''Ghost Wolf''Desktop''zip";
    DirectoryInfo di = new DirectoryInfo(path);
    if (di != null)
    {
        FileInfo fi = GetLatestFile(di);
        string folder = ExtractZip(fi);
        MessageBox.Show("Your'e Files Have Been Extracted", "Notice", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
    }
}

请告诉我这对你是否有效!!