Andrea Tand Andrea Tand - 1 year ago 89 Question

VB.Net creating a zip file and extracting from a zip file

I have a file "C:\Users\Wangui Kamande\Desktop\social.txt" which I would like to compress into a .zip file the location "C:\Users\Wangui Kamande\Desktop".
I got the following code from the Microsoft site but I am not so sure about how to go about this.
Is it that the file "C:\Users\Wangui Kamande\Desktop\social.txt" has to be in a folder?

The code from the mircosoft site is:

Private Sub btnCompress_Click(sender As Object, e As EventArgs) Handles btnCompress.Click
Dim startPath As String = "c:\example\start"
Dim zipPath As String = "c:\example\"
Dim extractPath As String = "c:\example\extract"
ZipFile.CreateFromDirectory(startPath, zipPath)
ZipFile.ExtractToDirectory(zipPath, extractPath)
End Sub

Answer Source

You want the ZipArchive.CreateEntryFromFile extension method to add individual files to a zip...

Using zip = ZipFile.Open("C:\Users\Wangui Kamande\Desktop\", ZipArchiveMode.Create)
    zip.CreateEntryFromFile("C:\Users\Wangui Kamande\Desktop\social.txt", "social.txt")
End Using