Wednesday, October 18, 2006

Zip and Unzip Files with VBScript and 7Zip

Occasionally it is really handy to zip and unzip files before you copy them around, or you want to archive them automatically for storage. I often use 7Zip for this task because it's free and open source, has a great compression ratio, and supports a ton of formats.

Since 7Zip is such a handy tool I thought I'd whip up a script to utilize it's command line version called 7za.exe so that I could call it from VBScript. You can use these functions to compress or uncompress files or folders.

If you want to use this script to compress more than one file at a time you can do this 2 distinct ways. By calling each file in the local directory like this:

sResults = Zip("file1.txt file2.txt file3.txt", "files.zip")

Or you could specify the wild card character like this:

sResults = Zip("""c:\program files\text files\*.txt""", "files.zip")

In the second example I used the double quotes because my path included spaces. Be sure to include this any time you have a file that has spaces in it's name or path. You can pass as many files as you want using these methods.

When using the unzip function, if the folder you select to extract to doesn't exist. It will be created automatically by 7za.exe.

Function Zip(sFile,sArchiveName)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com


  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 7za.exe------
  If oFSO.FileExists(sWorkingDirectory & "\" & "7za.exe") Then
    s7zLocation = ""
  ElseIf oFSO.FileExists("C:\Program Files\7-Zip\7za.exe") Then
    s7zLocation = "C:\Program Files\7-Zip\"
  Else
    Zip = "Error: Couldn't find 7za.exe"
    Exit Function
  End If
  '--------------------------------------

  oShell.Run """" & s7zLocation & "7za.exe"" a -tzip -y """ & sArchiveName & """ " _
  & sFile, 0, True  

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

Function UnZip(sArchiveName,sLocation)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com


  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 7za.exe------
  If oFSO.FileExists(sWorkingDirectory & "\" & "7za.exe") Then
    s7zLocation = ""
  ElseIf oFSO.FileExists("C:\Program Files\7-Zip\7za.exe") Then
    s7zLocation = "C:\Program Files\7-Zip\"
  Else
    UnZip = "Error: Couldn't find 7za.exe"
    Exit Function
  End If
  '--------------------------------------

  '-Ensure we can find archive to uncompress-
  If Not oFSO.FileExists(sArchiveName) Then
    UnZip = "Error: File Not Found."
    Exit Function
  End If
  '--------------------------------------

  oShell.Run """" & s7zLocation & "7za.exe"" e -y -o""" & sLocation & """ """ & _
  sArchiveName & """", 0, True
  UnZip = 1
End Function
Send this to:                          

Comments

unify said...

look here too:
http://mo8blog.wordpress.com/

3/5/2007 9:00:26 AM

Sudhanshu said...

Nice script, one small observation though- the command line utility is 7z.exe and not 7za.exe as in your script.

8/13/2007 12:26:01 AM

Nathan+Rice said...

It has changed recently (with in the past few minor revisions). Renaming the reference throughout the script will be enough to make this work with the newer revisions of 7zip. Thanks for the comment though.

8/13/2007 8:34:36 AM

Duncan said...

Small issue - you copy and pasted the sections looking for the folder/7za.exe. So the bottom function (Unzip) doesn't give an error if it fails - just returns a blank string :)

10/29/2007 5:38:18 AM

Duncan said...

Also doesn't this mean it will always fail?

oFSO.FileExists(sWorkingDirectory & " " & "7za.exe")

Why is there an additional space in there? I can't see what the point of the & " " & is?

Nice work tho.

10/29/2007 5:42:20 AM

Nathan+Rice said...

Duncan,

Good catch. That section is supposed to be appending a backslash (\). I updated the code to fix the issue. Not sure how I missed that one. Thanks for the bugcheck and feedback :D.

10/29/2007 9:56:42 PM

said...

can this script run in winzip?
hoping for a reply...

7/28/2008 3:38:06 AM

said...

Can we use this code for ASP pages using 7Zip

7/29/2009 5:04:09 AM

said...

Can you tell what directory or files it is zipping?

12/16/2009 5:20:59 PM

said...

I need a scipt which will zip a text file created by Vb script

so i will have to embed the zip script in my previous script. No Sooner the text file output is created i want the file to be zipped with a unique password. So request u to help me with such a script

6/7/2010 10:43:16 PM

said...

why don''t u guys read some manual asking these people who already spend enough effort. besides willing to share it with you. so go and read some

8/9/2010 3:45:33 AM

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above