使用C#提取Zip文件并带有进度报告

3
有人能告诉我是否可以(如果可以的话并给出一个例子),如何使用“ZipFile”(Ionic.zip,http://dotnetzip.codeplex.com/)显示进度条(和状态标签,如果可能)来显示ZIP文件被解压缩的进度吗?
我的WinForm很好地从我选择的路径提取ZIP文件到新路径,使用文本框、浏览按钮等等没有问题......然而唯一的问题是在这个过程中我不能使用表单上的任何东西,它就像它已经冻结了,但实际上只是因为它正在后台解压缩ZIP文件。
ZIP文件是一个大文件,我想通过添加和使用进度条来显示解压缩的进度和准确的预计时间,使其更加清晰易懂。
这肯定是可能的,我只是无法弄清楚如何在C# WinForms中实现,我已经在网上搜索了相当长时间,但仍未能找到适合我的示例。
下面是我所拥有的粗略示例:
private void button1_Click(object sender, EventArgs e)
{
    var ROBOT0007 = textBox1.Text + @"\" + "ROBOT0007"; //ROBOT0007 folder
    var ROBOT_INSTALL = textBox1.Text + @"\" + "911" + @"\" + "files"; //ROBOT0007/911/files
    var ROBOT_INSTALL_SPECIAL = ROBOT_INSTALL + @"\" + "special.rar";  //ROBOT0007/911/files/special.rar

    //If the path has text...
    if (textBox1.TextLength > 0)
    {
        //if the subfolder doesn't exist then make it.
        if (!Directory.Exists(ROBOT0007))
        {
            Directory.CreateDirectory(ROBOT0007);
        }

        //if the textbox directory exists
        if (Directory.Exists(ROBOT0007))
        {
            using (ZipFile zip = ZipFile.Read(ROBOT_INSTALL_SPECIAL))
            {
                zip.ExtractAll(ROBOT0007, ExtractExistingFileAction.OverwriteSilently);

            } 
        }
    }
}

更新(2014年04月11日):我现在已经移除了文本框,回归到简单的基础功能。以下代码使用一个后台工作线程,但是取消按钮对RAR文件没有效果...有什么建议吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;
using System.IO;

namespace BackgroundWorkerSample
{
    // The BackgroundWorker will be used to perform a long running action
    // on a background thread.  This allows the UI to be free for painting
    // as well as other actions the user may want to perform.  The background
    // thread will use the ReportProgress event to update the ProgressBar
    // on the UI thread.
    public partial class Form1 : Form
    {
        /// <summary>
        /// The backgroundworker object on which the time consuming operation 
        /// shall be executed
        /// </summary>
        BackgroundWorker backgroundWorker1;

        public Form1()
        {
            InitializeComponent();
            backgroundWorker1 = new BackgroundWorker();

            // Create a background worker thread that ReportsProgress &
            // SupportsCancellation
            // Hook up the appropriate events.
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler
                    (backgroundWorker1_ProgressChanged);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                    (backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        /// <summary>
        /// On completed do the appropriate task
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // The background process is complete. We need to inspect
            // our response to see if an error occurred, a cancel was
            // requested or if we completed successfully.  
            if (e.Cancelled)
            {
                lblStatus.Text = "Task Cancelled.";
            }

            // Check to see if an error occurred in the background process.

            else if (e.Error != null)
            {
                lblStatus.Text = "Error while performing background operation.";
            }
            else
            {
                // Everything completed normally.
                lblStatus.Text = "Task Completed...";
            }

            //Change the status of the buttons on the UI accordingly
            btnStart.Enabled = true;
            btnCancel.Enabled = false;
        }

        /// <summary>
        /// Notification is performed here to the progress bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            // This function fires on the UI thread so it's safe to edit

            // the UI control directly, no funny business with Control.Invoke :)

            // Update the progressBar with the integer supplied to us from the

            // ReportProgress() function.  

            progressBar1.Value = e.ProgressPercentage;
            lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }

        /// <summary>
        /// Time consuming operations go here </br>
        /// i.e. Database operations,Reporting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // The sender is the BackgroundWorker object we need it to
            // report progress and check for cancellation.
            //NOTE : Never play with the UI thread here...
            for (int i = 0; i < 100; i++)
            {
                //Thread.Sleep(100);
                string INSTALL_FOLDER= "C:" + @"\" + "Program Files (x86)" + @"\" + "Robot91111"+ @"\" + "basic" + @"\" + "string" + @"\" + "special.rar";
                string BURGOS_FOLDER = "C:" + @"\" + "Program Files (x86)" + @"\" + "Robot91111" + @"\" + "Burgos_Folder";
                if (!Directory.Exists(BURGOS_FOLDER))
                    {
                        Directory.CreateDirectory(BURGOS_FOLDER);
                        using (ZipFile zip = ZipFile.Read(INSTALL_FOLDER))
                        {
                            zip.ExtractAll(BURGOS_FOLDER, ExtractExistingFileAction.OverwriteSilently);
                        }
                    }

                // Periodically report progress to the main thread so that it can
                // update the UI.  In most cases you'll just need to send an
                // integer that will update a ProgressBar                    
                backgroundWorker1.ReportProgress(i);
                // Periodically check if a cancellation request is pending.
                // If the user clicks cancel the line
                // m_AsyncWorker.CancelAsync(); if ran above.  This
                // sets the CancellationPending to true.
                // You must check this flag in here and react to it.
                // We react to it by setting e.Cancel to true and leaving
                if (backgroundWorker1.CancellationPending)
                {
                    // Set the e.Cancel flag so that the WorkerCompleted event
                    // knows that the process was cancelled.
                    e.Cancel = true;
                    backgroundWorker1.ReportProgress(0);
                    return;
                }
            }

            //Report 100% completion on operation completed
            backgroundWorker1.ReportProgress(100);
        }

        private void btnStartAsyncOperation_Click(object sender, EventArgs e)
        {
            //Change the status of the buttons on the UI accordingly
            //The start button is disabled as soon as the background operation is started
            //The Cancel button is enabled so that the user can stop the operation 
            //at any point of time during the execution
            btnStart.Enabled = false;
            btnCancel.Enabled = true;

            // Kickoff the worker thread to begin it's DoWork function.
            backgroundWorker1.RunWorkerAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {

                // Notify the worker thread that a cancel has been requested.

                // The cancel will not actually happen until the thread in the

                // DoWork checks the backgroundWorker1.CancellationPending flag. 

                backgroundWorker1.CancelAsync();
            }
        }
    }
} 

有人可以用后台工作器来给个例子吗?每次我尝试使用“开始按钮”来启动解压缩,以及一个“停止按钮”来停止解压缩时,由于文本框和标签,会出现异常错误。如果我将它们注释掉,取消按钮在解压完成之前将无法工作。有什么建议吗? - Burgo855
你有尝试处理 zip.ExtractProgress 事件吗? - Marton
在项目的codeplex页面上显示了VB.Net示例:http://dotnetzip.codeplex.com/ - Marton
作为一名C#开发者,你只需要不到5分钟就能理解那段VB代码片段。 - Marton
显示剩余3条评论
4个回答

6
/*...*/
using (ZipFile zip = ZipFile.Read(ROBOT_INSTALL_SPECIAL))
        {
            zip.ExtractProgress += 
               new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
            zip.ExtractAll(ROBOT0007, ExtractExistingFileAction.OverwriteSilently);

        }
/*...*/

void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
   if (e.TotalBytesToTransfer > 0)
   {
      progressBar1.Value = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
   }
}

在进行上述操作后,当我运行exe文件时,出现了一个错误:"在BackgroundWorkerSample.exe中发生了一种类型为 'System.DivideByZeroException' 的异常,但在用户代码中未处理。额外信息:试图除以零。如果有此异常的处理程序,则可以安全地继续运行程序。"我不确定该如何解决这个问题?我尝试使用try/catch异常来处理,然后运行时没有出现错误,但是又回到了原点,取消按钮无法取消进程... - Burgo855
@Burgo855 我更新了我的答案来处理DivideByZeroException异常。至于取消进程,你需要自己进行一些研究,因为我不了解DotNetZip库。如果你仍然找不到解决方案,我建议你在这里开一个新的问题。StackOverflow上的问题应该只涉及一个特定的问题。 - Marton

3
private int totalFiles;
private int filesExtracted;

/*...*/

using (ZipFile zip = ZipFile.Read(ROBOT_INSTALL_SPECIAL))
{
    totalFiles = zip.Count;
    filesExtracted = 0;
    zip.ExtractProgress += ZipExtractProgress; 
    zip.ExtractAll(ROBOT0007, ExtractExistingFileAction.OverwriteSilently);
}

/*...*/

private void ZipExtractProgress(object sender, ExtractProgressEventArgs e)
{
    if (e.EventType != ZipProgressEventType.Extracting_BeforeExtractEntry)
        return;
    filesExtracted++;
    progressBar.Value = 100 * filesExtracted / totalFiles;
}

1
请尝试解释一下你的代码是做什么的。与被接受的答案相比,它有什么不同之处,为什么需要进行这些更改? - ollpu
这是唯一对我有效的代码。 它使用文件计数而不是传输的字节数来表示进度百分比。 - user2629253
你仍然可以尝试解释代码的不同之处。 - ollpu
这段代码使用压缩文件中的总文件数来确定进度,而不是传输的字节数,这样每次提取新的压缩条目时就会重置。 - Dave 5709

0
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

    using (ZipFile zip = ZipFile.Read(@"edu.zip"))
    {
        totalFiles = zip.Count;
        filesExtracted = 0;
        zip.ExtractProgress += ZipExtractProgress;
        zip.ExtractAll(@"./", ExtractExistingFileAction.OverwriteSilently);
    }
    if (backgroundWorker1.CancellationPending)
    {

        e.Cancel = true;
        backgroundWorker1.ReportProgress(0);
        return;
    }
    backgroundWorker1.ReportProgress(100);
}

private void ZipExtractProgress(object sender, ExtractProgressEventArgs e)
{
    if (e.EventType != ZipProgressEventType.Extracting_BeforeExtractEntry)
        return;
    filesExtracted++;
    this.Dispatcher.Invoke(new Action(() =>
    {
        progressBar1.Value = 100 * filesExtracted / totalFiles;
    }));

}

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        status.Content = "extractia a fost anulata";
    }
    else if (e.Error != null)
    {
        status.Content = "Ceva nu a mers ";
    }

}

void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    status.Content = "Se dezarhiveaza......" + progressBar1.Value.ToString() + "%";
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

在此之前,你需要创建一个BackgroundWorker对象: backgroundWorker1 = new BackgroundWorker(); 然后,你需要为该对象的DoWork事件、ProgressChanged事件和RunWorkerCompleted事件分别添加相应的处理方法: backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); 同时,你还需要设置backgroundWorker1的WorkerReportsProgress属性为true,以便报告进度信息; 并且将backgroundWorker1的WorkerSupportsCancellation属性设置为true,以支持取消操作。 - CoxMaster

0
        使用 System.IO.Compression;
private async void 解压(string filePath) { var _downloadPath = configuration.GetValue("DownloadPath"); var _extractPath = configuration.GetValue("ExtractPath"); var _extractPattern = configuration.GetValue("ExtractPattern");
Console.WriteLine($"正在删除目录中的旧文件: '{_extractPath}'"); var directoryInfo = new DirectoryInfo(_extractPath); foreach (var file in directoryInfo.GetFiles()) { file.Delete(); } Console.WriteLine($"正在解压文件: '{filePath}'");
var regex = new Regex(_extractPattern); var fileList = new List(); var totalFiles = 0; var filesExtracted = 0;
using (var archive = await Task.Run(() => ZipFile.OpenRead(filePath))) { foreach (var file in archive.Entries) { if (regex.IsMatch(file.Name)) { fileList.Add(file); totalFiles++; } }
foreach (var file in fileList) { Console.WriteLine($"正在提取文件: '{file.Name}'"); await Task.Run(() => { file.ExtractToFile($"{_extractPath}{file.Name}"); filesExtracted++; var progress = Convert.ToInt32(100 * filesExtracted / totalFiles); Console.WriteLine($"已提取: {progress}%"); }); } } }
appsettings.json 示例 { "DownloadPath": "f:\\download\\", "ExtractPath": "f:\\download\\extract\\", "ExtractPattern": "ACTSTAT.DBF|CENTERST.DBF|CURENTST.DBF|ESTSTAT.DBF|FLATTYPE.DBF|NDOCTYPE.DBF|OPERSTAT.DBF|ROOMTYPE.DBF|SOCRBASE.DBF|STRSTAT.DBF|[A-Z]{1}16.DBF", }

1
虽然这段代码片段可能解决了问题,但它并没有解释为什么或者如何回答这个问题。请在你的代码中包含解释,因为这真的有助于提高你的帖子质量。记住,你是在为未来的读者回答问题,而这些人可能不知道你提出代码建议的原因。你可以使用[编辑]按钮改进这个答案以获得更多的投票和声望! - Brian Tompsett - 汤莱恩

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接