Extract ZIP file using VBScript - Windows

Problem:

Windows does not ship with a command line utility to extract ZIP files

If you are trying to unzip something in a bigfix action without any extra downloads or dependencies, then the only real solution is to use VBScript to extract the files.

Solutions:

Example ActionScript:

// Extract ZIP file using VBScript
delete __createfile

createfile until END_OF_FILE

' https://github.com/jgstew/tools/blob/master/VBS/zipExtractSimple.vbs

strOutputDir   = "{ pathname of folders "__Download" of client folders of current sites }"

strZipFilePath = "{ pathname of files whose(name of it as lowercase ends with ".zip") of folders "__Download" of client folders of current sites }"
'
CreateObject( "Shell.Application" ).NameSpace( CreateObject("Scripting.FileSystemObject").GetAbsolutePathName( strOutputDir ) ).copyHere ( CreateObject( "Shell.Application" ).NameSpace( CreateObject("Scripting.FileSystemObject").GetAbsolutePathName( strZipFilePath ) ).Items() ), 1044

END_OF_FILE

delete __Download\zipExtractSimple.vbs
copy __createfile __Download\zipExtractSimple.vbs

// Extract Now
waithidden cscript //B //Nologo __Download\zipExtractSimple.vbs

The above actionscript will only work if there is exactly 1 zip file in the downloads folder, but could be modified to handle more. The reason it only works with exactly 1 zip file is that it dynamically looks for a zip file in the downloads folder. The advantage to this approach is that this actionscript can be dropped in and used in any action that involves extracting a single zip file.

Related:

1 Like

This is awesome! Small extension to handle command-line argument for file and output:

delete __createfile
createfile until END_OF_FILE
CreateObject( "Shell.Application" ).NameSpace( CreateObject("Scripting.FileSystemObject").GetAbsolutePathName( wscript.arguments.named("out")) ).copyHere ( CreateObject( "Shell.Application" ).NameSpace( CreateObject("Scripting.FileSystemObject").GetAbsolutePathName( wscript.arguments.named("file")) ).Items() ), 1044
END_OF_FILE
delete __Download\zipExtractSimple.vbs
copy __createfile __Download\zipExtractSimple.vbs

mkdir __Download\out1
waithidden cscript //B //Nologo __Download\zipExtractSimple.vbs /file:"{pathname of download file "myarchive1.zip"}" /out:"{pathname of download folder}\out1"

mkdir __Download\out2
waithidden cscript //B //Nologo __Download\zipExtractSimple.vbs /file:"{pathname of download file "myarchive2.zip"}" /out:"{pathname of download folder}\out2"

As the vbscript is a one-liner, there is no error handling; the zip file and the output folder must both exist before running the script.

1 Like

See this one for a VBScript that does a lot more: https://github.com/jgstew/tools/blob/master/VBS/zipExtract.vbs

I intend to add a bit more error checking to this one. (see the TODO: lines near the end) I use an unnamed parameter for the zip file since it is essential, but if not provided I look for a zip file in the current folder before failing. I used a named parameter for the output folder, but I assume current directory if it is not given.


The simplified one is relying on the relevance to handle the folder output location and zip file existing.

You can set the output folder to always be the current folder by setting it to ".\", which you can see here: https://github.com/jgstew/tools/blob/master/VBS/zipExtractSimple.vbs