0

I have a DotNetZip code for zipping the folder. It doesnt zip the file for the first time after cleaning the solution. After that it works fine. Can anybody know the issue why its happening??

Button Click Event Code

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

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

Method that gets called from Button Click

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.";
    }
}

Add & Save Handlers for Progress

    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();
        }

    }
DonMax
  • 970
  • 3
  • 12
  • 47
  • "It doesn't zip the file" but which error message you get? BTW don't pollute your code with Application.DoEvents(), if task is asynchronous then move it to another thread and use BeginInvoke to synchronize/update UI. – Adriano Repetti Jul 24 '14 at 10:17
  • I didnt get any error message. But It didnt reach here `ZipProgressEventType.Adding_AfterAddEntry` for the first time even though there are some files to add to zip folder. – DonMax Jul 24 '14 at 10:21
  • Try to change status message writer to a string in memory and see what's going on (logs to console will be lost for a winforms application). – Adriano Repetti Jul 24 '14 at 10:28

0 Answers0