SaveProgress Example for DotNetZip

When i was coding Send2FTP i needed a ZIP-Library that would be able to notify me about the progress while the compression.

After a littlebit research i found DotNetZip a really impressive ZIP Libary for dotNet.

But what i couldn’t find was a good WinForms example for the Save Progress Event. I Also don’t like to deliver my application with alot of dll’s therefore i packed the sources of DotNetZip into the Example.

So i made a small WinForms Example for implementing the SaveProgress Event of the DotNetZip Library and want to share it with you:

The Code isn’t too much only two Methods.

The buttonClick Event:

private void buttonCompress_Click(object sender, EventArgs e)
{
	if ((folderBrowserDialog1.ShowDialog() == DialogResult.OK) && (saveFileDialog1.ShowDialog() == DialogResult.OK))
	{
		buttonCompress.Enabled = false;
 
		String DirectoryToZip = folderBrowserDialog1.SelectedPath;
		String ZipFileToCreate = saveFileDialog1.FileName;
 
		using (ZipFile zip = new ZipFile())
		{
			zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;
			zip.SaveProgress += SaveProgress;
 
			zip.StatusMessageTextWriter = System.Console.Out;
			zip.AddDirectory(DirectoryToZip); // recurses subdirectories
			zip.Save(ZipFileToCreate);
		}
	}
}

And more interesting the SaveProgress Callback Method:

public void SaveProgress(object sender, SaveProgressEventArgs e)
{
	if (e.EventType == ZipProgressEventType.Saving_Started)
	{
		MessageBox.Show("Begin Saving: " + e.ArchiveName);
	}
	else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
	{
		labelCompressionStatus.Text = "Writing: " + e.CurrentEntry.FileName + " (" + (e.EntriesSaved + 1) + "/" + e.EntriesTotal + ")";
		labelFilename.Text = "Filename:" + e.CurrentEntry.LocalFileName;
 
		progressBar2.Maximum = e.EntriesTotal;
		progressBar2.Value = e.EntriesSaved + 1;
	}
	else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
	{
		progressBar1.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);
	}
	else if (e.EventType == ZipProgressEventType.Saving_Completed)
	{
		MessageBox.Show("Done: " + e.ArchiveName);
	}
}

Download (Sources&Binaries)

10 thoughts on “SaveProgress Example for DotNetZip

  1. Very nice code, thank you!

    But, how could i do this for extract a zip, istead to compress, please?

    I’ve the function:

    private void MyExtract(string zipToUnpack, string unpackDirectory)
    {
    using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
    {
    // here, we extract every entry, but we could extract conditionally
    // based on entry name, size, date, checkbox status, etc.
    foreach (ZipEntry e in zip1)
    {
    e.Password = “mySecretPass”;
    e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
    }
    }
    }

    And 2 forms (1 for choose file – not important for the status – and another to show the status of decompressing…)

    The second one will open after onClick event that calls the function MyExtract().

    It has only 1 progressbar (total – not for individual files) and a label with the name of file that is extracting, thats all.

    Could you help me, giving me an example please?

    Thank you!!!

  2. In c#.Net 2010,use like this.

    progressBar1.Value = Convert.ToInt32((e.BytesTransferred * 100) / e.TotalBytesToTransfer);

    • Just like in the calculation for the progressbar value:

      int percentage = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer); // Percentage

      • Nick,
        Can you provide a full example of this with a backgroundWorker?
        I have been looking for a working and so far, your sample is the only one I
        have found that uses dotnetzip with 2 progress bars AND actually works.

        Thank you!
        -Matt

      • Hi
        if I used that solution, I have always result 100%. Because e.BytesTransferred and e.TotlaBytesToTransfer are same values. I do not know what the problem is.

        Could you help me?
        Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *