Advertisement
ASP_Volume3 Files #66235

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
original-source
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">
        &nbsp;<br />
        <span style="color: navy"><strong>Zip Files Demo&nbsp;<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!&nbsp; 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 />
          &nbsp; &nbsp;&nbsp; -Add a reference to the "WindowsBase.dll"&nbsp;
          <br />
          <br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&nbsp; Project Menu | Add Reference...<br />
          <br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; If you can't find the .dll
          on the .net tab, then click on the
          <br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Browse tab, and try looking
          for it in the following directory:<br />
          <br />
          &nbsp; &nbsp;
          C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\<br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
          <br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; If you can't find it there,
          then you may need to search your&nbsp;<br />
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;system.<br />
          <br />
          <br />
          &nbsp; &nbsp;&nbsp;&nbsp; -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 />
          &nbsp; &nbsp;&nbsp; -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 />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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 />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IO.Path.GetFileName(uriFileName))
                <br />
                <br />
                Dim partUri As New Uri(zipUri, UriKind.Relative)
                <br />
                Dim contentType As String = _<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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 />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zip.CreatePart(partUri, contentType,
                _<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;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 />
          &nbsp; &nbsp;&nbsp; - 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 />
                &nbsp; &nbsp;&nbsp; Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"<br />
                <br />
                &nbsp; &nbsp; &nbsp;'Open the zip file if it exists, else create a new one
                <br />
                &nbsp; &nbsp;&nbsp; Dim zip As Package = ZipPackage.Open(zipPath, _&nbsp;<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)<br />
                <br />
                &nbsp; &nbsp; &nbsp;'Add as many files as you like:<br />
                &nbsp; &nbsp; &nbsp;AddToArchive(zip, "C:\TEMP\Compression\Compress Me1.txt")
                <br />
                &nbsp; &nbsp;&nbsp; AddToArchive(zip, "C:\TEMP\Compression\Compress Me2.txt")
                <br />
                &nbsp; &nbsp;&nbsp; AddToArchive(zip, "C:\TEMP\Compression\Compress Me3.txt")<br />
                <br />
                &nbsp; &nbsp; &nbsp;zip.Close() 'Close the zip file<br />
                <br />
                End Sub<br />
                <br />
                <br />
                Private Sub AddToArchive(ByVal zip As Package, _
                <br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                ByVal fileToAdd As String)<br />
                <br />
                &nbsp; &nbsp;&nbsp; 'Replace spaces with an underscore (_)
                <br />
                &nbsp; &nbsp;&nbsp; Dim uriFileName As String = fileToAdd.Replace(" ", "_")<br />
                <br />
                &nbsp; &nbsp;&nbsp; 'A Uri always starts with a forward slash "/"
                <br />
                &nbsp; &nbsp;&nbsp; Dim zipUri As String = String.Concat("/", _&nbsp;<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; IO.Path.GetFileName(uriFileName))
                <br />
                <br />
                &nbsp; &nbsp;&nbsp; Dim partUri As New Uri(zipUri, UriKind.Relative)
                <br />
                &nbsp; &nbsp;&nbsp; Dim contentType As String = _<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Net.Mime.MediaTypeNames.Application.Zip<br />
                <br />
                &nbsp; &nbsp;&nbsp; 'The PackagePart contains the information:
                <br />
                &nbsp; &nbsp;&nbsp; ' Where to extract the file when it's extracted (partUri)
                <br />
                &nbsp; &nbsp;&nbsp; ' The type of content stream (MIME type): &nbsp;(contentType)
                <br />
                &nbsp; &nbsp;&nbsp; ' The type of compression:&nbsp; (CompressionOption.Normal)
                &nbsp;
                <br />
                &nbsp; &nbsp;&nbsp; Dim pkgPart As PackagePart = zip.CreatePart(partUri, _<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contentType,
                CompressionOption.Normal)<br />
                <br />
                &nbsp; &nbsp; &nbsp;'Read all of the bytes from the file to add to the zip file
                <br />
                &nbsp; &nbsp;&nbsp; Dim bites As Byte() = File.ReadAllBytes(fileToAdd)<br />
                <br />
                &nbsp; &nbsp; &nbsp;'Compress and write the bytes to the zip file
                <br />
                &nbsp; &nbsp;&nbsp; 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.&nbsp; Unfortunately, you have no control
          over the presence of this file.&nbsp; 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.&nbsp; 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