Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Zip Files Easy!

0.00/5 (No votes)
25 Jul 2008 3  
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.

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"
    
        'Open the zip file if it exists, else create a new one 
        Dim zip As Package = ZipPackage.Open(zipPath, _
                  IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
    
        'Replace spaces with an underscore (_) 
        Dim uriFileName As String = fileToAdd.Replace(" ", "_")
    
        'A Uri always starts with a forward slash "/" 
        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
    
        'The PackagePart contains the information: 
        ' Where to extract the file when it's extracted (partUri) 
        ' The type of content stream (MIME type) - (contentType) 
        ' The type of compression to use (CompressionOption.Normal) 
        Dim pkgPart As PackagePart = _ 
                  zip.CreatePart(partUri, contentType, _
                  CompressionOption.Normal)
    
        'Read all of the bytes from the file to add to the zip file 
        Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
    
        'Compress and write the bytes to the zip file 
        pkgPart.GetStream().Write(bites, 0, bites.Length)
    
        zip.Close() 'Close the zip file
  • Refactored code for increased efficiency:

        Private Sub ZipFiles()
    
            Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"
    
            'Open the zip file if it exists, else create a new one 
             Dim zip As Package = ZipPackage.Open(zipPath, _ 
                  IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
    
            'Add as many files as you like:
            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() 'Close the zip file
    
        End Sub
    
    
        Private Sub AddToArchive(ByVal zip As Package, _ 
                             ByVal fileToAdd As String)
    
            'Replace spaces with an underscore (_) 
            Dim uriFileName As String = fileToAdd.Replace(" ", "_")
    
            'A Uri always starts with a forward slash "/" 
            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
    
            'The PackagePart contains the information: 
            ' Where to extract the file when it's extracted (partUri) 
            ' The type of content stream (MIME type):  (contentType) 
            ' The type of compression:  (CompressionOption.Normal)   
            Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
                       contentType, CompressionOption.Normal)
    
            'Read all of the bytes from the file to add to the zip file 
            Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
    
            'Compress and write the bytes to the zip file 
            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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here