Zip Files (made easy)

July 22, 2007

A few days back while working on a project I needed to zip files. There were several options available, one was obviously to use any third party component available most probably Winzip. But for that you will need to get a licensed copy for that and also install winzip utility on the client machine while deploying your application. To avoid this I tried and used the windows compression utility available under all the windows platforms (above than 2000). The idea is to actually create a file with ZIP extension and copy all the contents of the folder(s)/files(s) in it. The windows compression utility will take care of the compression. Try the following code,

Private Sub ZipFiles(ByVal SrcFolder As String, ByVal DestZipFile As String)
Dim emptyzip() As Byte = New Byte() {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim fs As FileStream = File.Create(DestZipFile)
fs.Write(emptyzip, 0, emptyzip.Length)
fs = Nothing
Dim sc As Shell32.ShellClass = New Shell32.ShellClass
Dim SrcFlder As Shell32.Folder = sc.NameSpace(SrcFolder)
Dim DestFlder As Shell32.Folder = sc.NameSpace(DestZipFile)
Dim items As Shell32.FolderItems = SrcFlder.Items()
DestFlder.CopyHere(items, 20)
End Sub

Now use the function call,


Thats it! it will zip and put all the contents of c:\myFolder into a zip file that will be created at c:\
Hope that helps..

—Happy Coding—