6

I want to silent extract of .rar archive. .Net haven't any Rar classes in IO.Compression, so i'd like to use cmd (it's better, than external dll). It extracts, but not in silent mode. What am I doing wrong?

const string source = "D:\\22.rar";
string destinationFolder = source.Remove(source.LastIndexOf('.'));
string arguments = string.Format(@"x -s ""{0}"" *.* ""{1}\""", source, destinationFolder);
Process.Start("winrar", arguments);
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151

5 Answers5

7

I use this bit of code myself (your code added):

const string source = "D:\\22.rar";
string destinationFolder = source.Remove(source.LastIndexOf('.'));
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "\"C:\\Program Files\\WinRAR\\winrar.exe\"";
p.StartInfo.Arguments = string.Format(@"x -s ""{0}"" *.* ""{1}\""", source, destinationFolder);
p.Start();
p.WaitForExit();

That will launch the WinRAR program in the background and return control to you once the file has completed un-rar-ing.

Hope that can help

txtechhelp
  • 6,625
  • 1
  • 30
  • 39
3

Use SharpCompress https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md there is also a nuget package for easy installation.

Install-Package sharpcompress -Version 0.22.0
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
1

Alternatively, you can embed with your application the tool called unRAR, the small executable developed by the WinRAR's developers (official download).

This utility is free, thus your users won't need to have a WinRAR license to use your application. :) It can be executed using the same way txtechhelp described.

Jämes
  • 6,945
  • 4
  • 40
  • 56
0

Use NUnrar, there is also a nuget package for easy installation.

Install-Package nunrar

NUnrar.Archive.RarArchive.WriteToDirectory(fileName,destinationfileName);
Robert
  • 5,278
  • 43
  • 65
  • 115
Gokhan Ertogan
  • 137
  • 1
  • 4
0

As Mihail Shishkov suggested it's possible to unrar files with SharpCompress library.

dotnet add package SharpCompress

Here is a basic usage example:

var archive = RarArchive.Open("/Downloads/archive.rar");

foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
    entry.WriteToDirectory("/Downloads");

I tried to use it in .NET 6 application under macOS and it works fine.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aleksei Mialkin
  • 2,257
  • 1
  • 28
  • 26