Login   /   Register

Creating a ZIP archive

Rate this article
     2 votes, average: 4.5 out of 52 votes, average: 4.5 out of 52 votes, average: 4.5 out of 52 votes, average: 4.5 out of 52 votes, average: 4.5 out of 5
Loading ... Loading ...
May 15th, 2008 by Yaron Assa

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

8 Responses to “Creating a ZIP archive”

  1. gordm Says:

    [+]

    Just a couple of notes: There is a typo in your declaration for the oShell object [currently "oSheel"]. The code will fail if Opt... ...

  2. Yaron Assa Says:

    [-]

    Thank you very much for your corrections!

  3. ungaljai Says:

    [+]

    Hi Yaron Assa, The vbscript code being provided in this article contains line numbers. I know that it will be useful for discussi... ...

  4. daniva Says:

    [+]

    Hi ungaljai we are using a software that insert code sinppest with our without line numbers. there is no such software to mak... ...

  5. vishala Says:

    [+]

    Hi Yaron Assa, The above code to create Zip archive is not working.It say "Object Required" while executing the code oZIP.CopyH... ...

  6. chandru.mohan Says:

    [-]

    @ vishala

    just create an empty zip file named some.zip under c drive. it should work

  7. vishala Says:

    [-]

    Hi Chandru,
    Thank you…it is working…but only after creating a non empty zip folder

  8. indianfrompune Says:

    [+]

    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 ... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 806 times