Here are a bunch of examples in VB.NET showing how to use the library.
There are also a few complete, working example applications shipped in the source code distribution.
Add items to a zip file:
Try
Using zip As ZipFile = New ZipFile
zip.AddFile("c:\photos\personal\7440-N49th.png", "")
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "")
zip.AddFile("ReadMe.txt")
zip.Save("MyZipFile.zip")
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
Extract items from a zip file:
Try
Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
Dim e As ZipEntry
For Each e In zip
e.Extract
Next
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
Extract all entries, and set the StatusMessageTextWriter so that verbose messages are generated:
Using zip As ZipFile = ZipFile.Read(FilePath)
zip.StatusMessageTextWriter= System.Console.Out
'Status Messages will be sent to the console during extraction
zip.ExtractAll()
End Using
Add a few files to a zip file, specifying different passwords for different items:
Try
Using zip As New ZipFile
'the first entry is not protected by a password
zip.AddFile("c:\datafiles\ReadMe.txt", "")
zip.Password = "123456!"
zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
zip.Password= "!Secret1";
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents")
zip.Save("Secret.zip")
End Using
Catch ex1 As System.Exception
System.Console.Error.WriteLine("exception: {0}", ex1)
End Try
Add a few files to a zip file, using WinZip-compatible AES encryption on the entries:
Try
Using zip As New ZipFile
zip.Password = "The.Silvertones.Box.Set!"
zip.Encryption = EncryptionAlgorithm.WinZipAes256
zip.AddFile("c:\datafiles\RawData-2008-12-20.csv", "")
zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents")
zip.Save("AES-Encrypted-Secret.zip")
End Using
Catch ex1 As System.Exception
System.Console.Error.WriteLine("exception: {0}", ex1)
End Try
Extract entries using a password:
Using zip As new ZipFile(FilePath)
Dim e As ZipEntry
For Each e In zip
If (e.UsesEncryption)
e.ExtractWithPassword("Secret!")
Else
e.Extract
End If
Next
End Using
Create a zip using ZIP64 extensions:
Try
Using zip As ZipFile = New ZipFile
zip.UseZip64WhenSaving = Zip64Option.AsNecessary
zip.AddFile("c:\datafiles\RawData-2009-02-12.csv", "")
zip.AddFile("ReadMe.txt")
zip.Save(String.Format("backup-{0}.zip", DateTime.Now.ToString("yyyyMMMdd")))
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
Create a zip file, add a file, and also add an entry from a string:
Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive."
Using zip1 As ZipFile = New ZipFile
zip1.AddFileFromString("Readme.txt", "", Content)
zip1.AddFile("MyDocuments\Resume.doc", "files")
zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G"))
zip1.Save("Content.zip")
End Using
Read in a zip file, remove a few entries, save the file:
Dim sw As New System.IO.StringWriter
Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip", sw)
Dim Threshold As New DateTime(2007, 7, 4)
' We cannot remove the entry from the list, within the context of
' an enumeration of said list.
' So we add the doomed entry to a list to be removed later.
' pass 1: mark the entries for removal
Dim MarkedEntries As New System.Collections.Generic.List(Of ZipEntry)
Dim e As ZipEntry
For Each e In zip
If (e.LastModified < Threshold) Then
MarkedEntries.Add(e)
End If
Next
' pass 2: actually remove the entry.
Dim zombie As ZipEntry
For Each zombie In MarkedEntries
zip.RemoveEntry(zombie)
Next
zip.Comment = "This archive has been updated."
zip.Save
End Using
Add a bunch of items, whether files or directories:
Dim itempaths As String() = _
New String() { "c:\temp\Readme.txt", _
"MyProposal.docx", _
"SupportingFiles", _
"images\Image1.jpg" }
Try
Using zip As New ZipFile(ZipToCreate, Console.Out)
Dim i As Integer
For i = 1 To itempaths.Length - 1
' will add Files or Dirs, recursing and flattening subdirectories.
zip.AddItem(itempaths(i), "flat")
Next i
zip.Save
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString())
End Try
Create a self-extracting archive:
Dim DirectoryPath As String = "c:\Documents\Project7"
Using zip As New ZipFile()
zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath))
zip.Comment = "This will be embedded into a self-extracting console-based exe"
zip.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.ConsoleApplication)
End Using
Update some entries in a Zip file:
Using zip1 As New ZipFile
' the UpdateFile method works even if the entry does not yet exist.
' Really it should be called "AddOrUpdateFile"
zip1.UpdateFile("MyDocuments\Readme.txt", "")
zip1.UpdateFile("CustomerList.csv", "")
zip1.Comment = "This zip archive has been created."
zip1.Save("Content.zip")
End Using
Using zip2 As ZipFile = ZipFile.Read("Content.zip")
zip2.UpdateFile("Updates\Readme.txt", "")
zip2.Comment = "This zip archive has been updated: the Readme has been changed."
zip2.Save
End Using