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:\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: 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