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 using C#. We’ll also cover how to unzip password-protected or AES encrypted ZIP archives. This article is divided into the following sections:
- C# API to Unzip Files
- Unzip files in ZIP archives in C#
- Unzip password-protected ZIP files
- Extract AES encrypted ZIP files in C#
C# API to Unzip Files
Unzip ZIP Files using C#
The process of unzipping ZIP 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 in Archive 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 extract 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.
Unzip AES Encrypted ZIP Files
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#.
In this article, you have learned how to unzip ZIP files using C#. Furthermore, you have seen how to unzip files in password-protected ZIP archives. You can explore more about Aspose.ZIP for .NET using documentation.