Extract RAR Files in C#

Introduction

RAR archives are a popular choice for compressing and bundling multiple files into a single package. In this guide, we will show you how to extract RAR files in C#, including password-protected and encrypted RAR archives. By using Aspose.ZIP for .NET, you can easily extract and manage both RAR4 and RAR5 archives efficiently.


Table of Contents

  1. Setting Up C# RAR Extraction
  2. Extract a Specific File from RAR
  3. Extract All Files from RAR Archive
  4. Extract a File from Password-Protected RAR
  5. Extract All Files from Password-Protected RAR
  6. Get a Free API License
  7. Conclusion and Additional Resources

1. Setting Up C# RAR Extraction

To get started with extracting files from RAR archives in C#, you’ll need to install Aspose.ZIP for .NET. This powerful library supports both RAR4 and RAR5 formats, including encrypted archives.

Installation

You can install the library via NuGet with the following command:

PM> Install-Package Aspose.Zip

Alternatively, you can download it directly from the Aspose Downloads Page.


2. Extract a Specific File from RAR

To extract a single file from a RAR archive, follow these steps:

  1. Load the RAR archive using the RarArchive class.
  2. Select the specific file you want to extract.
  3. Save the extracted file to your desired location.

Code Example

using (RarArchive archive = new RarArchive("input.rar"))
{
    RarArchiveEntry entry = archive.Entries["example.txt"];
    entry.Extract("output_folder/example.txt");
}

This method allows you to extract a single file from the RAR archive effectively.


3. Extract All Files from RAR Archive

To extract all files from a RAR archive, simply follow these steps:

  1. Load the RAR file.
  2. Specify the target directory for extraction.

Code Example

using (RarArchive archive = new RarArchive("input.rar"))
{
    archive.ExtractToDirectory("output_folder/");
}

This approach will extract all files from the archive into the specified directory.


4. Extract a File from Password-Protected RAR

When dealing with a password-protected RAR archive, you can extract a specific file by following these steps:

  1. Load the encrypted RAR archive.
  2. Provide the correct password.
  3. 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 efficiently.


5. Extract All Files from Password-Protected RAR

To extract all files from a password-protected RAR archive, follow these steps:

  1. Load the encrypted RAR file.
  2. Enter the correct password.
  3. Extract all files to your desired output folder.

Code Example

using (RarArchive archive = new RarArchive("protected.rar", "your_password"))
{
    archive.ExtractToDirectory("output_folder/");
}

This method ensures that you extract all encrypted files while preserving their original structure.


6. Get a Free API License

To unlock the full features of Aspose.ZIP, you can request a free temporary license.

For comprehensive documentation, visit the Aspose.ZIP Guide or engage with the community in the Aspose Forum for any queries.


7. Conclusion and Additional Resources

Summary

In this guide, we 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 for just $99!