PowerTCP Zip Compression for .NET
QuickZip



The QuickZip method presents a quick and easy way to compress a given source to a specified zip archive. It combines the two operations of Add and Zip into a single operation. The source is a filename, and can include wildcards. The destination can be a file or stream, according to the application needs.

Quickly zipping to a compressed file

To quickly and easily zip a file or wildcard collection of files to a compressed file:

  1. Add the Archive component to a new form.
  2. Add a button to the form.
  3. Specify the archive properties. For example, set the Password and Encryption properties to encrypt the entire collection. In the example to follow, all files in the directory "c:\Test" and its subdirectories, with the exception of "txt" files, are archived to the file "c:\test.zip". The archived files' paths are preserved during compression.
  4. Use the QuickZip method to create the zip file. Place the code below in the click event of the button added to the form in step 2.
  5. Compile and run the application. After pressing the button, a new archive "c:\test.zip" with the files from "c:\Test" should be found on disk.
C#
Copy Code
archive1.PreservePath = true;
archive1.IncludeSubs = true;
archive1.ExcludePattern = "*.txt";
archive1.QuickZip("c:\\Test\\*.*", "c:\\test.zip");
Visual Basic
Copy Code
Archive1.PreservePath = True
Archive1.IncludeSubs = True
Archive1.ExcludePattern = "*.txt"
Archive1.QuickZip("c:\Test\*.*", "c:\test.zip")

Quickly zipping to a compressed stream

To zip a file or wildcard collection of files to a compressed stream:

  1. Follow steps 1, 2 and 3 above, for zipping to a compressed file.
  2. Create a stream to hold the compressed archive.
  3. Use the QuickZip method to save the compressed archive to memory.
  4. Show the length of the new compressed archive.
  5. Compile and run the application. After pressing the button, a MessageBox with the size of the stream should be visible.
C#
Copy Code
System.IO.MemoryStream stream = new System.IO.MemoryStream();
archive1.QuickZip("c:\\Test\\*.*", stream);
MessageBox.Show("The size of the compressed stream is " + stream.Length.ToString() + " bytes.");
Visual Basic
Copy Code
Archive1.QuickZip("c:\Test\*.*", stream)
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()
MessageBox.Show("The size of the compressed stream is " + stream.Length.ToString() + "bytes.")

Asynchronous quick zipping

To zip a file or wildcard collection of files asynchronously:

  1. Follow steps 1, 2 and 3 above, for zipping to a compressed file.
  2. Use the asynchronous BeginQuickZip method to create the zip file.
  3. Add code for the EndZip Event. When using the BeginXXX methods, you must have corresponding EndXXX events (see Asynchronous Operation for more details).
  4. Compile and run the application. After pressing the button, a new archive "c:\test.zip" with the files from "c:\Test" should be found on disk.
C#
Copy Code
archive1.BeginQuickZip("c:\\Test\\*.txt", "c:\\test.zip", null);
...
private void archive1_EndZip(object sender, Dart.PowerTCP.Zip.EndEventArgs e)
{
    try
    {
        if (e.Exception == null)
            MessageBox.Show("Zip Complete!");
    else
        MessageBox.Show(e.Exception.Message);
    }
    catch {}
}
Visual Basic
Copy Code
Archive1.BeginQuickZip("c:\Test\*.*", "c:\test.zip", Nothing)
...
Private Sub Archive1_EndZip(ByVal sender As Object, ByVal e As Dart.PowerTCP.Zip.EndEventArgs) Handles Archive1.EndZip
    Try
        If (e.Exception Is Nothing) Then
            MessageBox.Show("Zip Complete!")
        Else
            MessageBox.Show(e.Exception.Message)
        End If
    Catch
    End Try
End Sub

PowerTCP Zip for .NET Documentation Version 2.1.1
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic