How to unzip a file using the cmd?

12

5

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

Posted 2018-04-16T06:42:16.463

Reputation: 123

https://stackoverflow.com/a/26843122/4568534 This answer shows yoyu how to unzip a file – Raghu Ranganathan – 2018-04-16T06:50:22.943

1if you have PowerShell 5 (builtin in Windows 10) then you could use powershell -command "Expand-Archive C:\foo\bar.zip C:\somewhere" – SimonS – 2018-04-16T08:39:05.463

Answers

7

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 S

Posted 2018-04-16T06:42:16.463

Reputation: 6 800

14

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

Posted 2018-04-16T06:42:16.463

Reputation: 141

What about older windows version? – Anthony Kong – 2019-07-09T03:41:26.060

4

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

venimus

Posted 2018-04-16T06:42:16.463

Reputation: 382

2Luckily, it’s bsdtar. Because .zip is not GZip (-z). – Daniel B – 2019-08-20T13:27:41.970

It does work well with .gz and .zip – venimus – 2019-08-21T10:03:39.317

2Yes. Because it ignores the incorrect -z argument and always uses autodetection. – Daniel B – 2019-08-21T10:50:32.043

Thanks, I removed it. I thought that with -z is to call the gzip submodule, which in turn does the autodetect. – venimus – 2019-08-23T07:26:44.963

0

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.

spoorthi vaidya

Posted 2018-04-16T06:42:16.463

Reputation: 1