Which commands can be used via the command line to unzip a file?
Preferably something built into Windows or open source/free tools.
9 Answers
If you already have Java Development Kit on your PC and the bin directory is in your path (in most cases), you can use the command line:
jar xf test.zip or if not in your path:
C:\Java\jdk1.6.0_03\bin>jar xf test.zip Complete set of options for the jar tool available here.
Examples:
Extract jar file jar x[v]f jarfile [inputfiles] [-Joption] jar x[v] [inputfiles] [-Joption] 4You could use :
or
Free byte zip:
57-Zip, it's open source, free and supports a wide range of formats.
7z.exe x myarchive.zip 1Firstly, write an unzip utility using vbscript to trigger the native unzip functionality in Windows. Then pipe out the script from within your batch file and then call it. Then it's as good as stand alone. I've done it in the past for numerous tasks. This way it does not require need of third party applications, just the one batch file that does everything.
I put an example on my blog on how to unzip a file using a batch file:
' j_unzip.vbs ' ' UnZip a file script ' ' By Justin Godden 2010 ' ' It's a mess, I know!!! ' ' Dim ArgObj, var1, var2 Set ArgObj = WScript.Arguments If (Wscript.Arguments.Count > 0) Then var1 = ArgObj(0) Else var1 = "" End if If var1 = "" then strFileZIP = "example.zip" Else strFileZIP = var1 End if 'The location of the zip file. REM Set WshShell = CreateObject("Wscript.Shell") REM CurDir = WshShell.ExpandEnvironmentStrings("%%cd%%") Dim sCurPath sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") strZipFile = sCurPath & "\" & strFileZIP 'The folder the contents should be extracted to. outFolder = sCurPath & "\" WScript.Echo ( "Extracting file " & strFileZIP) Set objShell = CreateObject( "Shell.Application" ) Set objSource = objShell.NameSpace(strZipFile).Items() Set objTarget = objShell.NameSpace(outFolder) intOptions = 256 objTarget.CopyHere objSource, intOptions WScript.Echo ( "Extracted." ) ' This bit is for testing purposes REM Dim MyVar REM MyVar = MsgBox ( strZipFile, 65, "MsgBox Example" Use it like this:
cscript //B j_unzip.vbs zip_file_name_goes_here.zip 0As other have alluded, 7-zip is great.
Note: I am going to zip and then unzip a file. Unzip is at the bottom.
My contribution:
Get the
7-Zip Command Line Version
Current URL
The syntax?
You can put the following into a .bat file
"C:\Program Files\7-Zip\7z.exe" a MySuperCoolZipFile.zip "C:\MyFiles\*.jpg" -pmypassword -r -w"C:\MyFiles\" -mem=AES256 I've shown a few options.
-r is recursive. Usually what you want with zip functionality.
a is for "archive". That's the name of the output zip file.
-p is for a password (optional)
-w is a the source directory. This will nest your files correctly in the zip file, without extra folder information.
-mem is the encryption strength.
FULL DOCUMENTATION (for "a" aka "Add") HERE
There are others. But the above will get you running.
NOTE: Adding a password will make the zip file unfriendly when it comes to viewing the file through Windows Explorer. The client may need their own copy of 7-zip (or winzip or other) to view the contents of the file.
EDIT::::::::::::(just extra stuff).
There is a "command line" version which is probably better suited for this:
(current (at time of writing) direct link)
So the zip command would be (with the command line version of the 7 zip tool).
"C:\WhereIUnzippedCommandLineStuff\7za.exe" a MySuperCoolZipFile.zip "C:\MyFiles\*.jpg" -pmypassword -r -w"C:\MyFiles\" -mem=AES256 Now the unzip portion: (to unzip the file you just created)
"C:\WhereIUnzippedCommandLineStuff\7zipCommandLine\7za.exe" e MySuperCoolZipFile.zip "*.*" -oC:\SomeOtherFolder\MyUnzippedFolder -pmypassword -y -r As an alternative to the "e" argument, there is a x argument.
e: Extract files from archive (without using directory names) x: eXtract files with full paths Documentation here:
FULL DOCUMENTATION (for "e" aka "Extract") HERE
2Thanks Rich, I will take note of that. So here is the script for my own solution. It requires no third party unzip tools.
Include the script below at the start of the batch file to create the function, and then to call the function, the command is... cscript /B j_unzip.vbs zip_file_name_goes_here.zip
Here is the script to add to the top...
REM Changing working folder back to current directory for Vista & 7 compatibility %~d0 CD %~dp0 REM Folder changed REM This script upzip's files... > j_unzip.vbs ECHO ' >> j_unzip.vbs ECHO ' UnZip a file script >> j_unzip.vbs ECHO ' >> j_unzip.vbs ECHO ' It's a mess, I know!!! >> j_unzip.vbs ECHO ' >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO ' Dim ArgObj, var1, var2 >> j_unzip.vbs ECHO Set ArgObj = WScript.Arguments >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO If (Wscript.Arguments.Count ^> 0) Then >> j_unzip.vbs ECHO. var1 = ArgObj(0) >> j_unzip.vbs ECHO Else >> j_unzip.vbs ECHO. var1 = "" >> j_unzip.vbs ECHO End if >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO If var1 = "" then >> j_unzip.vbs ECHO. strFileZIP = "example.zip" >> j_unzip.vbs ECHO Else >> j_unzip.vbs ECHO. strFileZIP = var1 >> j_unzip.vbs ECHO End if >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO 'The location of the zip file. >> j_unzip.vbs ECHO REM Set WshShell = CreateObject("Wscript.Shell") >> j_unzip.vbs ECHO REM CurDir = WshShell.ExpandEnvironmentStrings("%%cd%%") >> j_unzip.vbs ECHO Dim sCurPath >> j_unzip.vbs ECHO sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") >> j_unzip.vbs ECHO strZipFile = sCurPath ^& "\" ^& strFileZIP >> j_unzip.vbs ECHO 'The folder the contents should be extracted to. >> j_unzip.vbs ECHO outFolder = sCurPath ^& "\" >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO. WScript.Echo ( "Extracting file " ^& strFileZIP) >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO Set objShell = CreateObject( "Shell.Application" ) >> j_unzip.vbs ECHO Set objSource = objShell.NameSpace(strZipFile).Items() >> j_unzip.vbs ECHO Set objTarget = objShell.NameSpace(outFolder) >> j_unzip.vbs ECHO intOptions = 256 >> j_unzip.vbs ECHO objTarget.CopyHere objSource, intOptions >> j_unzip.vbs ECHO. >> j_unzip.vbs ECHO. WScript.Echo ( "Extracted." ) >> j_unzip.vbs ECHO. 0Originally ZIP files were created with MS-DOS command line software from PKWare, the two programs were PKZIP.EXE and PKUNZIP.EXE. I think you can still download PKUNZIP at the PKWare site here:
The actual command line could look something like this:
C:\>pkunzip c:\myzipfile.zip c:\extracttothisfolder\ 1Grab an executable from info-zip.
Info-ZIP supports hardware from microcomputers all the way up to Cray supercomputers, running on almost all versions of Unix, VMS, OS/2, Windows 9x/NT/etc. (a.k.a. Win32), Windows 3.x, Windows CE, MS-DOS, AmigaDOS, Atari TOS, Acorn RISC OS, BeOS, Mac OS, SMS/QDOS, MVS and OS/390 OE, VM/CMS, FlexOS, Tandem NSK and Human68K (Japanese). There is also some (old) support for LynxOS, TOPS-20, AOS/VS and Novell NLMs. Shared libraries (DLLs) are available for Unix, OS/2, Win32 and Win16, and graphical interfaces are available for Win32, Win16, WinCE and Mac OS.
Copy the below code to a batch file and execute. Below requires Winzip to be installed/accessible from your machine. Do change variables as per your need.
@ECHO OFF SET winzip_path="C:\Program Files\WinZip" SET source_path="C:\Test" SET output_path="C:\Output\" SET log_file="C:\Test\unzip_log.txt" SET file_name="*.zip" cd %source_path% echo Executing for %source_path% > %log_file% FOR /f "tokens=*" %%G IN ('dir %file_name% /b') DO ( echo Processing : %%G echo File_Name : %%G >> %log_file% %winzip_path%\WINZIP32.EXE -e %%G %output_path% ) PAUSE