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:

WindowsZip "c:\test.txt", "c:\"

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:

WindowsUnZip "c:\", "c:\"

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.

Function WindowsUnZip(sUnzipFileName, sUnzipDestination)
  'This script is provided under the Creative Commons license located
  'at . It may not
  'be used for commercial purposes with out the expressed written consent
  Set oUnzipFSO = CreateObject("Scripting.FileSystemObject")
  If Not oUnzipFSO.FolderExists(sUnzipDestination) Then
  End If

  With CreateObject("Shell.Application")
       .NameSpace(sUnzipDestination).Copyhere .NameSpace(sUnzipFileName).Items
  End With

  Set oUnzipFSO = Nothing
End Function

Function WindowsZip(sFile, sZipFile)
  'This script is provided under the Creative Commons license located
  'at . It may not
  'be used for commercial purposes with out the expressed written consent

  Set oZipShell = CreateObject("WScript.Shell")  
  Set oZipFSO = CreateObject("Scripting.FileSystemObject")
  If Not oZipFSO.FileExists(sZipFile) Then
  End If

  Set oZipApp = CreateObject("Shell.Application")
  sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count

  aFileName = Split(sFile, "\")
  sFileName = (aFileName(Ubound(aFileName)))
  sDupe = False
  For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
    If LCase(sFileName) = LCase(sFileNameInZip) Then
      sDupe = True
      Exit For
    End If
  If Not sDupe Then
    oZipApp.NameSpace(sZipFile).Copyhere sFile

    'Keep script waiting until Compressing is done
    On Error Resume Next
    sLoop = 0
    Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
      sLoop = sLoop + 1
    On Error GoTo 0
  End If
End Function

Sub NewZip(sNewZip)
  'This script is provided under the Creative Commons license located
  'at . It may not
  'be used for commercial purposes with out the expressed written consent

  Set oNewZipFSO = CreateObject("Scripting.FileSystemObject")
  Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
  oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
  Set oNewZipFSO = Nothing

End Sub


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