0

I'd like to include a Process.Start line in my c# code that extracts a single file from an archive. In particular I'm looking for what the command line execution looks like.

I.E. I have an archive Test.rar which has the file picture.png as well as a bunch of other files. How do I get picture.png to a destination of my choice?

Thanks.

fsnot
  • 1
  • 1
  • 1

2 Answers2

1

Use unrar.exe like this:

unrar.exe x test.rar C:\Destination

Process process = new Process();
process.StartInfo.FileName = "unrar.exe";
process.StartInfo.Arguments = "x test.rar C:\Destination";
process.Start();
process.WaitForExit();
Arsen
  • 965
  • 8
  • 7
  • Does this unrar the entire file? I just want one file from the archive as unrar'ing the entire file would take too long. – fsnot May 03 '13 at 23:21
  • You can use n to specify that you only want the file called file. unrar.exe x npicture.png test.rar C:\Destination. Run unrar.exe without parameters in command prompt to see all the options. – Arsen May 03 '13 at 23:24
  • Awesome! That's just what I needed. Thanks for the tip to run unrar in cmd. I honestly didn't think of it ): What about if there are multiple files with the same name in the archive? would it just be n? – fsnot May 03 '13 at 23:37
  • what if sfx file have password?? – AminM Mar 30 '14 at 05:33
1

Just kick off the process passing it the appropriate arguments. After you can deal with the file like you would any other.

Process process = new Process();
process.StartInfo.FileName = @"C:\MyPathToWinRar\winnrar.exe";
process.StartInfo.Arguments = @"unrar x c:\yourfile.rar fileToExtract.png c:\extractfolder\";
process.Start();
process.WaitForExit();

For more info on the winrar args go here; http://comptb.cects.com/2503-using-the-winrar-command-line-tools-in-windows

P.S. There are some libraries for this if you decide not to use Process. https://stackoverflow.com/questions/11737/net-library-to-unzip-zip-and-rar-files

Community
  • 1
  • 1
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • Thanks a lot! This is what I was looking for. if there were several files of the same name in different locations within the rar file's directory would I just use: "unrar x c:\yourfile.rar location\fileToExtract.png c:\extractfolder\"? – fsnot May 03 '13 at 23:32
  • @user2348593 I believe so however you'll have to find out by trying it cause I just read the docs and don't have winrar on comp I'm working on. – evanmcdonnal May 03 '13 at 23:47