|
|
Title | Compress the folders within a folder into a Zip file in Visual Basic 2005 |
Description | This example shows how to compress the folders within a folder into a Zip file in Visual Basic 2005. |
Keywords | Zip, compress, folder, compress folder, compressed folder, compression, Visual Basic 2005 |
Categories | Algorithms, Miscellany, Files and Directories |
|
|
This is one of the few examples on the VB Helper Web site that usees someone else's class. This example uses the CompressedFolder class by Microsoft MVP Eduardo A. Morcillo (see the code for his email address and Web page).
When you click the Zip button, the program creates a DirectoryInfo object representing the parent directory. It uses the DirectoryInfo object's GetDirectories method to loop through the parent's subdirectories.
For each subdirectory, the program adds ".zip" to the subdirectory name to get the name that the zip file should have in the parent directory. It creates a CompressedFolder object representing the Zip file and compressed the subdirectory into it.
When it finishes, the program displays a list of the subdirectories it compressed.
|
|
Private Sub btnZip_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnZip.Click
Dim di As New DirectoryInfo(txtFolder.Text)
Dim txt As String = ""
For Each sub_di As DirectoryInfo In di.GetDirectories()
Dim file_name As String = sub_di.FullName & ".zip"
Using cf As New CompressedFolder(file_name)
txt &= vbCrLf & sub_di.FullName
cf.CompressFile(sub_di.FullName)
End Using
Next sub_di
txtFolders.Text = txt.Substring(vbCrLf.Length)
End Sub
|
|
|
|
|
|