3

I want to write an extractor app to extract a lot of archived files (*.tgz and *.rar) using C#.

I can't find any universal component which support all of this archived file formats. For this reason, I'm trying to use WinRAR.exe. But in this case I have some problems. For example: I want to know if an archive did not extract successfully. And get error message and file name for review, but I cannot handle WinRAR errors in C#.

Any idea to handle WinRAR error? Or is there any universal component to extract archive files?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Kamran
  • 387
  • 1
  • 3
  • 19
  • you will need to use winrar com/interop if it exist to be able to catch any error or use any .net dll that manage compress file such as SharpZipLib. – Franck Sep 02 '14 at 10:37
  • @Franck Thanks, can you tell me more details about *winrar com/interop* ? – Kamran Sep 02 '14 at 10:44
  • you can also use http://comptb.cects.com/using-the-winrar-command-line-tools-in-windows/ and set the output to a stream in your program – Sebastian L Sep 02 '14 at 11:01
  • i am pretty sure you can catch the exit code, however i never used winrar, and have no idea if it has valid exit codes – Droa Sep 02 '14 at 11:07

2 Answers2

1

I'm thinking you could run the process and wait for it to finish.

  static public string RunAndGetOutput( string filename, string arguments )
  {
     // Start the child process.
     Process p = new Process();
     // Redirect the output stream of the child process.
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.FileName = filename;
     p.StartInfo.Arguments = arguments;
     p.Start();
     // Read the output stream first and then wait.
     string output = p.StandardOutput.ReadToEnd();
     p.WaitForExit();
     // p.ExitCode - could read this to get the exit code
     return output;
  }

Then you can either look at output as string, or return p.ExitCode, or both.

Derek
  • 7,615
  • 5
  • 33
  • 58
  • thanks but problem is winrar does not return error code in this struct ! Do you seen the winrar error dialog ? I want to handle all error that shows in error dialog ! – Kamran Sep 02 '14 at 11:43
  • Where did you get winrar from? I saw one website that talked about using "rar.exe" rather than winrar.exe to be used from the console. I don't think you want to be using something that brings up a dialog. – Derek Sep 02 '14 at 12:36
  • You may also want to look into 7-zip. It is open source, free, and quite popular. You can use the function I put above and use either p.ExitCode and/or the "output" string to check for success. – Derek Sep 02 '14 at 12:39
1

Console version Rar.exe cannot be used here as it supports only RAR archives. Extraction from ZIP, TAR and other archive formats is supported only by WinRAR.exe.

Open help of WinRAR - file WinRAR.chm in program files folder of WinRAR. Select tab Contents and open Command line mode and WinRAR exit codes and click on List of WinRAR exit codes. This list contains the values returned to the calling application which is here your application. As you can read any value greater 0 means there was a problem.

Next help of WinRAR contains also below Command line mode the list item Switches. There are several switches which are of interest for your task. Best look first on help page Alphabetic switches list.

You should call WinRAR.exe always with the switch -cfg- to avoid depending on user configuration.

The -ilog switch is the next one to use to get all error message written into a file which can be read after WinRAR finished.

But I do not really understand why somebody should use your application for extracting archives as there are already several shareware and freeware tools available. Also WinRAR is shareware. So the users of your application would need to buy a license for WinRAR.

Only unrar.dll as used by many other applications for extracting RAR archives is freeware as also console application UnRAR.exe. But with UnRAR.exe and unrar.dll it is only possible to extract RAR archives. Creating RAR archives or extracting archives of other formats are not supported by UnRAR.exe and unrar.dll.

Mofi
  • 46,139
  • 17
  • 80
  • 143