If you’re looking to extract ZIP archives programmatically in C#, you’ve come to the right place! This article serves as a comprehensive guide on how to effectively handle 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.
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
- C# API to Unzip Files - Free Download
- How to Extract ZIP Files in C#
- Unzip Password-Protected ZIP Files
- Extract AES Encrypted ZIP Files in C#
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:
- Extract each file from the ZIP archive individually.
- Unzip all files into a specified folder.
C# Extract Each File in ZIP
To extract files individually while monitoring the extraction progress, follow these steps:
- Open the ZIP archive using a FileStream.
- Initialize an instance of the Archive class with the FileStream object.
- Access files within the ZIP using the Archive.Entries collection.
- Set up an ArchiveEntry.ExtractionProgressed event handler to display the extraction progress.
- Utilize the ArchiveEntry.Extract(string) method to extract files.
Here’s a code sample demonstrating how to extract files from a ZIP archive in C#:
Unzip ZIP Files into a Folder in C#
If you prefer to unzip all files into a specific folder, follow these steps:
- Open the ZIP archive using the FileStream class.
- Create an instance of the Archive class initialized with the ZIP’s FileStream object.
- Use the Archive.ExtractToDirectory(string) method to unzip files into the designated folder.
Here’s a code sample for unzipping ZIP files into a folder:
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.
Here’s a sample code snippet for unzipping a password-protected ZIP file:
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#:
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.