Zip文件夹第一次没有工作C#和DotNetZip

时间:2014-07-24 10:14:11

标签: c# dotnetzip

我有一个DotNetZip代码用于压缩文件夹。清洁解决方案后,它不会首次压缩文件。之后它工作正常。任何人都可以知道它为什么会发生这个问题吗?

按钮点击事件代码

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        copy_stuff(textBox1.Text, textBox2.Text, textBox3.Text);
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

从按钮点击

调用的方法
private void copy_stuff(string srcFolder, string destFolder, string Backup)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AddProgress += AddProgressHandler;
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
        zip.SaveProgress += SaveProgress;
        zip.StatusMessageTextWriter = System.Console.Out;
        zip.AddDirectory(destFolder);
        zip.Save(Backup + "\\VibrantBackup" + DateTime.Now.ToString("yyyyMMdd hh.mm.ss") + ".zip");
        label1.Text = "Compression completed.";
    }
}

添加&保存处理程序以取得进展

    int _numEntriesToAdd = 0;
    int _numEntriesAdded = 0;
    void AddProgressHandler(object sender, AddProgressEventArgs e)
    {
        switch (e.EventType)
        {
            case ZipProgressEventType.Adding_Started:
                _numEntriesToAdd = 0;
                _numEntriesAdded = 0;
                label1.Text = "Adding files to the zip...";
                label1.Update();
                Application.DoEvents();
                break;
            case ZipProgressEventType.Adding_AfterAddEntry:
                _numEntriesAdded++;
                label1.Text = String.Format("Adding file: {0} :: {2}",
                                         _numEntriesAdded, _numEntriesToAdd, e.CurrentEntry.FileName);
                label1.Update();
                Application.DoEvents();
                break;
            case ZipProgressEventType.Adding_Completed:
                label1.Text = "Added all files";
                label1.Update();
                Application.DoEvents();
                break;
        }
    }

    public void SaveProgress(object sender, SaveProgressEventArgs e)
    {
        if (e.EventType == ZipProgressEventType.Saving_Started)
        {
            label1.Text = "Begin Saving: " + e.ArchiveName;
            label1.Update();
            Application.DoEvents();
        }
        else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
        {
            label1.Text = "Processing : " + e.CurrentEntry.FileName;
            label1.Update();
            label3.Text = "Files Processed: (" + (e.EntriesSaved + 1) + "/" + e.EntriesTotal + ")";
            label3.Update();
            Application.DoEvents();
            progressBar3.Maximum = e.EntriesTotal;
            progressBar3.Value = e.EntriesSaved + 1;
        }
        else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
        {
            //progressBar2.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);
            //label3.Text = "Writing: " + e.CurrentEntry.FileName + " (" + (e.EntriesSaved + 1) + "/" + e.EntriesTotal + ")";
            label1.Update();
            Application.DoEvents();
        }

    }

0 个答案:

没有答案