Zip Files Easy!
The purpose of this article is to demonstrating zipping files using the ZipPackage class in this System.IO.Packaging namespace. This demonstration uses: VB.Net 2008 .Net Framwork 3.0
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Upload
<br />
<br />
<table style="width: 100%">
<tr>
<td align="center">
<table style="width: 614px; font-family: Verdana">
<tr>
<td align="left" style="text-align: center">
<br />
<span style="color: navy"><strong>Zip Files Demo <br />
<span style="font-size: 10pt">VB.Net 2008</span></strong></span></td>
</tr>
<tr>
<td align="left">
<span style="color: navy">
<br />
<br />
</span>
<span style="color: navy">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 System.IO.Packaging namespace and a few lines of code, you can easily create
.zip archives with as many files as you like!<br />
<br />
<br />
At it's easiest:<br />
<br />
-Add a reference to the "WindowsBase.dll"
<br />
<br />
- Project Menu | Add Reference...<br />
<br />
If you can't find the .dll
on the .net tab, then click on the
<br />
Browse tab, and try looking
for it in the following directory:<br />
<br />
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\<br />
<br />
If you can't find it there,
then you may need to search your <br />
system.<br />
<br />
<br />
-Add an Imports statement at the top of your class:<br />
<br /><table style="font-size: 10pt; font-family: 'Courier New'; color: black;" width="100%">
<tr>
<td style="width: 81px">
</td>
<td>
Imports System.IO.Packaging</td>
</tr>
</table>
<br />
<br />
-Write some code:<br />
<br />
<table style="font-size: 10pt; font-family: 'Courier New'; color: black;" width="100%">
<tr>
<td style="width: 81px">
</td>
<td>
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"
<br />
Dim fileToAdd As String = "C:\TEMP\Compression\Compress Me.txt"<br />
<br />
'Open the zip file if it exists, else create a new one
<br />
Dim zip As Package = ZipPackage.Open(zipPath, _<br />
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)<br />
<br />
'Replace spaces with an underscore (_)
<br />
Dim uriFileName As String = fileToAdd.Replace(" ", "_")<br />
<br />
'A Uri always starts with a forward slash "/"
<br />
Dim zipUri As String = String.Concat("/", _<br />
IO.Path.GetFileName(uriFileName))
<br />
<br />
Dim partUri As New Uri(zipUri, UriKind.Relative)
<br />
Dim contentType As String = _<br />
Net.Mime.MediaTypeNames.Application.Zip<br />
<br />
'The PackagePart contains the information:
<br />
' Where to extract the file when it's extracted (partUri)
<br />
' The type of content stream (MIME type) - (contentType)
<br />
' The type of compression to use (CompressionOption.Normal)
<br />
Dim pkgPart As PackagePart = _
<br />
zip.CreatePart(partUri, contentType,
_<br />
CompressionOption.Normal)<br />
<br />
'Read all of the bytes from the file to add to the zip file
<br />
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)<br />
<br />
'Compress and write the bytes to the zip file
<br />
pkgPart.GetStream().Write(bites, 0, bites.Length)<br />
<br />
zip.Close() 'Close the zip file<br />
</td>
</tr>
</table>
<br />
<br />
<br />
- Refactored code for increased efficiency:<br />
<br />
<table style="font-size: 10pt; font-family: 'Courier New'; color: black;" width="100%">
<tr>
<td style="width: 81px">
</td>
<td>
Private Sub ZipFiles()<br />
<br />
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"<br />
<br />
'Open the zip file if it exists, else create a new one
<br />
Dim zip As Package = ZipPackage.Open(zipPath, _ <br />
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)<br />
<br />
'Add as many files as you like:<br />
AddToArchive(zip, "C:\TEMP\Compression\Compress Me1.txt")
<br />
AddToArchive(zip, "C:\TEMP\Compression\Compress Me2.txt")
<br />
AddToArchive(zip, "C:\TEMP\Compression\Compress Me3.txt")<br />
<br />
zip.Close() 'Close the zip file<br />
<br />
End Sub<br />
<br />
<br />
Private Sub AddToArchive(ByVal zip As Package, _
<br />
ByVal fileToAdd As String)<br />
<br />
'Replace spaces with an underscore (_)
<br />
Dim uriFileName As String = fileToAdd.Replace(" ", "_")<br />
<br />
'A Uri always starts with a forward slash "/"
<br />
Dim zipUri As String = String.Concat("/", _ <br />
IO.Path.GetFileName(uriFileName))
<br />
<br />
Dim partUri As New Uri(zipUri, UriKind.Relative)
<br />
Dim contentType As String = _<br />
Net.Mime.MediaTypeNames.Application.Zip<br />
<br />
'The PackagePart contains the information:
<br />
' Where to extract the file when it's extracted (partUri)
<br />
' The type of content stream (MIME type): (contentType)
<br />
' The type of compression: (CompressionOption.Normal)
<br />
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _<br />
contentType,
CompressionOption.Normal)<br />
<br />
'Read all of the bytes from the file to add to the zip file
<br />
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)<br />
<br />
'Compress and write the bytes to the zip file
<br />
pkgPart.GetStream().Write(bites, 0, bites.Length)<br />
<br />
End Sub</td>
</tr>
</table>
<br />
<br />
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.<br />
<br />
Additionally, if you set any of the properties for the PackagePart<br />
(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 metadata about package relationships.<br />
<br />
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.<br />
<br />
I hope it's helpful to you!<br />
<br />
VBRocks<br />
<br />
www.VisualBasicRocks.com<br />
</span></td>
</tr>
<tr>
<td align="left">
</td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
Original Comments (3)
Recovered from Wayback Machine