Zip and UnZip Files Using the Windows Shell and VBScript

Zipping and UnZipping files is a very common task in Windows. Probably one of the more popular search terms that bring people to my site. I have already written two articles on how to zip and unzip files in Windows.

The first article discussed how to zip and unzip using 7Zip. 7Zip is a popular open source Windows application. This is my personal favorite because it is free and open source, and it also has one of the best compression algorithms so you get nice small files and compression time is totally configurable.

The second article I wrote was how to zip and unzip using WinZip. I wrote this article because I encountered a client that had already paid for and deployed a copy of WinZip enterprise wide and wanted to utilize it. This happens to be my least favorite way because it’s closed source, it’s not free, and you have to install it, it’s obviously not built into the framework of Windows.

Well recently I encountered another situation where I couldn’t install new software (it simply wasn’t practical) but still needed to zip and unzip files. I looked into it a bit and found that you can utilize Windows built in zip and unzip functionality through the shell’s “copyhere” method.

It’s not quite as straight forward as the other two methods. For one there isn’t a way to create a new zip file right from the shell. So if you want to zip a file, you first have to create an empty zip file and then copy the files you want to compress into the existing zip. It also doesn’t have configurable compression methods. You have normal compression and if you want “store”, “fast” or “ultra” type compression you’ll have to use a third party utility.

The one great advantage it does have is being built right into Windows 2003, 2008, XP and Vista. Since older versions of Windows like 2000 and NT obviously don’t have that functionality built into the shell, you can’t use these scripts with them. You’ll have to settle for one of the two methods that I outlined above, or come up with some other method yourself.

So, here are a few examples of how to use these functions:

If the file “” doesn’t exist, it’ll be created by the “NewZipFile” sub. If the zip file does exist, the file will simply be added into the existing zip. If for some reason the file you’re trying to compress already exists in the zip, the default behavior is to skip re-zipping it since it already exists.
Same syntax for the “WindowsUnZip” function:

This will simply unzip the contents of the into the root of the c drive.
As always please feel free to reply with questions or comments.

Note: I just found out that the Windows shell has a file size limitation of around 2gb. If your source file is too big you will receive the following error:

“The compression cannot be performed because the size of the resulting Compressed (zipped) Folder is too large.”

This error appears to be totally undocumented in any of Microsoft’s (or even Google) documentation. Something to consider if you decide to implement this globally. 7Zip does not have this limitation so you may consider using that instead.

Update: This function hangs with a prompt to overwrite files if the destination files exist when unzipping. You many want to ensure that the directory you unzip to contains no duplicates or simply unzip to a new folder.


5 thoughts on “Zip and UnZip Files Using the Windows Shell and VBScript

  1. Good post.
    But be aware that the file names passed to the functions cannot be relative.
    Otherwise get errors on the NameSpace call.

    outFile = “”
    inFlr = “InFldr”
    WindowsZip inFldr outFile ‘ fails

    fix by:
    Set fso = CreateObject(“Scripting.FileSystemObject”)
    outFileA = fso.GetAbsolutePathName(outFile)
    inFldrA = fso.GetAbsolutePathName(inFldr)
    WindowsZip inFldrA outFileA ‘ ok

  2. i use this code, when i use this as stand alone program then work perfectly but when i use this code with my other program then display error Object variable or With block variable not set.
    I check code multiple times but result is same
    so can you help about this error

    • If you email me your code I’ll take a look. But you haven’t provided enough information for anyone to help you. You might also find some luck at stackoverflow.

      • i use this code as same u wrote , only file name and destination address change which i use with variable, it display error code 91 means “Object variable or With block variable not set”, when i view in debug view it error on this line code sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count, it create zip file but not add file in zip which i want.
        how can i solve this?

Leave a Comment