How to unpack the rar files

Sometime we have .rar file and we want to extract that file into some folder through C# code. Folder may contain images, text files, documants and others. You can do this by using third party .net libraries. There are many libraries available.

DotNetZip, Rebex Zip, NUrar, ZipLib.

Here to unpack the zip file I am using NUrar library which is freely available on codeplex.

You can download it from codeplex.

NUrar can unrar any single or multipart archieve. There are two ways to do this
  1. RarArchieve
  2. RarReader

Here is a sample code

For Single Archieve file

RarArchive file = RarArchive.Open(source);
foreach (RarArchiveEntry rarFile in file.Entries)
{
string path = samplePath + Path.GetFileName(entry.FilePath);
rarFile.ExtractToFile(path);
}

For Multipart Archieve file
public static void GetStreams()
{
RarArchive file = RarArchive.Open(GetMultiPartStreams());
foreach (RarArchiveEntry rarFile in file.Entries)
{
string path = Path.Combine(@"C:\", rarFile.FilePath);
using (FileStream output = File.OpenWrite(path))
{
rarFile.WriteTo(output);
}
}
}
public static IEnumerable<Stream> GetMultiPartStreams()
{
foreach (string file in Directory.GetFiles(@"C:\parts"))
{
yield return File.OpenRead(file);
}
}