Dev Direct Solution Center

For more information and to buy this product...

Creating a ZIP file from a .Net WinForms App using DART PowerTCP Zip Compression

  For a copy of the sample project please click here to download and install the product evaluation.

Introduction

This solution illustrates how we can create an application which selects one or more input files and generate a standard ZIP file which can be read by ZIP applications such as WinZip. The resultant file is ideal for transmission over the Internet. The application provides control over encryption, the compression level and provides progress information to the user.

Detail

The application will enable a user to select a list of files to be Zipped up, select a level of compression and whether or not encryption is required, and then specify the name/location of the output ZIP archive. The UI controls are all standard .Net controls and the work is done by Dart's PowerTCP Zip Compression component.

Setup

Get the sample code by downloading and installing the evaluation version of the product using the link above. Following installation, the sample .Net project will be located in:

My Documents\Dart Sample Applications\PowerTCP\Zip Compression for .NET Trial\VS2005\WinForms\Zipper

Select VB or C# directory according to your language preference.

The ZIP component of type Dart.PowerTCP.Zip.Archive is called archive1 and it sits on the form as a non-visual component.

Selecting files to be Zipped

The "Browse" button displays a common dialog control and this returns a string array of filenames which are loaded into archive1 in the FillList method:

C#
private void FillList() { Working = true; //Add files to archive from specified search pattern statusZip.Text = "Searching for files ..."; try { archive1.PreservePath = chkPreservePaths.Checked; foreach(string FileName in FilesSelected) { archive1.Add(FileName); } } catch (Exception ex) { if (!Killing) MessageBox.Show(ex.Message); } . .
VB.Net
Private Sub FillList() Working = True 'Add files to archive from specified search pattern statusZip.Text = "Searching for files ..." Try archive1.PreservePath = chkPreservePaths.Checked For Each FileName As String In FilesSelected archive1.Add(FileName) Next FileName Catch ex As Exception If (Not Killing) Then MessageBox.Show(ex.Message) End If End Try . .

Note that the PreservePath property is set prior to loading the filenames into archive1 to ensure that the path information for each file is preserved or not according to the user's preference.

The list of files in the archive can be cleared using the Clear() method, which in this case is called from the Click handler of the "Clear" button.

Creating the archive

The code to create the archive is contained in the "Zip" button's click event handler.

A dialog is displayed which enables the user to specify the name and location of the output archive:

C#
private void cmdZip_Click(object sender, System.EventArgs e) { Working = true; cmdZip.Capture = false; //Zip files in archive if (cmdZip.Text == "Zip!") { //Specify archive name dlgSave.Title = "Save Archive"; dlgSave.Filter = "ZIP Files (*.zip)|*.zip"; dlgSave.DefaultExt = ".zip"; dlgSave.FileName = ""; dlgSave.OverwritePrompt = true; if (dlgSave.ShowDialog() == DialogResult.OK) { . .
VB.Net
Private Sub cmdZip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdZip.Click Working = True cmdZip.Capture = False 'Zip files in archive If cmdZip.Text = "Zip!" Then 'Specify archive name dlgSave.Title = "Save Archive" dlgSave.Filter = "ZIP Files (*.zip)|*.zip" dlgSave.DefaultExt = ".zip" dlgSave.FileName = "" dlgSave.OverwritePrompt = True If dlgSave.ShowDialog() = System.Windows.Forms.DialogResult.OK Then . .

If the user has checked the "Encrypt Files" check box then we display a form to get the password and encryption type. These are passed into the Zip component via the Password and DefaultEncryption properties respectively.

C#
. . if (cbEncrypt.Checked) { frmPassword Pass = new frmPassword(""); if (Pass.ShowDialog() == DialogResult.OK) { archive1.Password = Pass.txtPassword.Text; archive1.DefaultEncryption = (Encryption)Pass.cboEncryption.SelectedIndex + 1; for (int i = 0; i < archive1.Count; i++) { lvwFiles.Items[i].ForeColor = Color.Red; } } } . .
VB.Net
. . If cbEncrypt.Checked Then Dim Pass As New frmPassword("") If Pass.ShowDialog() = System.Windows.Forms.DialogResult.OK Then archive1.Password = Pass.txtPassword.Text archive1.DefaultEncryption = CType(Pass.cboEncryption.SelectedIndex, Encryption) + 1 For i As Integer = 0 To archive1.Count - 1 lvwFiles.Items(i).ForeColor = Color.Red Next i End If End If . .

Finally the Zipmethod is invoked, passing the filename of the Zip archive to be created...

C#
archive1.Zip(dlgSave.FileName);
VB.Net
archive1.Zip(dlgSave.FileName)

There are several events that Archive class can raise while executing the Zip method. We handle Progress, BusyChanged and Exception.

Progress is where we update the display of the progress bar.

The handler receives an instance of Dart.PowerTCP.Zip.ProgressEventArgs "e", which contains ProcessedTotalBytes and TotalBytes (as well as other useful values) and can be used to drive the progress bar as follows:

C#
private void archive1_Progress(object sender, Dart.PowerTCP.Zip.ProgressEventArgs e) { try { progZip.Maximum = 100; double Percent = ((double)e.ProcessedTotalBytes / (double)e.TotalBytes) * 100; progZip.Value = Convert.ToInt16(Percent); } catch { } }
VB.Net
Private Sub archive1_Progress(ByVal sender As Object, ByVal e As Dart.PowerTCP.Zip.ProgressEventArgs) Handles archive1.Progress Try progZip.Maximum = 100 Dim Percent As Double = (CDbl(e.ProcessedTotalBytes) / CDbl(e.TotalBytes)) * 100 progZip.Value = Convert.ToInt16(Percent) Catch End Try End Sub

Conclusion

Our solution shows that with just a couple of method calls we can generate a standard Zip archive containing one or several files. With just a little extra code PowerTCP Zip Compression can also encrypt the archive for added protection. This solution can easily be adapted to create a ZIip dialog for you application.

Of course, this is just a simple example and PowerTCP Zip Compression will also allow you to create more complex applications incluing compression and decompression via streams, advanced encryption and disk spanning.

Visit Dart Communications for more information and more samples.