Reputation: 10015
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);
Upvotes: 6
Views: 9679
Reputation: 2781
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.
Upvotes: 0
Reputation: 175
Use NUnrar, there is also a nuget package for easy installation.
Install-Package nunrar
NUnrar.Archive.RarArchive.WriteToDirectory(fileName,destinationfileName);
Upvotes: 0
Reputation: 15797
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
Upvotes: 3
Reputation: 7245
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.
Upvotes: 1
Reputation: 6777
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
Upvotes: 7