Windows XP has a built-in ZIP mechanism integrated into its file-system. We can easily use this to create standard ZIP archives with only a few commands:
1: 'Variable Declaration
2: Dim sSourceFolder
3: Dim sArchiveFile
4:
5: Dim oShell
6: Dim oZIP
7: Dim oSourceFolder
8:
9: 'Variable Initalization
10: sSourceFolder = "C:\SomeFolder"
11: sArchiveFile = "C:\some.zip"
12: set oShell = CreateObject("Shell.Application")
13:
14: 'Create ZIP
15: Set oZIP= oShell.NameSpace(sArchiveFile)
16:
17: 'Get Source Folder
18: Set oSourceFolder=oShell.NameSpace(sSourceFolder)
19:
20: 'Add source items to ZIP
21: oZIP.CopyHere(oSourceFolder.Items)
Update: As some of you have remarked, the above code only works if there’s already an empty ZIP file in place. Well, no problem – just add this code after line 11:
1: 'Create an empty ZIP file
2: 'Insert after line 11 (when sArchiveFile is defined
3:
4: Dim oFSO
5: dim oFile
6:
7: Set oFSO = CreateObject( "Scripting.FileSystemObject" )
8: Set oFile = oFSO.OpenTextFile( sArchiveFile , ForWriting, True )
9: oFile.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
10: oFile.Close
11: Set oFile = Nothing
12: Set oFSO = Nothing
Posted in ZIP


Yaron Assa




May 16th, 2008 at 4:59 pm
Just a couple of notes:
There is a typo in your declaration for the oShell object [currently “oSheel”]. The code will fail if Option Explicit is declared in the script.
Also, the title for the article should probably be “Writing to an Existing Zip Archive”. The code fails if there is no existing archive when it is first run [Error is “Object Required: ‘oZIP’”]. THe simple solution is to create an empty zip corresponding to the value entered into sArchiveFile.
Other than that, a very handy little utility function. Thanks for sharing this.
GM
May 16th, 2008 at 5:49 pm
Thank you very much for your corrections!
May 21st, 2008 at 4:04 pm
Hi Yaron Assa,
The vbscript code being provided in this article contains line numbers. I know that it will be useful for discussions. But when I copy & paste this code to execute, I need to remove all those line numbers. So why don’t we make it like having two columns where first column is for line no. and second column is for code. It will solve both the purpose of refering the code easily as well as copying only the code without line numbers.
May 22nd, 2008 at 3:42 pm
Hi ungaljai
we are using a software that insert code sinppest with our without line numbers.
there is no such software to make columns (rows|code)
so we have a problem.
in QTP you can make this task easy with replacing regular expressions.
for example, if copy the code above in QTP then select
Edit > Replace you will see the Replace dialog box.
check the option “Regular expression” in the “Find what” text bow enter : [0-9]{1,2}:
Leave blank the “Replace with” edit box.
and last, click on the Replace All button
good luck
May 23rd, 2008 at 7:48 am
Hi Yaron Assa,
The above code to create Zip archive is not working.It say “Object Required” while executing the code oZIP.CopyHere(oSourceFolder.Items).I added all object to watch and checked its value…the values for all the objects was . so oZip is also treated as an object.Please let me know as to why i got that error
Thanks in advance for your help
May 26th, 2008 at 11:20 am
@ vishala
just create an empty zip file named some.zip under c drive. it should work
May 27th, 2008 at 6:10 am
Hi Chandru,
Thank you…it is working…but only after creating a non empty zip folder
May 31st, 2008 at 10:04 am
this is not working at all
and it is giving the error: object not found.
can any one help on this and provide good code to zip and unzip the folder.