How to Unzip A File in C#
Tags: csharp, dotnet, unzipfile, tutorial
Published: 2024-03-19 13:14:04

Working with [compressed files](https://www.win-rar.com/open-zip-file.html?&L=0) is a common task in software development, especially when dealing with large amounts of data or when transferring files over the Internet. In [C#](https://learn.microsoft.com/en-us/dotnet/csharp/), developers have several options for handling compressed files, one of which is [IronZip](https://ironsoftware.com/csharp/zip/), a powerful library for working with zip files in .NET applications. In this article, we'll explore how to unzip files using IronZip in C#.

##How to unzip a file in C#
1. Open or Create a New Project
2. Install IronZip Library
3. Add namespace IronZip
4. Use ExtractArchiveToDirectory() method to unzip file
5. Use ExtractArchiveToDirectory() with Password Parameter to unzip password-protected file
6. List all entries of the zip archive
7. Replace Entry using the ReplaceEntry() method
8. Delete Entry from the zip file using Delete() method

##What is IronZip.?

[IronZip](https://ironsoftware.com/csharp/zip/) is a leading C# ZIP archive library designed for [creating, reading, and extracting](https://ironsoftware.com/csharp/zip/tutorials/create-read-extract-zip/) archives in a .NET workspace. Developed by [Iron Software](https://ironsoftware.com/), it provides a user-friendly API prioritizing accuracy, ease of use, and speed. With IronZip, developers can efficiently manage zip files, including [creating](https://ironsoftware.com/csharp/zip/blog/using-ironzip/create-zip-file-in-csharp-net-core-tutorial/), [extracting](https://ironsoftware.com/csharp/zip/blog/using-ironzip/csharp-extract-zip-file-tutorial/), and [adding](https://ironsoftware.com/csharp/zip/examples/add-files-to-zip/) files to existing archives. The library supports popular formats such as ZIP, TAR, GZIP, and BZIP2, making it a versatile choice for handling compressed data in C# projects.

Before we begin unzipping files with IronZip, we need to Install the IronZip Library in our C# Project. Create or Open existing Projects in Visual Studio. The Project can be of any type. I am using Console Application Project to keep the tutorial simple. The same code will work for any type of C# or .NET Project.

##Install IronZip Library:

To install IronZip via the Package Manager Console in Visual Studio, follow the following steps:

1. Launch Visual Studio and open your C# project.
2. Navigate to the "View" menu and select "Other Windows" > "Package Manager Console." This will open the Package Manager Console at the bottom of the Visual Studio window.
3. In the Package Manager Console, type the following command to install IronZip:
 `Install-Package IronZip`
5. Press Enter to execute the command.

The Package Manager Console will download and install the IronZip package along with any dependencies. You'll see progress messages in the console indicating the status of the installation.

With IronZip installed in your C# project, you can now utilize its functionality for working with zip archives.

##C# Unzip files:
Let's walk through the process of unzipping a file using IronZip in C#:
###Import IronZip Namespace:
Begin by importing the IronZip namespace into your C# file.

```
using IronZip;

```
###Unzip Files in C#:

Use the ExtractArchiveToDirectory() method to extract all files from the zip archive to a specified directory.

```
public static void Main(string[] args)
{
    IronZipArchive.ExtractArchiveToDirectory("Docs.zip", "extractedDocs");
}
```
The above code extracts files from a ZIP archive named "Docs.zip" into a directory named "extractedDocs" using IronZip. Its purpose is to extract the contents of a zipped file named "Docs.zip" into a specified directory named "extractedDocs". This functionality is particularly useful for scenarios such as handling user-uploaded ZIP files, extracting resources, or managing compressed data within a C# application. By specifying the path to the ZIP archive and the target directory, developers can efficiently unzip files and organize extracted content according to their application's needs.
![File System](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tz2kc5yin046xz8ag43.png)
##Unzip Password Protected Zip file:

IronZip also provides a method for unzipping password-protected ZIP files. Simply include an additional parameter, "Password," when calling the same ExtractArchiveToDirectory() method.

```
IronZipArchive.ExtractArchiveToDirectory("Docs.zip", "extractedDocs","P@ssw0rd@123");
```

The IronZipArchive.ExtractArchiveToDirectory() method extracts the contents of the "Docs.zip" file to the "extractedDocs" directory. The additional parameter "P@ssw0rd@123" specifies the password required to access and unzip the password-protected ZIP file, ensuring secure extraction of its contents.

##Access Existing Zip File:

Let's access the existing zip archive and view all its entries without extracting the archive. The following code demonstrates how to retrieve a list of entries within the zip archive using IronZip in C#

```
string filename = "Docs.zip";
using (var archive = new IronZipArchive(filename))
{
    // Get Entries list
    List<Entry> entries = archive.Entries();
    foreach (Entry entry in entries)
    {
        Console.WriteLine(entry.Name);
    }
}
```

This above code uses IronZip to access a ZIP archive named "Docs.zip". Within a using block, it initializes an IronZipArchive object with the specified file. By calling the Entries() method, it retrieves a list of all entries (multiple files and directories) within the archive. Finally, it iterates through each entry, printing its name to the console. This approach provides a straightforward means to enumerate and process the contents of a ZIP file using IronZip in C#.

The Output is as:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hd0ni9et5n252xlctveo.png)
##Update Zip files - Replace Zip Archive Entry:

We can Update Zip files using Ironzip in a very easy and efficient way. The following code efficiently replaces a file within the provided archive and saves the modified archive under the same filename.

```
 string filename = "Docs.zip";
 using (var archive = new IronZipArchive(filename))
 {
     archive.ReplaceEntry(@"Docs/Invoice-4.png", ("new_invoice.png"));
     archive.Save();
 }
```

In the above C# code snippet, we have performed a file replacement operation within the "Docs.zip" archive. Specifically, the existing file named "Invoice-4.png" within the "Docs" directory is replaced with a new file named "new_invoice.png". Subsequently, the modified archive is saved using the same filename, ensuring the updated content is preserved within the ZIP file. This functionality allows for efficient management and manipulation of files within ZIP archives using IronZip in C#.

##Update Zip File - Delete Zip Archive Entry:

 

```
static void Main(string[] args)
 {
     string filename = "Docs.zip";
     using (var archive = new IronZipArchive(filename))
     {
         archive.Delete("Docs/Doc-1.pdf");
         archive.Save();
     }
 }
```

The above C# code snippet employs IronZip to remove a specific file, "Doc-1.pdf," from the "Docs.zip" archive. Utilizing the Delete() method within the IronZipArchive object allows for the efficient removal of unnecessary files from the archive, streamlining file management operations. The subsequent call to Save() ensures that the modifications are persisted, updating the ZIP file accordingly. This approach not only streamlines file management within ZIP archives but also facilitates maintaining archive cleanliness, reducing unnecessary clutter, and optimizing storage space.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca13myjxwcrqxjuy6l1y.png)
##Conclusion:

In conclusion, IronZip proves to be a versatile solution for managing various compressed file formats, including ZIP, RAR files, and GZ file, while offering functionality like accessing local file header. IronZip simplifies the handling of compressed data within C# applications with its comprehensive features and user-friendly API. Furthermore, IronZip's licensing model provides flexibility, offering a [free trial](https://ironsoftware.com/csharp/zip/#trial-license) option for developers to evaluate its capabilities before making a [purchase](https://ironsoftware.com/csharp/zip/licensing/), ensuring satisfaction with the product.