Zip and UnZip Files Using the Windows Shell (XP, Vista, 2003 and 2008) and VBScript
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:\test.zip"
If the file "test.zip" 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:\test.zip", "c:\"
This will simply unzip the contents of the test.zip 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: 1/28/2008 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 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 oUnzipFSO = CreateObject("Scripting.FileSystemObject") If Not oUnzipFSO.FolderExists(sUnzipDestination) Then oUnzipFSO.CreateFolder(sUnzipDestination) 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 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 oZipShell = CreateObject("WScript.Shell") Set oZipFSO = CreateObject("Scripting.FileSystemObject") If Not oZipFSO.FileExists(sZipFile) Then NewZip(sZipFile) End If Set oZipApp = CreateObject("Shell.Application") sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count aFileName = Split(sFile, "\") sFileName = (aFileName(Ubound(aFileName))) 'listfiles sDupe = False For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items If LCase(sFileName) = LCase(sFileNameInZip) Then sDupe = True Exit For End If Next 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 Wscript.Sleep(100) sLoop = sLoop + 1 Loop On Error GoTo 0 End If End Function Sub NewZip(sNewZip) '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 oNewZipFSO = CreateObject("Scripting.FileSystemObject") Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip) oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0) oNewZipFile.Close Set oNewZipFSO = Nothing Wscript.Sleep(500) End Sub |
| Send this to: |

Comments
Bhushan Chirmade said...
I have tried using WindowsUnzip function provided by you in this article however this function will not override the contents if the directory is already present on destination. I have tried using available options with copyHere method but it seems that this method ignores the options.
Please let me know if you have solution on this.
10/24/2007 12:54:37 AM
Nathan+Rice said...
You'll have to loop through the contents of your zip, then check the destination and remove duplicates. The "Zip" function has an example of how to loop through the zip file contents:
For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
If LCase(sFileName) = LCase(sFileNameInZip) Then
sDupe = True
Exit For
End If
Next
10/24/2007 1:45:28 PM
Jeremias said...
I found a spelling error in the code above.
"If Not oFSO.FolderExists(sUnzipDestination) Then
oFSO.CreateFolder(sUnzipDestination)
End If
"
oFSO in quoted code should be changed to oUnzipFSO.
Apart from that it is a wonderful script! Thank you for sharing it!
11/23/2007 3:47:49 AM
Nathan+Rice said...
Thank you. I have made the correction. :)
11/23/2007 11:21:52 AM
Icarus122003 said...
Pertaining to the 2gb file limit I was wondering if your machine is formatted fat32. Windows can't deal with files larger than 2gb with a fat32 partition but NTFS seems ok with them. It might be something you could look into. I have not tested this theory with your script but I have had many experiences in the past with this issue.
12/3/2007 1:05:06 PM
Nathan+Rice said...
This does not have anything to do with fat32. That was my initial thought too. Windows also fails to UNzip files larger than 2GB too. Try it. It's a limitation with the implementation of the zipping engine. Strange and totally undocumented as far as I can tell.
12/3/2007 2:18:51 PM
darius said...
Object Required error on the namespace copyhere
line
VBSCript Runtime Error
12/10/2007 6:21:25 AM
Billy said...
I am getting an Object Required "Wsscript" error on the Wsscript.Shell(500) line of the new zip sub. Is there something I need to do to get past this error?
1/9/2008 11:23:26 AM
Nathan+Rice said...
Billy:
Re-copy and paste the script. You have 2 "S"s where there should be 1. "Wscript" not "Wsscript".
1/9/2008 11:42:34 AM
Billy said...
I have 'Wscript' typed in the asp page. I mistakenly spelled it in the comments that I sent to you. I'm still getting the error.
1/9/2008 12:23:02 PM
Nathan+Rice said...
This is not ASP code, this is VBScript. If you want to use this in an ASP page you'll have to use a component or some bit of custom code to pause. ASP doesn't have a native sleep function.
1/9/2008 2:28:16 PM
Billy said...
Thanks
1/10/2008 5:55:59 AM
Billy said...
The asp page is written in VBScript. <%@ Language=VBScript %>
1/10/2008 6:13:28 AM
David said...
Yes - I am getting the same issue. This is on Windows 2003. The message is "Object required: 'NameSpace(...)'". This is in a .vbs file. Also, oUnzipShell is not used for anything, is it?
1/28/2008 8:09:07 AM
said...
Did you guys got the solution for this "Object required: ''NameSpace(...)''". . Please reply I need this urgently. Thanks.
7/1/2008 4:46:13 PM
said...
I too am getting:
Object required: "Namespace(...)"
errors on Windows 2003 with similar but not exact code. It works fine on Windows XP though...
7/7/2008 12:00:34 PM
said...
Hi,
I have a strange bug where, as far as i can tell. If the file to be zipped is very small, or is compressed quickly the wait loop never exits.
Debugging suggests the .count value is always zero in this case. Despite the fact that the file is already present in the zip file.
My "solution" to this is to test for sLoop being 600 (60 seconds) and exit anyway.
Do you know of a more elegant way of testing?
Thanks,
Richard
9/25/2008 7:17:49 AM
said...
I''ve tried using this script, and I am getting to the point Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip) in the sub...
Then I get the error:
Error: Invalid procedure call or argument.
Code: 800A0005
9/25/2008 11:53:12 PM
said...
After several researches and tries, I found out that the best way to avoid bugs is giving a sleep time long enough.
With multiple zip files, zip files dont get the time to be created and lately, I discovered that copyhere method could also delete files from source folder.
12/18/2008 3:16:54 AM
said...
I am getting an error...
"Error Copying File or Folder
The File Exists."
It had been working for several months, then it stopped working today. I can extract the file manually, but the program fails.
Any advice?
Thanks - Jim
4/2/2009 10:02:27 AM
said...
I think this is exactly what I am looking for. I have a client that needs to email a ZIP file with .mdb file in it. I want to script something that uses the built Windows Zip function to unzip the file, rename the .mdb files and zip it back up. The reason for keeping it in the ZIP format is because the application that is reading this file needs it in that format.
My problem is I don''t know how to run the script!
This is what I haved tried:
U:\Tools\ZIPScript>zipscript.vbs WindowsUnZip "u:\tools\zipscript\catanad.zip", "u:\tools\zipscript\temp"
Where:
zipscript.vbs is the script you wrote
catanad.zip is the file I want to unzip
temp\ is the folder where I want to put it.
Thanks for the post, I hope I can get this working.
8/6/2009 10:38:52 AM
said...
I have the same problem as Jim 4/2/2009 10:02:27 AM
above - it was working fine several months, and now it doesn''t work anymore, or only works some of the time. I''m using it on Windows 2003 x64. I get the dialog box that says "Error Copying File or Folder - The File Exists" but it doesn''t say what file or folder has the problem. If I iterate through all of the Items in the NameSpace(zipfile).Items, and copy each one individually, it gives the error for every single file. Why oh why did this extremely useful tool suddenly stop working??????
/me silently curses Microsoft . . .
10/22/2009 12:49:26 PM
said...
I have a script to copy the files and a script to zip the folder. I need to make a single script. How can I combine those two script in a single script? Any help will be great.
12/1/2009 12:30:06 PM
said...
To combine two scripts you can create a bat file with the following content:
cscript [the (absolute) path to your 1st script]
cscript [the (absolute) path to your 2nd script]
Now run the bat file by either double click it or type the name in the command line (similar to when you use Scheduled Tasks)
2/10/2010 8:00:14 AM
said...
"Error Copying File or Folder - The File Exists" - Fixed!
This is due to shell.application.namespace creating a temp folder everytime you use a zip file. It creates these file in the _users_ temp folder. Once you have 99 of these you will start getting the "The File Exists" error.
5/27/2010 5:41:40 AM
said...
I have a client that needs to email a ZIP file with .mdb file in it. I want to script something that uses the built Windows Zip function to unzip the file, rename the .mdb files and zip it back up.
9/8/2010 12:06:38 AM