ComponentOne Zip for .NET 2.0
In This Topic
    Compressing and Decompressing Folders
    In This Topic

    C1Zip allows users to zip entire folders without tampering the original folder structure and compress them into a zip archive, and decompress them back.

    In this topic, you will learn how to compress and decompress folders using C1.Zip library.

    The image shows an application performing compressing and decompressing folders using C1.Zip.

    Compress Folders

    The Zip library provides the AddFolder method in C1ZipEntryCollection to compress an entire folder into a zip file, preserving the folder structure for later expansion. The AddFolder method has many overloads in the class. In the code snippet, we have used the AddFolder(string path, string searchPattern, bool includeSubfolders) method. This overload method uses the parameters such as path, which is the full path of the folder to be added to the zip file, searchPattern, which is a mask that specifies which files to add, and includeSubfolders, which can be set to true to include sub folders, and set to false to only include files at the root level. In the code snippet, the AddFolder method is called on the Entries property of the C1ZipFile object.

    This is the C# Code for compressing and decompressing folders in WinForms applications:

    C#
    Copy Code
    private void button1_Click(object sender, EventArgs e)
    {
        // Create the C1ZipFile member
        C1.Zip.C1ZipFile zip = new C1.Zip.C1ZipFile();
        zip.Create(@"C:\temp\file.zip");
        // Compress the folder into a zip file
        zip.Entries.AddFolder(@"C:\temp\Data-dll", "*.*", true);
    }
    

    This is the VB Code for compressing and decompressing folders in WinForms applications:

    VB
    Copy Code
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim zip As C1.Zip.C1ZipFile = New C1.Zip.C1ZipFile()
        zip.Create("C:\temp\file.zip")
        ' Compress the folder into a zip file
        zip.Entries.AddFolder("C:\temp\Data-dll", "*.*", True)
    End Sub
    

    This is the C# code for compressing and decompressing folders in WPF applications:

    C#
    Copy Code
    void BtnNew_Click(object sender, RoutedEventArgs e)
    {
        // Create the C1ZipFile member.
        zip = new C1ZipFile();
        // Show open file dialog. 
        SaveFileDialog fo = new SaveFileDialog();
        fo.Filter = "Zip Files (*.zip) | *.zip";
        var open = fo.ShowDialog();
        if (open.HasValue && open.Value)
        {
            try
            {
                // Create zip file. 
                zip.Create(fo.FileName);
            }
            catch
            {
                MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip");
            }
        }
    }
    

    This is the VB code for compressing and decompressing folders in WPF applications:

    VB
    Copy Code
    Private Sub BtnNew_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Create the C1ZipFile member.
        zip = New C1ZipFile()
        ' Show open file dialog. 
        Dim fo As SaveFileDialog = New SaveFileDialog()
        fo.Filter = "Zip Files (*.zip) | *.zip"
        Dim open = fo.ShowDialog()
        If open.HasValue AndAlso open.Value Then
            Try
                ' Create zip file. 
                zip.Create(fo.FileName)
            Catch
                MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip")
            End Try
        End If
    End Sub
    

    Decompress Folders

    The Zip library provides the ExtractFolder method in C1ZipEntryCollection class to extract the contents of the zip file into a specified path. The method has the path parameter, which denotes destination path for the unzipped files. Note that if the zip file contains compressed folders, new folders will be created under the destination path to preserve the hierarchical structure of the zip archive. In the code snippet, the ExtractFolder method is called on the Entries property of the C1ZipFile object.

    This is the C# Code for compressing and decompressing folders in WinForms applications:

    C#
    Copy Code
    private void button2_Click(object sender, EventArgs e)
    {
         C1.Zip.C1ZipFile zip = new C1.Zip.C1ZipFile();
        zip.Open(@"C:\temp\imagelist.zip");
        // Decompress the zip file into a folder
        zip.Entries.ExtractFolder(@"C:\temp\empty");
    }
    

    This is the VB Code for compressing and decompressing folders in WinForms applications:

    VB
    Copy Code
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim zip As C1.Zip.C1ZipFile = New C1.Zip.C1ZipFile()
        zip.Open("C:\temp\imagelist.zip")
        ' Decompress the zip file into a folder
        zip.Entries.ExtractFolder("C:\temp\empty")
    End Sub
    

    This is the C# code for compressing and decompressing folders in WPF applications:

    C#
    Copy Code
    private void BtnOpen_Click(object sender, RoutedEventArgs e)
    {
        // Create the C1ZipFile member.
        zip = new C1ZipFile();
        Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
        op.Filter = "Zip(*.zip)|*.zip";
        var open = op.ShowDialog();
        if (open.HasValue && open.Value)
        {
            if (zip == null)
            {
                zip = new C1ZipFile();
            }
    
            zip.Open(op.FileName);
            btnExtract.IsEnabled = true;
            UpdateDisplay();
        }
    }
    

    This is the VB code for compressing and decompressing folders in WPF applications:

    VB
    Copy Code
    Private Sub BtnOpen_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Create the C1ZipFile member.
        zip = New C1ZipFile()
        Dim op As OpenFileDialog = New OpenFileDialog()
        op.Filter = "Zip(*.zip)|*.zip"
        Dim open = op.ShowDialog()
        If open.HasValue AndAlso open.Value Then
            If zip Is Nothing Then
                zip = New C1ZipFile()
            End If
    
            zip.Open(op.FileName)
            Me.btnExtract.IsEnabled = True
            UpdateDisplay()
        End If
    End Sub
    
    See Also