71

I want to make a BAT file that will ZIP or UNZIP a file. For zipping a file, I have found this question: Can you zip a file from the command prompt using ONLY Windows' built-in capability to zip files?

The answers given there are great and working for me, but I couldn't find any information about how to unzip the files. Like in the link, I can't assume any third-party tools (except winRAR).

Thanks ahead and sorry for English mistakes

Chen Tasker
  • 813
  • 1
  • 6
  • 4
  • https://stackoverflow.com/a/26843122/4568534 This answer shows yoyu how to unzip a file – Razetime Apr 16 '18 at 06:50
  • 7
    if you have PowerShell 5 (builtin in Windows 10) then you could use `powershell -command "Expand-Archive C:\foo\bar.zip C:\somewhere"` – SimonS Apr 16 '18 at 08:39
  • Does this answer your question? [Create .zip folder from the command line - (Windows)](https://superuser.com/questions/201371/create-zip-folder-from-the-command-line-windows) – cowlinator Apr 21 '20 at 21:52

6 Answers6

110

On Windows 10 build 17063 or later you can use tar.exe (similar to the *nix one). This is also available in the nanoserver docker container

C:\> tar -xf archive.zip

Note: zip support is not well documented

ref: https://www.freebsd.org/cgi/man.cgi?query=bsdtar&sektion=1&manpath=FreeBSD+5.3-stable

venimus
  • 1,898
  • 2
  • 15
  • 11
  • 5
    Luckily, it’s `bsdtar`. Because `.zip` is not GZip (`-z`). – Daniel B Aug 20 '19 at 13:27
  • 2
    It does work well with .gz and .zip – venimus Aug 21 '19 at 10:03
  • 3
    Yes. Because it ignores the incorrect `-z` argument and always uses autodetection. – Daniel B Aug 21 '19 at 10:50
  • Thanks, I removed it. I thought that with `-z` is to call the gzip submodule, which in turn does the autodetect. – venimus Aug 23 '19 at 07:26
  • It works perfectly for me on `Windows 10`. FYI: I am using the `tar` command from GNU `CoreUtils for Windows` http://gnuwin32.sourceforge.net/packages/coreutils.htm – Happy Jan 12 '21 at 18:34
  • After reading `tar --help`, the support for `.zip` files seems totally undocumented and also unexpected to me. Maybe it's worth mentioning this in the answer text.. – Daniel Alder May 21 '21 at 08:38
  • since it claims to be a bsdtar it is documented here https://www.freebsd.org/cgi/man.cgi?query=bsdtar&sektion=1&manpath=FreeBSD+5.3-stable however many options are missing though – venimus May 21 '21 at 12:20
  • 2
    be careful with this as it will overwrite existing files with no indication at all. `-k` can apparently be used to not overwrite, but haven't tried it. (also note that `tar --help` has more detailed help than `tar /?`) – Dave Cousineau Jun 20 '21 at 18:46
49

If you have Windows 10, you can use the much shorter Powershell equivalent

Expand-Archive -Force C:\path\to\archive.zip C:\where\to\extract\to
MegaBatchGames
  • 607
  • 1
  • 5
  • 4
16

This batch file code will help you to unzip a file.

@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Temp\" "c:\FolderName\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%

N.B. C:\Temp is folder where it stores the Extracted (UnZip) File.

And, c:\FolderName\batch.zip is source path, (where Zip files are stored).

Please, Change the Full File Path ( Drive, Folder & Zip file name), according to your need.

Rajesh Sinha
  • 8,995
  • 6
  • 15
  • 35
  • Seems like he just copied it from here https://stackoverflow.com/questions/21704041/creating-batch-script-to-unzip-a-file-without-additional-zip-tools . Shouldn't this answer be reported for plagarism, because he is not referencing the original source. – samarendra chandan bindu Dash Oct 09 '22 at 17:08
12

If you have Windows 10 (and powershell), but still want to unzip from within .bat/.cmd-file (cmd.exe), you can use

powershell -command "Expand-Archive -Force '%~dp0my_zip_file.zip' '%~dp0'"

where my_zip_file.zip is the file to be unzipped and %~dp0 points to the same folder are where the .bat/.cmd-file is (Change the folder, if needed).

Niko Föhr
  • 1,388
  • 2
  • 17
  • 25
  • I have two problems with this. spawning a powershell takes prohibitively long (about 20 seconds in my test), and it can't even extract a zip file with another extension. – Mark Jeronimus Jan 04 '21 at 16:00
  • There is no powershell in some nano containers for example. Not a valuable answer ... – Ari Nov 18 '21 at 08:26
  • 1
    Nothing in the question mentions that it needs to run in a nano container. Very useful answer for me – janv8000 Apr 03 '23 at 08:45
3
ZipFile="C:\Users\spvaidya\Music\folder.zip"
ExtractTo="C:\Users\spvaidya\Music\"




'If the extraction location does not exist create it.

Set fso = CreateObject("Scripting.FileSystemObject")

If NOT fso.FolderExists(ExtractTo) Then



 fso.CreateFolder(ExtractTo)

End If

'Extract the contants of the zip file.

set objShell = CreateObject("Shell.Application")

set FilesInZip=objShell.NameSpace(ZipFile).items

objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)

Set fso = Nothing
Set objShell = Nothing

The following vbscript can be saved as file.vbs and then run using batch script like:

file.vbs

save this in .bat file and run it.

1

I use this one-liner to extract all zip files in the current path to their own subdirectory based on their file name:

gci -Recurse -Filter *.zip |ForEach-Object {Expand-Archive -Path $_.Fullname -DestinationPath $_.BaseName -Force}
Aaron Hudon
  • 119
  • 3