user34537
user34537

Reputation:

How to extract ZIP files with WinRAR command line?

While trying to extract zip files I get the error:

c:\path\name.zip is not RAR archive
No files to extract

My code is:

p.StartInfo.FileName = @"C:\Program Files\WinRAR\rar.exe";
p.StartInfo.Arguments = string.Format("x -o- {2} \"{0}\" * \"{1}\"\\ ",
  szFN,
  outFolder,
  passWord == null ? "" : string.Format("-p\"{0}\"", passWord));

The GUI version can extract zip and 7z files.

Why doesn't this work? How can I extract zip and 7z files?

(NOTE: I have different source code for 7zip. I guess I can merge the two and only use the above when the file has a rar extension. But I don't like that solution.)

Upvotes: 18

Views: 91684

Answers (4)

Stefano Carfagna
Stefano Carfagna

Reputation: 109

for /f "tokens=*" %G in ('dir /on /b "D:\BACKUP_DATI\EXCEL\OPER*.ZIP"') do "C:\Program Files\7-Zip\7z.exe" x  "..\%G" –aoa

References to further reading:

Upvotes: 0

user2874632
user2874632

Reputation: 481

Free unrar.exe and console versionRar.exe of WinRAR support only RAR archive format. That is clearly described in second paragraph in manual for Rar.exe which is the text file Rar.txt in program files folder of WinRAR.

You need to use WinRar.exe instead which supports also other archive formats:

[path\winrar.exe] x [switches] [path to zip file] [files to extract, . for all files] [path folder to extract to]

Example:

"%ProgramFiles%\WinRAR\winrar.exe" x -ibck c:\file.zip *.* c:\folder\

The syntax, commands and switches for GUI version WinRAR.exe are listed and described in help of WinRAR. Click in menu Help on menu item Help topics, open on help tab Contents the item Command line mode and read the help pages listed under this item.

For example the switch -ibck supported only by WinRAR.exe but not by Rar.exe is for running the extraction in background which means GUI version of WinRAR makes the extraction minimized to an icon in Windows system tray.

Upvotes: 48

Writwick
Writwick

Reputation: 2163

You can use either SevenZipSharp or DotNetZip Library in your Application!

But I will go for SevenZipSharp Lib as It supports all the archives supported by 7-Zip!

Both Source and Binary are available in the Links!

Upvotes: 1

Mick
Mick

Reputation: 81

rar.exe can indeed only unpack rar files. It's not at all the same as WinRAR.

For unpacking ZIP files in .NET, you might want to look at the DotNetZip library instead. It has a license compatible with commercial software, unlike CSharpZipLib.

If you need to support RAR as well, you can use UnRAR.dll with pinvoke:
http://www.rarlab.com/rar_add.htm
http://www.rarlab.com/rar/UnRARDLL.exe

Or this .NET unRAR libary:
http://www.chilkatsoft.com/rar-dotnet.asp

Perhaps this one for 7zip.

Upvotes: 5

Related Questions