47

I'm trying to make a .bat script for windows 7 x64 to create a folder, unzip a file into that folder without having to use additional addons like 7zip or unzip. Been searching and it seemed like windows doesn't have builtins to allow unzip easily in command. Can I unzip/expand files without additional addons?

JustinBieber
  • 1,127
  • 3
  • 12
  • 16
  • can be made with jsscript/wbscript http://www.robvanderwoude.com/vbstech_files_zip.php and eventually hybrid-ed to .bat file. – npocmaka Feb 11 '14 at 14:16
  • 1
    Possible duplicate of [How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?](http://stackoverflow.com/questions/28043589/how-can-i-compress-zip-and-uncompress-unzip-files-and-folders-with-bat) – aschipfl Jan 09 '17 at 15:39
  • [Does Windows have a built-in ZIP command for the command line?](https://serverfault.com/q/39071/343888) – phuclv Apr 27 '19 at 09:39

5 Answers5

67

Try this:

@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Temp\" "c:\path\to\batch.zip"
exit /b

:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%

Revision

To have it perform the unzip on each zip file creating a folder for each use:

@echo off
setlocal
cd /d %~dp0
for %%a in (*.zip) do (
    Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa"
)
exit /b

If you don't want it to create a folder for each zip, change Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa" to Call :UnZipFile "C:\Temp\" "c:\path\to\%%~nxa"

Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • @MattWilliamson “Those are proprietary to the 7zip program.” 7z is not a proprietary format. – Rufflewind Nov 23 '16 at 04:37
  • 1
    Getting an empty file named 'echo' each run that extracts a zip folder. Fixed by adding `if exist "%~dp0echo" del /f /q "%~dp0echo"` to the end of my cmd file. – Jacques Mathieu Jul 05 '18 at 16:30
  • Incidentally, the same code can be used with VBA: `Dim o, z` : `Set o=CreateObject("Shell.Application")` : `Set z=o.Namespace(zipFile).items` : `o.Namespace(destFolder).CopyHere(z)` : `Set o=Nothing` where 'zipFile' is a complete path & zip filename and 'destFolder' is the destination path like `x:\myPath\ `. – ashleedawg Oct 05 '18 at 18:24
18

If you have PowerShell 5.0 or higher (pre-installed with Windows 10 and Windows Server 2016):

powershell Expand-Archive your.zip -DestinationPath your_destination

So code inside .ps1 file looks something like this:

Expand-Archive your.zip -DestinationPath your_destination
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Artem
  • 1,773
  • 12
  • 30
11

Here is a quick and simple solution using PowerShell:

powershell.exe -nologo -noprofile -command "& { $shell = New-Object -COM Shell.Application; $target = $shell.NameSpace('C:\extractToThisDirectory'); $zip = $shell.NameSpace('C:\extractThis.zip'); $target.CopyHere($zip.Items(), 16); }"

This uses the built-in extract functionality of the Explorer and will also show the typical extract progress window. The second parameter 16 to CopyHere answers all questions with yes.

A Person
  • 1,062
  • 9
  • 17
  • I had errors and solved them by using full path and left the extrackToThisDirectory empty, otherwise I would get Error 0x80010135: Path too long. Thanks! – Alex Seceleanu Feb 15 '21 at 15:23
  • Unfortunately this method (as it is) doesn't use "functionality of the Explorer" but the currently defined default "functionality". In my case it just opens 7zip... – Limer Feb 21 '22 at 23:42
5

Here's my overview about built-in zi/unzip (compress/decompress) capabilities in windows - How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?

To unzip file you can use this script :

zipjs.bat unzip -source C:\myDir\myZip.zip -destination C:\MyDir -keep yes -force no
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Great tool. Be careful with the -force option or you will lose all your data in the destination directory! – Alan Oct 09 '21 at 19:45
2

Another approach to this issue could be to create a self extracting executable (.exe) using something like winzip and use this as the install vector rather than the zip file. Similarly, you could use NSIS to create an executable installer and use that instead of the zip.

John
  • 3,458
  • 4
  • 33
  • 54