How to extract ZIP files with WinRAR command line?

微笑、不失礼 提交于 2019-12-03 15:39:13

问题


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.)


回答1:


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.




回答2:


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.




回答3:


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!




回答4:


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:

  • 7-Zip command line examples
  • FOR /D - Loop through directory


来源:https://stackoverflow.com/questions/1315662/how-to-extract-zip-files-with-winrar-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!