183

Which commands can be used via the command line to unzip a file?
Preferably something built into Windows or open source/free tools.

Chris Sainty
  • 7,861
  • 2
  • 37
  • 59
  • 27
    Unless you're running a really old version of windows (ME or earlier), the windows command line is not DOS. :) – Tyler McHenry Jun 20 '09 at 13:15
  • 1
    A quick Google search turned up [this](http://dotnetperls.com/7-zip-examples). That works in windows (it's not clear whether you mean that you're using Windows, or the OS is actually DOS). – Jeremy Jun 20 '09 at 12:58
  • The easyest way to unzip a file is using unzip.exe. Example: unzip.exe source.zip -d target_dir. URL: http://stahlworks.com/dev/index.php?tool=zipunzip – zappee Mar 21 '18 at 12:48
  • If you have Python installed, it's a [three-line operation](https://stackoverflow.com/a/36662770/1143274). – Evgeni Sergeev Jul 23 '18 at 20:46
  • 4
    Windows with Powershell5 can do that natively with `Exand-Archive` and `Compress-Archive`. [Honour where honour is due.](https://stackoverflow.com/a/40228216/521554) – LosManos Dec 18 '18 at 20:02
  • 1
    unzip [filename].zip – Amranur Rahman Dec 31 '18 at 16:59
  • [How to unzip a file using the cmd?](https://superuser.com/q/1314420/) - This thread on superuser seems to be much updated. – brc-dd Mar 19 '21 at 13:13
  • in newer windows version you can use tar - very simple – Aviko Aug 22 '21 at 13:06

9 Answers9

234

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]
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
Brian
  • 13,412
  • 10
  • 56
  • 82
  • 42
    A hint. You need the JDK, and not just the JRE if you want to use jar. – Qben Oct 31 '12 at 08:37
  • 1
    it is weird,m jar actually solves the problem partially, cause it is able to list the files inside the archive and extract specified files, however, it is not possible to extract all the files at the same time... pretty weird – mikus Aug 19 '13 at 10:27
  • @mikus: why not, just use the command jar.exe xvf Compressed.zip – Xinus Jun 27 '14 at 10:27
  • 1
    java.io.FileNotFoundException: test.zip (The system cannot find the file specified) – markj Apr 02 '15 at 10:19
57

You could use :

http://membrane.com/synapse/library/pkunzip.html

or

7zip: http://www.7-zip.org/download.html

Free byte zip: http://www.freebyte.com/fbzip/

or infozip: http://infozip.sourceforge.net/

Red33mer
  • 810
  • 3
  • 15
  • 22
46

7-Zip, it's open source, free and supports a wide range of formats.

7z.exe x myarchive.zip
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
ninesided
  • 23,085
  • 14
  • 83
  • 107
  • 1
    if an the extracted contents already exist, and you want to override the existing contents without being prompted, then use "7z.exe x -y myarchive.zip" – Suketu Bhuta Apr 08 '18 at 00:36
42

Firstly, 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
Ross Patterson
  • 9,527
  • 33
  • 48
Justin Godden
  • 557
  • 5
  • 3
25

As others 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

http://www.7-zip.org/download.html

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

https://sevenzip.osdn.jp/chm/cmdline/commands/add.htm

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: http://www.7-zip.org/download.html

(current (at time of writing) direct link) http://sourceforge.net/projects/sevenzip/files/7-Zip/9.20/7za920.zip/download

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

http://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Please, how do i add verbose to it, so it will say where the files were saved? – Raul Chiarella Mar 02 '22 at 20:33
  • You can only add (command line arguments) that are available. Follow the 2 different breadcrumbs I gave in my answer. (labeled "FULL DOCUMENTATION ___ HERE" where ____ is a/Add or e/Extract. – granadaCoder Mar 02 '22 at 20:46
11

Thanks 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.
Sebastian
  • 6,293
  • 6
  • 34
  • 47
Justin Godden
  • 557
  • 5
  • 3
6

Originally 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:

http://www.pkware.com/software-pkzip/dos-compression

The actual command line could look something like this:

C:\>pkunzip c:\myzipfile.zip c:\extracttothisfolder\
Zombo
  • 1
  • 62
  • 391
  • 407
Michiel
  • 2,624
  • 1
  • 18
  • 14
3

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

gimel
  • 83,368
  • 10
  • 76
  • 104
1

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
Scott
  • 21,211
  • 8
  • 65
  • 72