96

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
  • 1,063

6 Answers6

149

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
  • 2,488
57

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
17

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 Fohr
  • 1,616
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.

2

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}
2

To unzip all files in the current directory in one command, using Powershell:

Get-ChildItem *.zip | Expand-Archive -DestinationPath .\Extracted -Force
Karobwe
  • 129