182

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

8
  • 1
    A quick Google search turned up this. 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, 2009 at 12:58
  • 27
    Unless you're running a really old version of windows (ME or earlier), the windows command line is not DOS. :) Jun 20, 2009 at 13:15
  • The easyest way to unzip a file is using unzip.exe. Example: unzip.exe source.zip -d target_dir. URL: stahlworks.com/dev/index.php?tool=zipunzip
    – zappee
    Mar 21, 2018 at 12:48
  • 4
    Windows with Powershell5 can do that natively with Exand-Archive and Compress-Archive. Honour where honour is due.
    – LosManos
    Dec 18, 2018 at 20:02
  • 1
    unzip [filename].zip Dec 31, 2018 at 16:59

9 Answers 9

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]
4
  • 42
    A hint. You need the JDK, and not just the JRE if you want to use jar.
    – Qben
    Oct 31, 2012 at 8: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, 2013 at 10:27
  • @mikus: why not, just use the command <code>jar.exe xvf Compressed.zip</code>
    – Xinus
    Jun 27, 2014 at 10:27
  • 1
    java.io.FileNotFoundException: test.zip (The system cannot find the file specified)
    – markj
    Apr 2, 2015 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/

5
  • 11
    The first URL listed is a true 16-bit application, thus does not work on x64 operating systems. Just a heads up. Sep 11, 2013 at 4:42
  • 53
    if you're on Windows 7 or 10 with powershell you can use: powershell.exe -NoP -NonI -Command "Expand-Archive '.\file.zip' '.\unziped\'"
    – AK_
    Mar 17, 2018 at 21:11
  • @AK_ what about Windows 8 or 8.1?
    – Frank Vu
    Jul 19, 2018 at 9:29
  • powershell commandlet Expand-Archive needs .net 4.5 on windows 7 sp1
    – WestFarmer
    Aug 7, 2020 at 8:58
  • 1
    in newer windows version you can use tar - very simple
    – Aviko
    Aug 22, 2021 at 13:06
45

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

7z.exe x myarchive.zip
1
  • 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" Apr 8, 2018 at 0: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
0
23

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

2
  • Please, how do i add verbose to it, so it will say where the files were saved? Mar 2, 2022 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. Mar 2, 2022 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.
0
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\
1
  • doesnt run on win64 Feb 9, 2021 at 11:56
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.

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