
Introduction
RAR archives are widely used for compressing and bundling multiple files into a single package. In this guide, we will demonstrate how to extract RAR files in C#, including password-protected and encrypted RAR archives. Using Aspose.ZIP for .NET, you can seamlessly extract and manage RAR4 and RAR5 archives.
Table of Contents
- Setting Up C# RAR Extraction
- Extract a Specific File from RAR
- Extract All Files from RAR Archive
- Extract a File from Password-Protected RAR
- Extract All Files from Password-Protected RAR
- Get a Free API License
- Conclusion and Additional Resources
1. Setting Up C# RAR Extraction
To extract files from RAR archives in C#, install Aspose.ZIP for .NET. This library supports RAR4, RAR5, and encrypted archives.
Installation
Install via NuGet:
PM> Install-Package Aspose.Zip
Alternatively, download from the Aspose Downloads Page.
2. Extract a Specific File from RAR
Follow these steps to extract a single file from a RAR archive:
- Load the RAR archive using the
RarArchive
class. - Select the specific file within the archive.
- Extract and save the file.
Code Example
using (RarArchive archive = new RarArchive("input.rar"))
{
RarArchiveEntry entry = archive.Entries["example.txt"];
entry.Extract("output_folder/example.txt");
}
This method extracts a single file from the RAR archive.
3. Extract All Files from RAR Archive
To extract all files from a RAR archive, follow these steps:
- Load the RAR file.
- Extract all files to a target directory.
Code Example
using (RarArchive archive = new RarArchive("input.rar"))
{
archive.ExtractToDirectory("output_folder/");
}
This method extracts all files from the archive to the specified directory.
4. Extract a File from Password-Protected RAR
To extract a specific file from a password-protected RAR archive, follow these steps:
- Load the encrypted RAR archive.
- Provide the password.
- Extract the desired file.
Code Example
using (RarArchive archive = new RarArchive("protected.rar", "your_password"))
{
RarArchiveEntry entry = archive.Entries["secure_file.txt"];
entry.Extract("output_folder/secure_file.txt");
}
This method unlocks and extracts a specific file from a password-protected RAR archive.
5. Extract All Files from Password-Protected RAR
To extract all files from a password-protected RAR archive, follow these steps:
- Load the encrypted RAR file.
- Provide the password.
- Extract all files to the output folder.
Code Example
using (RarArchive archive = new RarArchive("protected.rar", "your_password"))
{
archive.ExtractToDirectory("output_folder/");
}
This method extracts all encrypted files while preserving their structure.
6. Get a Free API License
To unlock full Aspose.ZIP features, request a free temporary license.
For documentation, visit the Aspose.ZIP Guide or ask questions in the Aspose Forum.
7. Conclusion and Additional Resources
Summary
This guide covered:
✅ How to extract RAR files in C#
✅ Extracting password-protected and encrypted RAR archives
✅ Handling both single and batch extractions
With Aspose.ZIP for .NET, you can efficiently extract, compress, and manage archives in your applications. Start automating RAR file processing today!