Zip and unzip files

Viewed 25784

I am a programmer using VS2012. I am wanting to unzip a zip file (made with Winzip, filzip or other zip compression routines) and then also be able to zip the files back up into a zip file.

What is the best library to use for this and can I please have some sample code on how to use the library?

EDIT

I am using VB.net, here is my code:

Public Function extractZipArchive() As Boolean
    Dim zipPath As String = "c:\example\start.zip"
    Dim extractPath As String = "c:\example\extract"

    Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
        For Each entry As ZipArchiveEntry In archive.Entries
            If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then
                entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
            End If
        Next
    End Using
End Function

What import statements do I need to use? Currently I have added the following:

Imports System.IO
Imports System.IO.Compression

I am getting the error:

Type 'ZipArchive' is not defined

How can I fix this error?

5 Answers
    Dim fileStream As Stream = File.OpenRead("your file path")

    Using zipToOpen As FileStream = New FileStream(".......\My.zip", FileMode.CreateNew)
        Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Create)
            Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("your file path")
            fileStream.CopyTo(readmeEntry.Open())
        End Using
    End Using
Related