In the previous post, you have learned different ways of zipping files and folders into ZIP archives using Aspose.ZIP for .NET. In this post, I’ll show you how to extract or unzip files in ZIP archives programmatically using C#. We’ll also cover how to unzip password-protected or AES encrypted ZIP archives. This article is divided into the following sections:
- Unzip files in ZIP archives in C#.
- Unzip password-protected ZIP files in C#.
- Unzip AES encrypted ZIP files in C#.
Unzip Files using C#
The process of unzipping files could be achieved in two way:
- Unzip each file in ZIP separately.
- Unzip files into a specified folder.
In the first method, you can explicitly access and unzip each file in the ZIP archive. Furthermore, you can print the progress of the extraction process. Whereas, the second method simply unzips the files into the specified folder.
Unzip Each File Separately
The following are the steps to access and unzip files and print the extraction progress:
- Open the ZIP archive into a FileStream object.
- Create and initialize Archive‘s instance with the FileStream object.
- Access files in the ZIP using Archive.Entries collection.
- Set ArchiveEntry.ExtractionProgressed event handler to print the extraction progress.
- Extract/unzip files using ArchiveEntry.Extract(string) method.
The following code sample shows how to unzip files in a ZIP archive using C#.
Unzip Files into a Folder
The following steps are used to unzip files into a specific folder.
- Open the ZIP archive using the FileStream class.
- Create an instance of Archive class and initialize it with ZIP’s FileStream object.
- Unzip files using Archive.ExtractToDirectory(string) method.
The following C# code sample unzips files into a folder.
Unzip Password-Protected ZIP Files in C#
You can also unzip a password-protected ZIP archive using Aspose.ZIP for .NET. For this, you only need to specify the password using the ArchiveLoadOptions class that will be passed as a second parameter to the Archive‘s constructor. The following is the sample code to unzip a password-protected ZIP file using C#.
Unzip AES Encrypted ZIP Files in C#
A ZIP archive can also be encrypted with AES encryption. Aspose.ZIP for .NET supports encrypting archives with AES128, AES192, and AES256 encryption methods. Unzipping an AES encrypted ZIP file is similar to unzipping password-protected ZIP files. Just specify the decryption password using ArchiveLoadOptions class and rest will be taken care of by the API. The following code sample shows how to unzip AES encrypted ZIP files in C#.