How to extract a rar file that contains multiple folders with overwrite in c# winform application

subirshan picture subirshan · Apr 22, 2013 · Viewed 10.2k times · Source

I want to extract a rar file to a location . The thing is , that the rar file contains 4 folders, and the extraction fails. I need to extract all files and folders in my rar to the location folder. And extract the files that doesn't exist.

What I have done so far :

Process winrar = new Process();
winrar.StartInfo.FileName = WinrarPath + @"\unrar.exe";
winrar.StartInfo.CreateNoWindow = true;
winrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
winrar.EnableRaisingEvents = true;
winrar.ErrorDataReceived += new 
DataReceivedEventHandler(winrar_ErrorDataReceived);
string src = downloadFilPath; // directory , not the file itself
string des = @"D:\"
winrar.StartInfo.Arguments = string.Format("x -o+ {0} {1}", src, des);
winrar.Start();
winrar.WaitForExit();

It works great , if the rar file contains only one folder . The problem is for a rar file that contains more than one folder in it. Maybe it will help you give me a solution for my problem.

Thanks , Shuki

Answer

Mels picture Mels · Apr 22, 2013

Use the command line switch "-o+" to specify automatic overwriting. In your example, the arguments line would become:

winrar.StartInfo.Arguments = string.Format("x -o+ \"{0}\" \"{1}\"", src, des);

However, depending on how portable and reliable you need this application to be, you might want to consider using a native .NET library for RAR and other archive handling. One such library I've used successfully in the past is SharpCompress.

From the WinRAR help file:

Switch -O[+|-] - set the overwrite mode


This switch can be used both when extracting and updating archived files. Following modes are available:

-o Ask before overwrite (default for extracting files)

-o+ Overwrite all (default for updating archived files);

-o- Skip existing files.