Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/449b52340d441019d848e40c12b7e39b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/449b52340d441019d848e40c12b7e39b to your computer and use it in GitHub Desktop.
Unrar Files in C# | RAR Extractor and Opener Programmatically using C#
// Load input RAR file.
RarArchive archive = new RarArchive("Sample.rar");
// Unrar or extract all files from the archive
archive.ExtractToDirectory("extracted");
// Load an encrypted RAR file
RarArchive archive = new RarArchive("Data_Password.rar");
// Unrar or extract password protected files from the archive
// Specify password as String at second argument of method
archive.ExtractToDirectory("PasswordExtracted" , "Aspose");
// Load input RAR file.
using (RarArchive archive = new RarArchive("Sample.rar"))
{
// Create a file with Create() method.
using (var destination = File.Create("Extracted_File1.txt"))
{
// Open an entry from the RAR archive.
using (var source = archive.Entries[0].Open())
{
byte[] buffer = new byte[1024];
int bytesRead;
// Write extracted data to the file.
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
destination.Write(buffer, 0, bytesRead);
}
}
}
// Load the RAR file Encypted with Password.
FileInfo fi = new FileInfo("Data_Password.rar");
using (RarArchive archive = new RarArchive(fi.OpenRead()))
{
// Specify file name for the output file.
using (FileStream destination = File.Create("Password_Extracted_File1.txt"))
{
// Extract the password protected file.
archive.Entries[0].Extract(destination, "Aspose");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment