"Dick" <rsutton43@xxxxxx> wrote in message
>I have a need to create several zip archives and then archive those into a
>single archive and copy that archive to external media (i.e. CDR, USB,
>etc). Currently, I'm using WinZip, but would like to not have to call a 3rd
>party program to accomplish my task. I use this utility on many machines.
>Using a 3rd party program has it's own set of problems: it can halt for a
>variety of reasons, etc.
> Has someone figured out how to interface with Windows built-in zip dll's?
> I figure it must require a Com object in order to call the zip dll's. Or
> has someone figured out another solution?
> Thanks in advance...
My experience has shown that it can be done, but not in a graceful manner.
It is easy to zip a folder and its contents. You just create an empty .zip
file (a fixed byte pattern of less than 100 bytes, as I recall), then set up
a shell folder object for that .zip file path (not a filesystem folder
object) and execute this shell folder object's copyhere method to copy the
collection of shell folder items of interest. Optionally you can have a
progress bar, but the process becomes asynchronous to your script, so the
script has no indication of when the zipping process is done. So your
script might do an item count. But if any of the items are for hidden
files, they are ignored, not coppied, so your counter won't work. To get
around this, you could first copy everything you want zipped into a folder,
and then zip that folder and its contents. This way you would only be
zipping one thing - the folder - and hidden files within this folder would
be zipped. But you may not want this additional container folder within
your zip file. Messy. To get some sample code that includes building the
empty zip file, try groups.google.com, searching for something like:
empty zip group:*.scripting
I think that adding files/folders one at a time to an existing .zip is
resource intensive, possibly insignificant while the zip file is small, but
probably significant when the zip file is large. So I generally tend to try
to build the whole zip file at once.
Note that using shell folder items, you can recurse through the zip
file/folder structure, or .CAB file/folder structure, and can access the
info for each item in all the columns that you see when you open the zip/cab
as a file folder on your screen. Starting at the zip/cab file as the root
folder, you can recurse through the normal file/nested folder structure
within, but you are blocked from recursing into internal compressed
file/folders. At least that is my experience. You might create a database
of the CRC32 values listed in the zip folder windows, so you can find out
fairly reliably which zip files contain the same or different versions of
specific file names/paths.
Here is my current zipper script.
'Script to demo zipping all the files in a specified folder into a new
' .Zip file
'Note: This demo does NOT have a direct method of monitoring progress.
'The Shell Folder's CopyHere method starts an external process to
' do the actual copying; depending on options set, it may provide
' popup windows to show progress, ask what to do about
' collisions, etc. This external process does not show up in Task
' Note that any progress or other popup windows take focus away
' from whatever the user may be doing, or from other windows
' to which scripts may be sending keystrokes through the use
' of things like SendKeys or AutoIt.
'This script overwrites the specified .Zip file, creating a new one,
' thus ensuring that completion can be assumed when the number of
' items in the .Zip file matches the number of items in the source
'Note that this code sets up a shell application folder object
' associated with the folder containing the stuff to be zipped.
' It zips the non-hidden items in that folder object. The
' Item and Items propertes do not include hidden items. It
' zips top level non-hidden items (files and folders), plus the
' contents (both hidden and non-hidden) of folders. The count of
' items is the number of top level items in the source folder.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim sCurrentFolder, sFolderPath, sFullZipPath
sCurrentFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
sFullZipPath = sCurrentFolder & "\dir.zip"
sFolderPath = sCurrentFolder & "\CurntFlopy"
iZippedItemCount = ZipAFolder(sFolderPath, sFullZipPath)
MsgBox "Done: ZipFileCount = " & iZippedItemCount
Function ZipAFolder(sFolderPath, sFullZipPath)
'Note: any existing zip file at sFullZipPath is destroyed.
'Creates a new zip file at sFullZipPath and
' copies all of the contents of sFolderPath into it.
Set oShellApp = CreateObject("Shell.Application")
ZipInitialFileCount = oShellApp.NameSpace(sFolderPath).Items.Count
' Create an empty ZIP file
' Copy non-hidden files from sFolderPath into the ZIP file
' Keep script waiting until compression is done
On Error Resume Next
Do Until oShellApp.NameSpace(sFullZipPath).Items.Count _
On Error Goto 0
ZipAFolder = oShellApp.NameSpace(sFolderPath).Items.Count
End Function 'ZipAFolder(sFolderPath, sFullZipPath)
'Creates OR overwrites the file sZipPath, with a binary string
' that represents the contents of an empty .Zip file/folder.
'If the file name's extension is .zip, then WXPSP2 (and perhaps
' other Windows versions) will treat this file as an empty
' compressed folder.
Dim sBinary 'String equivalent of empty .Zip file/folder.
sBinary = "PK" & Chr(5) & Chr(6) & String(18, 0)
.CreateTextFile(sZipPath, True, False).Write(sBinary)
End Function 'CreateMTZip(sZipPath)
------------end of code------------