DotNetZip - VB.NET Examples
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:
1Try 2 Using zip As ZipFile = New ZipFile 3 zip.AddFile("c:\photos\personal\7440-N49th.png", "") 4 zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "") 5 zip.AddFile("ReadMe.txt") 6 zip.Save("MyZipFile.zip") 7 End Using 8Catch ex1 As Exception 9 Console.Error.WriteLine("exception: {0}", ex1.ToString) 10End Try
Extract items from a zip file:
1Try 2 Using zip As ZipFile = ZipFile.Read(ZipFileToExtract) 3 Dim e As ZipEntry 4 For Each e In zip 5 e.Extract 6 Next 7 End Using 8Catch ex1 As Exception 9 Console.Error.WriteLine("exception: {0}", ex1.ToString) 10End Try
Extract all entries, and set the StatusMessageTextWriter so that verbose messages are generated:
1Using zip As ZipFile = ZipFile.Read(FilePath) 2 zip.StatusMessageTextWriter= System.Console.Out 3 'Status Messages will be sent to the console during extraction 4 zip.ExtractAll() 5End Using
Add a few files to a zip file, specifying different passwords for different items:
1Try 2 Using zip As New ZipFile 3 'the first entry is not protected by a password 4 zip.AddFile("c:\datafiles\ReadMe.txt", "") 5 zip.Password = "123456!" 6 zip.AddFile("c:\photos\personal\7440-N49th.png", "images") 7 zip.Password= "!Secret1"; 8 zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents") 9 zip.Save("Secret.zip") 10 End Using 11Catch ex1 As System.Exception 12 System.Console.Error.WriteLine("exception: {0}", ex1) 13End Try
Add a few files to a zip file, using WinZip-compatible AES encryption on the entries:
1Try 2 Using zip As New ZipFile 3 zip.Password = "The.Silvertones.Box.Set!" 4 zip.Encryption = EncryptionAlgorithm.WinZipAes256 5 zip.AddFile("c:\datafiles\RawData-2008-12-20.csv", "") 6 zip.AddFile("c:\photos\personal\7440-N49th.png", "images") 7 zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents") 8 zip.Save("AES-Encrypted-Secret.zip") 9 End Using 10Catch ex1 As System.Exception 11 System.Console.Error.WriteLine("exception: {0}", ex1) 12End Try
Extract entries using a password:
1Using zip As new ZipFile(FilePath) 2 Dim e As ZipEntry 3 For Each e In zip 4 If (e.UsesEncryption) 5 e.ExtractWithPassword("Secret!") 6 Else 7 e.Extract 8 End If 9 Next 10End Using
This example creates a zip using ZIP64 extensions. ZIP64 allows you to exceed 4gb in a zip, or 65535 entries in a zip.
1Try 2 Using zip As ZipFile = New ZipFile 3 zip.UseZip64WhenSaving = Zip64Option.AsNecessary 4 zip.AddFile("c:\datafiles\RawData-2009-02-12.csv", "") 5 zip.AddFile("ReadMe.txt") 6 zip.Save(String.Format("backup-{0}.zip", DateTime.Now.ToString("yyyyMMMdd"))) 7 End Using 8Catch ex1 As Exception 9 Console.Error.WriteLine("exception: {0}", ex1.ToString) 10End Try
Create a zip file, add a file, and also add an entry from a string. When the zip is unzipped, the content from the string will be inserted into the file "Readme.txt".
1Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive." 2Using zip1 As ZipFile = New ZipFile 3 zip1.AddEntry("Readme.txt", "This is the readme content...") 4 zip1.AddFile("MyDocuments\Resume.doc", "files") 5 zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G")) 6 zip1.Save("Content.zip") 7End Using
Create a zip file, and add an entry taking content from a stream, like a MemoryStream or a FileStream.
1Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive." 2Using zip1 As ZipFile = New ZipFile 3 zip1.AddEntry("Readme.txt", stream) 4 zip1.AddFile("MyDocuments\Resume.doc", "files") 5 zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G")) 6 zip1.Save("Content.zip") 7End Using
Read in a zip file, remove a few entries, save the file:
1Dim sw As New System.IO.StringWriter 2Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip", sw) 3 Dim Threshold As New DateTime(2007, 7, 4) 4 ' We cannot remove the entry from the list, within the context of 5 ' an enumeration of said list. 6 ' So we add the doomed entry to a list to be removed later. 7 ' pass 1: mark the entries for removal 8 Dim MarkedEntries As New System.Collections.Generic.List(Of ZipEntry) 9 Dim e As ZipEntry 10 For Each e In zip 11 If (e.LastModified < Threshold) Then 12 MarkedEntries.Add(e) 13 End If 14 Next 15 ' pass 2: actually remove the entry. 16 Dim zombie As ZipEntry 17 For Each zombie In MarkedEntries 18 zip.RemoveEntry(zombie) 19 Next 20 zip.Comment = "This archive has been updated." 21 zip.Save 22End Using
Add a bunch of items, whether files or directories:
1Dim itempaths As String() = _ 2 New String() { "c:\temp\Readme.txt", _ 3 "MyProposal.docx", _ 4 "SupportingFiles", _ 5 "images\Image1.jpg" } 6Try 7 Using zip As New ZipFile(ZipToCreate, Console.Out) 8 Dim i As Integer 9 For i = 1 To itempaths.Length - 1 10 ' will add Files or Dirs, recursing and flattening subdirectories. 11 zip.AddItem(itempaths(i), "flat") 12 Next i 13 zip.Save 14 End Using 15Catch ex1 As Exception 16 Console.Error.WriteLine("exception: {0}", ex1.ToString()) 17End Try
Create a self-extracting archive:
1Dim DirectoryPath As String = "c:\Documents\Project7" 2Using zip As New ZipFile() 3 zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath)) 4 zip.Comment = "This will be embedded into a self-extracting console-based exe" 5 zip.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.ConsoleApplication) 6End Using
Update some entries in a Zip file:
1Using zip1 As New ZipFile 2 ' the UpdateFile method works even if the entry does not yet exist. 3 ' Really it should be called "AddOrUpdateFile" 4 zip1.UpdateFile("MyDocuments\Readme.txt", "") 5 zip1.UpdateFile("CustomerList.csv", "") 6 zip1.Comment = "This zip archive has been created." 7 zip1.Save("Content.zip") 8End Using 9 10Using zip2 As ZipFile = ZipFile.Read("Content.zip") 11 zip2.UpdateFile("Updates\Readme.txt", "") 12 zip2.Comment = "This zip archive has been updated: the Readme has been changed." 13 zip2.Save 14End Using
Produce a zip file that contains embedded zip files.
1Public Sub Run() 2 Using s1 As Stream = ZipIntoMemory("c:\temp\dir1") 3 Using s2 As Stream = ZipIntoMemory("c:\temp\dir2") 4 Using zip1 as New ZipFile 5 zip1.AddEntry("test1.zip", s1) 6 zip1.AddEntry("test2.zip", s2) 7 ' save to a file. Could also save to a stream here 8 zip1.Save("Tescher.zip") 9 End Using 10 End Using 11 End Using 12End Sub 13 14Public Function ZipIntoMemory(ByVal path As String) As Stream 15 Dim ms As New MemoryStream 16 Using zip1 as New ZipFile 17 zip1.AddDirectory(path, "Result") 18 zip1.Save(ms) 19 End Using 20 ' move the stream position to the beginning 21 ms.Seek(0,SeekOrigin.Begin) 22 Return ms 23End Function