How to Extract A RAR file selected by dialogbox

Viewed 2602

I have a code in C# to extract a zip file to a specific folder. I want to extract a RAR file from that code. I tried some other things like 7-zip, IO.Compression but it was not suitable for me. here is the code which I used to extract the zip file.

DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                ZipFile.ExtractToDirectory(openFileDialog1.FileName, "TestFolder"); 
                MessageBox.Show("ZIP file extracted successfully!");
            }

what I want, is like this. When a user selects a Zip or RAR file this code extract the respected file in the pre-specified folder. This is a windows form Application. Please help. Any help is welcome. Thanks

1 Answers

RAR is a different compression format. You should use another library to handle RAR files, since there is nothing built in for .NET. For example: http://sharpcompress.codeplex.com

From the official documentation page: "Extract all files from a Rar file to a directory using RarArchive"

using (var archive = RarArchive.Open("Test.rar"))
{
    foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
    {
        entry.WriteToDirectory("D:\\temp", new ExtractionOptions()
        {

        });
    }
}
Related