How to extract ZIP files with WinRAR command line? - 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.)

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.

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.

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!

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

Related

How to create mask in winrar to archive folders by name?

I have folders C:\testA01, C:\testA02 and C:\testB01, C:\testB02.
How to create a command to archive only folders with testA in their name?
"C:\Program Files\WinRAR\WinRAR.exe" a -r -u -rr8 -y C:\BACKUP\backup.rar C:\testA ?? \*.*
Windows command line interpreter does not support wildcards in folder paths. It is only possible to search for folders matching a pattern with DIR or FOR.
But take a look on text file WhatsNew.txt in program files directory of WinRAR version 5.30 or any later version. There can be read:
Folder wildcards are allowed in RAR command line in file names to
archive. For example:
rar a backup c:\backup\2015*\*
will archive all '2015*' folders in c:\backup. Use -r switch to search
for '2015*' also in c:\backup subfolders.
WinRAR v5.30 can be used on Windows XP and later Windows.
A command line for your backup task is for example:
"%ProgramFiles%\WinRAR\Rar.exe" u -cfg- -ep1 -idq -r -rr8 -y C:\BACKUP\backup.rar C:\testA*\*
For details on the used command u which is like command a with switch -u and the used switches see text file Rar.txt being the manual for the console version Rar.exe of WinRAR.
Of course you can also run from command line the GUI version, but one switch must be changed:
"%ProgramFiles%\WinRAR\WinRAR.exe" u -cfg- -ep1 -ibck -r -rr8 -y C:\BACKUP\backup.rar C:\testA*\*
The switch -ibck for running GUI version in background (minimized to system tray) is used instead of -idq which means for console version to run in quiet mode with only error messages printed into console window.
The commands and switches for the GUI version with some differences to console version are explained in help of WinRAR. Start WinRAR, click in menu Help on menu item Help topics, select tab Contents, open item Command line mode and read the pages listed below this list item.

Which commands to use to extract and reconstruct multivolume archives using .rev files on CentOS 6.x?

Which commands can be used to extract & reconstruct multivolume RAR archives with missing parts using .rev files in Centos 6.x?
I have many RAR archives split into many parts:
dbzdv1
part01.rar
part02.rar
part03.rar
...
dbzdv2
part01.rar
part02.rar
part03.rar
...
Screenshot of files of a multivolume archive http://imageshack.com/a/img633/5450/ekUKTd.png
All volumes are protected with same password.
1. I want extract with a single command all protected multivolume RAR archives
Every RAR archive is split into many parts.
2. The .rev file should be used to automatically reconstruct a missing part.
3. I shoud have folders with files inside after extraction.
folder1
folder2
folder3
I want compress with RAR with a single command all those folders into separate archives:
folder1.rar
folder2.rar
folder3.rar
4. I want get a multivolume RAR archive with no compression and automatically tested on errors if an extracted folder is very large.
folder1 --> folder1.part01.rar
folder2.part02.rar
Problems:
If I try using this command to extract a volume:
unrar e dbzdv1.part01.rar
Shortly after start in terminal I'm asked for password for every part, not just once for the entire multivolume archive. I don't want to type password for 100-500 parts.
If I try using WinRAR on Wine and open files the error Not enough memory is output.
Screenshot of files of winrar-wine error http://imageshack.com/a/img913/2331/n6LOtl.png
Introduction
I'm not using CentOS and therefore all my answers are not verified on this operating system.
WinRAR is installed usually on Windows with 3 executables:
GUI version WinRAR.exe supporting compression in RAR and ZIP format and even more common archive formats for extraction (shareware).
Console version Rar.exe supporting compression and extraction of RAR archives only (shareware).
Console application UnRAR.exe for extraction of RAR archives only (free, no license needed).
In program files folder of WinRAR there is additionally the text file Rar.txt which is the manual for the console version Rar. GUI Version WinRAR supports nearly same set of switches, but there are some differences like -af<type> supported only by GUI version. The complete list of switches for WinRAR can be found in help on page Alphabetic switches list (Contents - Command line mode - Switches). Rar and UnRAR output both a short help with listing all supported commands and switches if executed in a command prompt window without any parameter.
Extraction from several password protected multivolume archives
The switch -p[pwd] can be used on command line to extract files from a password protected archive of any type as explained for example in Rar.txt.
Examples:
UnRAR.exe x -pPasswordOfArchive MyArchive.rar
UnRAR.exe x "-p!Password Of Archive!" MyArchive.rar
This switch with the password must be enclosed in double quotes if the password contains 1 or more spaces or other special characters according to the requirements for command line strings of the script interpreter (cmd.exe on Windows, bash or sh on Linux).
Only the first file of a multivolume archive must be specified on command line to extract all files and folders from all volumes (parts) of an archive.
Example:
UnRAR.exe x -pMyPassword MyMultiVolumeArchive.part1.rar
The other files (parts) of MyMultiVolumeArchive must be in same directory as first file and all parts must be present and valid.
The manual Rar.txt contains for command rv the paragraph:
RAR reconstructs missing and damaged volumes either when using rc command or automatically, if it cannot locate the next volume and finds the required number of .rev files when unpacking.
I tested the statement about automatically using *.rev files and it is not 100% true for v5.10 on Windows on extraction for multivolume RAR archives independent on which executable is used and if the multivolume RAR4 archive is password protected, completely encrypted or not protected. A missing part is automatically reconstructed using the recovery volumes, but not a damaged part resulting in one or more checksum errors on extraction of files. It is necessary to run a reconstruct on a multivolume RAR archive with a damaged volume before extraction can be done for all files and folders successfully.
The manual Rar.txt contains near top in chapter about RAR command line syntax the paragraph:
Many RAR commands, such as extraction, test or list, allow to use wildcards in archive name. If no extension is specified in archive mask, RAR assumes .rar, so * means all archives with .rar extension. If you need to process all archives without extension, use *. mask. *.* mask selects all files. Wildcards in archive name are not allowed when archiving and deleting.
So extraction of all files and folders from all password protected multivolume RAR archives in same directory is very easy:
UnRAR.exe x -idcdp -pCommonPassword *.rar
UnRAR automatically detects all *.rar files in the directory independent on type (single or multivolume archive) and decompresses them all whereby nothing is extracted twice. So after extraction of MyMultiVolumeArchive.part1.rar it continues automatically on part 2, 3, ... and after processing this multivolume archive, continues with next archive with a different file name in same directory.
It might be useful to use additionally the switch -ad to extract all files and folders of an archive to a folder with name of archive file to avoid extraction of files from different archives into same directory.
Example:
UnRAR.exe x -idcdp -ad -pCommonPassword *.rar
But take into account that the created folder contains also .part*1 in name for a multivolume archive. So using this switch on multivolume archives does not produce nice looking folder names.
On Windows the following command can be used to remove .part*1 from all folder names:
for /F "usebackq delims=" %D in ( `dir *part* /AD /B /ON` ) do ren "%D" "%~nD"
As batch file:
#echo off
for /F "usebackq delims=" %%D in ( `dir *part* /AD /B /ON` ) do ren "%%D" "%%~nD"
It is not possible to specify several RAR archives on a command line without usage of wildcards.
A command line like
UnRAR x -pArchivePassword FirstArchive.part1.rar SecondArchive.part001.rar ThirdArchive.rar
would be interpreted as order to extract the files SecondArchive.part001.rar and ThirdArchive.rar from multivolume archive FirstArchive.
Reconstruct several password protected multivolume archives
The command rc must be used instead of x to reconstruct a multivolume archive using recovery volumes (*.rev).
This command is not supported by UnRAR, only by Rar and WinRAR.
It is not possible to reconstruct all multivolume RAR archives in a directory with a single command. If a wildcard like *.rar is used with command rc, just the first RAR archive matching the file mask will be processed for checking archive integrity and reconstruct a volume if damaged or missing.
Therefore a command line respectively shell script (*.bat, *.sh) is required to run RAR with command rc on all multivolume archives in a directory.
But it is not necessary to first test archive integrity on each multivolume archive using command t as archives with no damaged or missing volume are not modified at all on reconstruct.
Example for a single archive:
Rar.exe rc -pArchivePassword MyMultiVolumeArchive.part001.rar
Here is a Windows batch file for that task:
#echo off
setlocal EnableDelayedExpansion
for /F "usebackq delims=" %%F in ( `dir *part*1.rar /A-D /B /ON` ) do (
rem Get string of archive file name after first dot which should be
rem the dot before part*1 as otherwise this procedure would produce
rem a wrong result. In other words this procedure does not work with
rem archive file names containing an additional dot before .part*.rar.
for /F "tokens=1,2 delims=." %%M in ( "%%~nF" ) do (
set PartNumber=%%N
rem Remove all zeros from "part*1"
set PartNumber=!PartNumber:0=!
rem Remove also "part" from "part*1".
set PartNumber=!PartNumber:part=!
rem Check if part number is 1 and not 11 or 101 or 111.
if "!PartNumber!" == "1" (
echo.
echo Reconstruct archive %%M
"C:\Program Files\WinRAR\Rar.exe" rc -idcd -pArchivePassword "%%F"
)
)
)
endlocal
Create a RAR archive for each subdirectory in a directory
WinRAR has after pressing button Add in toolbar the option Put each file to separate archive on tab Files explained in help opened by pressing button Help. WinRAR creates for each selected folder and selected file in root of the current directory a separate archive with name of folder / file.
But this is a feature supported by WinRAR only on using GUI. It is not supported on command line as far as I know whether by WinRAR nor by Rar.
Therefore it is necessary to run a small command line / shell script (*.bat, *.sh) to compress each folder in a directory into a separate archive. Just a single line is necessary to achieve that at least on Windows.
Windows command line:
for /F "usebackq delims=" %D in ( `dir /AD /B /ON` ) do "C:\Program Files\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -t -v10M "%D.rar" "%D"
Windows batch file:
#echo off
for /F "usebackq delims=" %%D in ( `dir /AD /B /ON` ) do "C:\Program Files\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r-t -v10M "%%D.rar" "%%D"
The example compresses each folder recursively to an archive with same name using best compression, omitting the path to the folder in archive and creating a multivolume archive with 10 MB per volume. The created archives are additionally tested for integrity although this is usually not needed.

How do I silently install a 7-zip self-extracting archive to a specific directory?

The Ruby Devkit is a 7-zip based self-extracting archive.
I would like to invoke it silently without having to install 7-Zip to extract the files to a folder of my choosing, so that I can script the installation. I imagine it to be something like:
cmd> DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe /silent /dir="C:\DevKit"
But that, of course, doesn't work. What command line flags must I use to silently extract this archive into a folder of my choice?
try this:
C:\> DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe -o"C:\DevKit" -y
Update 2017: The tool from 7zsfx.info is now dead and gone.
Original, old post from 08-2015:
If you are trying to extract an 7zip SFX (http://7zsfx.info/) archive:
sfx.exe -y -gm2 -InstallPath="C:\\your\\target\\path"
Switches Docu
-y hide some prompts
-gm2 hides the extraction dialog completely (silent mode)
-InstallPath sets the target path (you need double backslashes)
7z SFX Guide
The official way to create a SFX package is to use -sfx[{name}] : Create SFX archive.
And that means the created SFX packages uses two kinds of CLI options:
official CLI options from 7zSFX, and
the passed through options you configured in your config, before creating the package.
You can think of it as parameter forwarding to the packaged executable. This parameter forwarding depends on the SetEnvironment and RunProgramm configuration!
The full process:
Create archive Package.7z:
containing Installer.msi and additional crap.cab file.
Create config file config.txt:
;!#Install#!UTF-8!
Title="Installation"
SetEnvironment="strInstall=hidcon:Installer.msi /qn"
RunProgram="%strInstall%"
;!#InstallEnd#!
Now we generate Test.exe by combining sfx+config+archive into an executable.
copy /b 7zS.sfx + config.txt + Package.7z SfxInstaller.exe
Note: 7zS.sfx is from the official 7zip extra package.
Now, when you run SfxInstaller.exe you can pass for instance /lv InstallerLog.txt to create a install log, e.g.
SfxInstaller.exe /lv InstallerLog.txt
Since 7-zip is used, simply create a self-extracting archive in .exe. and run it with switches -o and -y.
I use it to save space on USB drive. For instance, I run VDiskAir application infrequently. I create a self-extracting archive of the VDiskAir program folder (about 15MB):
7z a -SFX -mx9 VDiskAir.exe [VDiskAir folder path]
NB: -mx9 is used here to maximise compression.
I create a DOS BAT to run the self-extracting VDiskAir.exe (about 5MB) created, save it as VDiskAir.bat containing:
VDiskAir.exe -o%TMP% -y
%TMP%\VDiskAir\VDisk_Air.exe
I'm not worried that the VDiskAir folder (in %TMP% extracted with VDiskAir program files) is undeleted after running VDiskAir this way, since I have a BAT script to clear %TMP% on shutting down/starting up.
Below is what I use for Autodesk product:
Start /W %~dp0AutoCAD_2018_French_LP_Win_64bit_dlm.sfx.exe -suppresslaunch -d C:\Autodesk

How to partially extract a folder from a 7z file using powershell

I'm trying to automate the install of my platform. I've made a script for compressing the build of the deployables to a 7zip file.
Now i need to uncompress partially some folders to a specific destination.
Package
-app1
--folder11
---folder111
--folder12
-app2
--folder21
--folder22
...
I need to create a powershell script to extract the content of 'app1' to a destination folder.
I've been trying to use the following command but the result is not the as i expected.
I've been receiving the full path and not the content from folder11 recursivelly.
Set-Alias zip $ZipCommand
zip x $FilePath app1\folder11 -oc:DeployableFolder -r
Any ideas? Suggestions?
Thanks.
I tried and had no issue.
set-alias zip "c:\Program Files\outils\7-Zip\7z.exe"
zip x program.7z python-core-2.6.1\lib -oc:\data
I eventually got a c:\data\python-core-2.6.1 which only contains the lib folder with all its subfolders & files.
The only difference I see is the backslash \ in the output path.
HTH

Compressing only files using 7z without preserving the path

I am using 7z command line executable to zip files, but I see that while adding to an archive the path of the files is preserved in the archive.
So if I do
7z a -tzip myzip.zip dir1\dir2\*
the archive myzip.zip will contain the path dir1\dir2. I do not want this, rather I want only the files to be added to the zip file without the paths being preserved.
I searched quite a bit but do not seem to find any way of doing this, maybe I am missing something obvious?
Thanks
Just add a dot before the path, i.e.
7z a -tzip -r myzip.zip .\Relative\Dir\*
Give the full path. That should work. Not the relative path from the current location.
For example, I give the below, where I want the files in the man5 folder to be archived.
$ 7z a -tzip myzip.zip /home/pradeeban/Desktop/man4/man5/*
The zip contained only the files, without the directories.
Then I gave only the relative path. It had the directories, inside the zip.
$ 7z a -tzip myzip.zip Desktop/man4/man5/*
Tried with Linux (Ubuntu 12.04). Not sure whether that differs from Windows.
I discovered a way to do this by using a relative path:
7z a -tzip myzip.zip %CD%\dir1\dir2\*
%CD% is how you get the current path in a Windows batch file, but it also works from the command line. More info about Capturing the current directory from a batch file.
As explained in related question in 7-zip user FAQ, 7z stores paths relative to working directory, so you will need to first cd to desired top-level directory for archive and run 7-zip from here.
cd dir1\dir2\
7z a -tzip myzip.zip *
If you run it from script and don't want to affect it with changed directory, use directory push/pop facilities available in your shell of choice or run cd+7-zip in spawned process to avoid affecting your entire script with changed directory. For example, using Windows' start that would be:
start /D dir1\dir2\ /wait 7z a -tzip myzip.zip *
This worked for me
Consider folder structure like C:\Parent\SubFolders..... And you want to create parent.zip which will contain all files and folders C:\Parent without parent folder [i.e it will start from SubFolders.....]
cd /D "C:\Parent"
"7z.exe" a Parent.zip "*.*" -r
This will create Parent.zip in C:\Parent