Zip whole folder using WindowsBase

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
So here is the code I use, I want to mod it so I can zip whole folder.Private Sub ZipFiles()

Dim zipPath As String = "C:destmyzip.zip"

Open the zip file if it exists, else create a new one
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)

Add as many files as you like:
AddToArchive(zip, "C:WinRAR.exe")

zip.Close() Close the zip file

End Sub


Private Sub AddToArchive(ByVal zip As Package, _
ByVal fileToAdd As String)

Replace spaces with an underscore (_)
Dim uriFileName As String = fileToAdd.Replace(" ", "_")

A Uri always starts with a forward slash "/"
Dim zipUri As String = String.Concat("/", _
IO.Path.GetFileName(uriFileName))

Dim partUri As New Uri(zipUri, UriKind.Relative)
Dim contentType As String = _
Net.Mime.MediaTypeNames.Application.Zip

The PackagePart contains the information:
Where to extract the file when its extracted (partUri)
The type of content stream (MIME type): (contentType)
The type of compression: (CompressionOption.Normal)
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
contentType, CompressionOption.Normal)

Read all of the bytes from the file to add to the zip file
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)

Compress and write the bytes to the zip file
pkgPart.GetStream().Write(bites, 0, bites.Length)

End Sub
Thanks.

View the full article
 
Back
Top