Demo Preview
Introduction
Zipping multiple files has never been easier for .Net programmers than it is now with the .Net framework 3.0! Using the new ZipPackage class in the namespace System.IO.Packaging
and a few lines of code, you can easily create .zip archives with as many files as you like!
Using the Code
At it's easiest:
- Add a reference to the "WindowsBase.dll" (Project menu | Add Reference...)
If you can't find the .dll on the .net tab, then click on the Browse tab, and try looking for it in the following directory:
- C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\
If you can't find it there, then you may need to search your system.
- Add an Imports statement at the top of your class:
Imports System.IO.Packaging
- Write some code:
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"
Dim fileToAdd As String = "C:\TEMP\Compression\Compress Me.txt"
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim uriFileName As String = fileToAdd.Replace(" ", "_")
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
Dim pkgPart As PackagePart = _
zip.CreatePart(partUri, contentType, _
CompressionOption.Normal)
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
pkgPart.GetStream().Write(bites, 0, bites.Length)
zip.Close()
- Refactored code for increased efficiency:
Private Sub ZipFiles()
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
AddToArchive(zip, "C:\TEMP\Compression\Compress Me1.txt")
AddToArchive(zip, "C:\TEMP\Compression\Compress Me2.txt")
AddToArchive(zip, "C:\TEMP\Compression\Compress Me3.txt")
zip.Close()
End Sub
Private Sub AddToArchive(ByVal zip As Package, _
ByVal fileToAdd As String)
Dim uriFileName As String = fileToAdd.Replace(" ", "_")
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
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
contentType, CompressionOption.Normal)
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
pkgPart.GetStream().Write(bites, 0, bites.Length)
End Sub
Points of Interest
Something you'll notice, if you open the .zip file with WinZip or some other zip utility, is a "[Content_Types].xml" file. Unfortunately, you have no control over the presence of this file. It is a file that includes content information about the types of file that are included in the zip file, such as "txt" for a Text File, or "doc" for a Word Document, etc.
Additionally, if you set any of the properties for the PackagePart
(pkgPart.Package.PackageProperties
), then you will have additional files included in the zip archive, such as a "###.psmdcp" file (where ### is a randomly generated number), which is a file containing metadata for the package properties; and an ".rels" file, which is an xml file containing meta-data about package relationships.
I've included an entire Zip Demo for you that demonstrates zipping and unzipping zip archives in VB.Net 2008. It's style is similar to WinZip, but not quite as full featured.
I hope it's helpful to you!
VBRocks
2008 MS Visual Basic MVP