---
title: "How do you zip a folder and all its contents using built-in C# libraries?"  
description: "How do you zip a folder and all its contents using built-in C# libraries?"  
author: "ICSM Computer"  
published: 2025-05-13  
updated: 2025-05-26  
canonical: https://www.mindstick.com/forum/161613/how-do-you-zip-a-folder-and-all-its-contents-using-built-in-c-sharp-libraries  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# How do you zip a folder and all its contents using built-in C# libraries?

How do you [zip](https://www.mindstick.com/interview/34182/compress-old-log-files-e-g-zip-after-7-days) a folder and all its contents using built-in C# [libraries](https://answers.mindstick.com/qa/49244/which-company-is-ahead-in-classification-of-data-for-libraries-innovations)?

## Replies

### Reply by Anubhav Sharma

To **zip a folder and all its contents** using built-in C# libraries, use the `System.IO.Compression.ZipFile` class, available in .NET Framework 4.5+ and .NET Core.

### Example: Zip an Entire Folder

```cs
using System.IO.Compression;

public class ZipHelper
{
    public static void ZipFolder(string sourceFolderPath, string destinationZipPath)
    {
        // Overwrite if ZIP already exists
        if (File.Exists(destinationZipPath))
            File.Delete(destinationZipPath);

        ZipFile.CreateFromDirectory(sourceFolderPath, destinationZipPath, CompressionLevel.Optimal, includeBaseDirectory: true);
    }
}
```

### Usage

```cs
string sourceFolder = @"C:\MyData\Reports";
string destinationZip = @"C:\MyData\Reports.zip";

ZipHelper.ZipFolder(sourceFolder, destinationZip);
```

### Parameters Explained

| Parameter | Description |
| --- | --- |
| `sourceFolderPath` | Folder to zip |
| `destinationZipPath` | Destination ZIP file path |
| `CompressionLevel.Optimal` | Uses best balance of compression and speed |
| `includeBaseDirectory: true` | Includes the top-level folder in the archive |

### Notes

1. Requires a reference to `System.IO.Compression` and `System.IO.Compression.FileSystem`.
2. If using .NET Core or .NET 5+, just import the namespaces—no need for extra assembly references.


---

Original Source: https://www.mindstick.com/forum/161613/how-do-you-zip-a-folder-and-all-its-contents-using-built-in-c-sharp-libraries

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
