Extract RAR Files in C#

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

  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 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:

  1. Load the RAR archive using the RarArchive class.
  2. Select the specific file within the archive.
  3. 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:

  1. Load the RAR file.
  2. 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:

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


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. Provide the password.
  3. 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!