Zip Files with VBScript and WinZip

A couple of months ago I posted an entry about zipping and unzipping files with 7zip and VBScript. The script works well and I use it quite frequently and I definitely recommend it over WinZip if you have a choice.

I ran into a situation for a client where they needed a scripting solution but didn’t want to use open source software because of a lack of support options and reluctance to move to an open source software model. Well in this situation I had to make due with what I had and I whipped up a script to zip files using WinZip. Sorry no UnZip yet, if someone really needs it feel free to send me an email or leave a comment.

This particular function relies on WZZIP.exe which is the command line equivalent of WinZip’s gui interface. There are lots of command line switches you could add in if you desired other behavior. For example you can select to delete the files after they have been successfully compressed or select different levels of compression if you desired speed over minimum file size.

Anyway here is the function to zip files using WinZip instead of 7Zip.

Function Zip(sFile,sArchiveName)
  'This function executes the command line
  'version of WinZip and reports whether
  'the archive exists after WinZip exits.
  'If it exists then it returns true. If
  'not it returns an error message.

  '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 oFSO = WScript.CreateObject("Scripting.FileSystemObject")
  Set oShell = WScript.CreateObject("Wscript.Shell")

  '--------Find Working Directory--------
  aScriptFilename = Split(Wscript.ScriptFullName, "\")
  sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
  sWorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")

  '-------Ensure we can find WZZIP.exe------
  If oFSO.FileExists(sWorkingDirectory & " " & "WZZIP.EXE") Then
    sWinZipLocation = ""
  ElseIf oFSO.FileExists("C:\program files\WinZip\WZZIP.EXE") Then
    sWinZipLocation = "C:\program files\WinZip\"
    Zip = "Error: Couldn't find WZZIP.EXE"
    Exit Function
  End If

  oShell.Run """" & sWinZipLocation & "wzzip.exe"" -ex -r -p -whs -ybc """ & _
  sArchiveName & """ """ & sFile & """", 0, True  

  If oFSO.FileExists(sArchiveName) Then
    Zip = 1
    Zip = "Error: Archive Creation Failed."
  End If
End Function


Leave a Comment