If you’re looking to extract ZIP archives programmatically in C#, you’re in the right place! This article serves as a comprehensive guide on effectively handling ZIP file extraction using the .NET Archive Extraction Library. We will explore various methods for unzipping files, including how to manage password-protected archives and AES encryption.

Extract ZIP Files in C#

In our previous article on creating ZIP files, we discussed different techniques for packaging files using Aspose.ZIP for .NET. Now, let’s dive into unzipping ZIP files and extracting files from both password-protected and AES encrypted ZIP archives in C#.

Table of Contents

Extract ZIP Archives in C# - API Installation

Before we get started, ensure that you have downloaded and referenced Aspose.ZIP for .NET. You can also install the package via the NuGet Package Manager. To add the library to your project, run the following command:

PM> NuGet\Install-Package Aspose.Zip

How to Extract ZIP Files in C#

Extracting ZIP files can be accomplished in two primary ways:

  1. Extract each file from the ZIP archive individually.
  2. Unzip all files into a specified folder using .NET Core Zip.

C# Extract Each File in ZIP

To extract files individually while monitoring the extraction progress, follow these steps:

Here’s a code sample demonstrating how to extract files from a ZIP archive in C#:

// Open ZIP file
using (FileStream zipFile = File.Open("compressed_files.zip", FileMode.Open))
{
using (Archive archive = new Archive(zipFile, new ArchiveLoadOptions()))
{
// Access each entry in ZIP archive
for (int i = 0; i < archive.Entries.Count; i++)
{
int percentReady = 0;
// Log extraction progress to the console.
archive.Entries[i].ExtractionProgressed += (s, e) =>
{
int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize);
if (percent > percentReady)
{
Console.WriteLine(string.Format("{0}% decompressed", percent));
percentReady = percent;
}
};
// Extract entry's content to disk.
archive.Entries[i].Extract(archive.Entries[i].Name);
}
}
}

Unzip ZIP Files into a Folder in C#

If you prefer to unzip all files into a specific folder, follow these steps:

Here’s a code sample for unzipping ZIP files into a folder:

// Open ZIP file
using (FileStream zipFile = File.Open("compressed_files.zip", FileMode.Open))
{
using (var archive = new Archive(zipFile))
{
// Unzip files to folder
archive.ExtractToDirectory("Unzipped Files");
}
}

C# Unzip Password-Protected ZIP Files

You can extract password-protected ZIP archives using Aspose.ZIP for .NET. Simply specify the password using the ArchiveLoadOptions class, which you will pass as the second parameter to the Archive’s constructor. For instance, to C# Unzip File with Password, refer to the following example.

Here’s a sample code snippet for unzipping a password-protected ZIP file:

// Open ZIP file
using (FileStream zipFile = File.Open("compressed_files.zip", FileMode.Open))
{
// Decrypt using password
using (var archive = new Archive(zipFile, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
// Extract files to folder
archive.ExtractToDirectory("Unzipped Files");
}
}

Extract AES Encrypted ZIP Files in C#

If your ZIP archive is encrypted with AES, Aspose.ZIP for .NET supports AES128, AES192, and AES256 encryption methods. Extracting an AES encrypted ZIP file is similar to unzipping a password-protected archive; you only need to provide the decryption password using the ArchiveLoadOptions class.

Here’s how to extract AES encrypted ZIP files in C#:

// Open ZIP file
using (FileStream zipFile = File.Open("encrypted.zip", FileMode.Open))
{
// Decrypt and extract to folder
new Archive(zipFile, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }).ExtractToDirectory("decrypted");
}

C# ZIP Extraction API - Get a Free License

You can perform ZIP extraction without any evaluation limitations by obtaining a free temporary license.

Conclusion

In this article, we’ve covered how to unzip ZIP files using C# and tackled the extraction of password-protected ZIP archives. Additionally, we explored how to handle encrypted ZIP files. For more information on using Aspose.ZIP for .NET, check out the documentation.

This guide is your go-to resource for C# .NET 6 Zip File Extraction, C# .NET 7 Zip File Extraction, and more. Whether you are working with .NET Core Zip file extraction to specific folder or implementing .NET Zip File Decompression with Error Handling, the techniques discussed here will enhance your ZIP extraction capabilities in .NET.

To further assist you, this guide includes information on C# Unzip File, C# Unzip File to Folder, and C# Decompress ZIP. You will also learn about C# Open ZIP, C# Unpack ZIP, and how to Extract ZIP without Password. If you’re interested in working with password-protected archives, we will cover how to Retrieve ZIP Password and Open Encrypted ZIP File. Follow these instructions to effectively manage your ZIP files in C#.

Furthermore, if you want to know how to .NET Unzip files or perform C# Archive operations, this guide provides all the necessary information you need to get started.

More in this category