Creating a ZIP Archive

Article Tools

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
Previous postExtracting a ZIP File Next postOverride the Object Exist Property

Related Posts

Post Your Comment

You must be logged in to post a comment.