Implementing the CompressedFolder Class
(Page 1 of 4 )
In today’s article I’ll be continuing my explanation of the custom CompressedFolder class that we created. We’ve already discussed all of the properties and methods that have been publicly exposed through the class’s object. Now we’ll look at the rest of the methods involved, as well as how to implement the class in your own scripts.
If you need to download the code for this series again, you can find the latest version here.
Moving right along, we come to the second group of methods in this class. These are the internal methods that are mirrored by the exposed methods. As I mentioned previously, I like to keep my exposed methods very simple by pointing them to hidden methods that are only used internally by my class. This provides me with a sort of interface between the class user and the code itself.
Private Sub NewCompressedFolder(strPath)
Const adTypeBinary = 1
Const adTypeText = 2
Const adWriteChar = 0
Const adSaveCreateNotExist = 1
Const adSaveCreateOverwrite = 2
With CreateObject("ADODB.Stream")
.Open
.Type = adTypeText
.WriteText ChrB(&h50) & ChrB(&h4B) & ChrB(&h5) & ChrB(&h6)
For i = 1 To 18
.WriteText ChrB(&h0)
Next
.SaveToFile strPath, adSaveCreateNotExist
.Close
.Open
.Type = adTypeBinary
.LoadFromFile strPath
.Position = 2
arrBytes = .Read
.Position = 0
.SetEOS
.Write arrBytes
.SaveToFile strPath, adSaveCreateOverwrite
.Close
End With
End Sub
The exposed Open method points to the NewCompressedFolder method for creating new compressed folders. My dedicated readers will recognize this code. Basically, I’m exploiting the ADODB Stream object and forcing it to write a binary file. I predetermined the binary string necessary to create a compressed file by examining an empty one with a hex editor. If you really want to understand this portion of code, you should take the time to read my article “Compressed Folders in WSH.”
You should note that this method does not parse the provided string before attempting to use it. This is mostly for lack of space. If you were to implement this code or distribute it, it really should have some proper error-handling. Sending a malformed path to the ADODB Stream object will provide some extremely strange results.
Private Function AddFile(strFolder, strFile, blnKeepOriginal)
Set objFolder = objShell.NameSpace(strFolder)
intCount = objFolder.Items.Count
Select Case CBool(blnKeepOriginal)
Case True
objFolder.CopyHere strFile, 1548
Case False
objFolder.MoveHere strFile, 1548
End Select
Do Until objFolder.Items.Count = intCount + 1
Sleep 200
If Not ShellBusy Then Exit Do
Loop
End Function
The AddFile method is used to add a file to the compressed folder. I’ve taken advantage of the Shell Object’s ability to handle compressed folders natively by basically masking calls to the MoveHere and CopyHere methods. Since these procedures are performed synchronously, I’ve added a little timer loop to pause execution until the process completes. This provides some means of monitoring the process completion. (As a part of this, I created the ShellBusy subroutine that you’ll see later.)
Here again, I’m assuming that the provided file name is correct. In a production environment, proper handling should be included to verify that the provided parameter is correct before attempting to move or copy that file into the compressed folder.
Next: More internal methods >>
More Code Examples Articles
More By Nilpo