# Compressing and Decompressing Folders

With the information in this topic, you can perform compression and decompression of folders using C1.Zip API.

## Content



**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.

![UI with buttons](https://cdn.mescius.io/document-site-files/images/1ca042b8-879c-4e78-bfe8-47e89261a2fa/images/compress-decompress-folders-zip.png)

### Compress Folders

The Zip library provides the [AddFolder](/componentone/api/services/online-zip/dotnet-standard-api/C1.Zip/C1.Zip.C1ZipEntryCollection.AddFolder.html) method in [C1ZipEntryCollection](/componentone/api/services/online-zip/dotnet-standard-api/C1.Zip/C1.Zip.C1ZipEntryCollection.html) 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](/componentone/api/services/online-zip/dotnet-standard-api/C1.Zip/C1.Zip.C1ZipFile.html) object.

### WinForms

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

```csharp
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:

```vbnet
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
```

### WPF

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

```csharp
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:

```vbnet
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](/componentone/api/services/online-zip/dotnet-standard-api/C1.Zip/C1.Zip.C1ZipEntryCollection.ExtractFolder.html) 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.

### WinForms

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

```csharp
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:

```vbnet
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
```

### WPF

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

```csharp
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:

```vbnet
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

[Compressing and Decompressing Files](/componentone/docs/services/online-zip/compressing-and-decompressing-files)

[Compress Files to .NET Streams](/componentone/docs/services/online-zip/compress-files-netstreams)

[Walkthrough](/componentone/docs/services/online-zip/walkthrough)