ZipPackage Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Implements a derived subclass of the abstract Package base class—the ZipPackage class uses a ZIP archive as the container store. This class cannot be inherited.

Namespace:   System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)


Public NotInheritable Class ZipPackage
	Inherits Package


Gets the file access setting for the package.(Inherited from Package.)


Gets the core properties of the package.(Inherited from Package.)


Saves and closes the package plus all underlying part streams.(Inherited from Package.)

System_CAPS_pubmethodCreatePart(Uri, String)

Creates a new uncompressed part with a given URI and content type.(Inherited from Package.)

System_CAPS_pubmethodCreatePart(Uri, String, CompressionOption)

Creates a new part with a given URI, content type, and compression option.(Inherited from Package.)

System_CAPS_pubmethodCreateRelationship(Uri, TargetMode, String)

Creates a package-level relationship to a part with a given URI, target mode, and relationship type. (Inherited from Package.)

System_CAPS_pubmethodCreateRelationship(Uri, TargetMode, String, String)

Creates a package-level relationship to a part with a given URI, target mode, relationship type, and identifier (ID).(Inherited from Package.)


Deletes a part with a given URI from the package.(Inherited from Package.)


Deletes a package-level relationship.(Inherited from Package.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Saves the contents of all parts and relationships that are contained in the package.(Inherited from Package.)


Serves as the default hash function. (Inherited from Object.)


Returns the part with a given URI.(Inherited from Package.)


Returns a collection of all the parts in the package.(Inherited from Package.)


Returns the package-level relationship with a given identifier.(Inherited from Package.)


Returns a collection of all the package-level relationships.(Inherited from Package.)


Returns a collection of all the package-level relationships that match a given RelationshipType. (Inherited from Package.)


Gets the Type of the current instance.(Inherited from Object.)


Indicates whether a part with a given URI is in the package.(Inherited from Package.)


Indicates whether a package-level relationship with a given ID is contained in the package.(Inherited from Package.)


Returns a string that represents the current object.(Inherited from Object.)


This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended for application use. Use the type-safe Dispose method instead. (Inherited from Package.)

The Package.Open method uses ZipPackage containers by default.

This example shows how to create a basic ZipPackage.

The example creates a package that contains a single document part which is defined as the package's root element by a package-level PackageRelationship.

The package also contains an image part and a second PackageRelationship which defines an association between the source document part and the target image part. (The image is a resource that is used with the document).

'  -------------------------- CreatePackage --------------------------
''' <summary>
'''   Creates a package zip file containing specified
'''   content and resource files.</summary>
Private Shared Sub CreatePackage()
    ' Convert system path and file names to Part URIs. In this example
    ' Dim partUriDocument as Uri /* /Content/Document.xml */ =
    '     PackUriHelper.CreatePartUri(
    '         New Uri("Content\Document.xml", UriKind.Relative))
    ' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
    '     PackUriHelper.CreatePartUri(
    '         New Uri("Resources\Image1.jpg", UriKind.Relative))
    Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
    Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))

    ' Create the Package
    ' (If the package file already exists, FileMode.Create will
    '  automatically delete it first before creating a new one.
    '  The 'using' statement insures that 'package' is
    '  closed and disposed when it goes out of scope.)
    Using package As Package = Package.Open(packagePath, FileMode.Create)
        ' Add the Document part to the Package
        Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)

        ' Copy the data to the Document Part
        Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
            CopyStream(fileStream, packagePartDocument.GetStream())
        End Using ' end:using(fileStream) - Close and dispose fileStream.

        ' Add a Package Relationship to the Document Part
        package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)

        ' Add a Resource Part to the Package
        Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)

        ' Copy the data to the Resource Part
        Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
            CopyStream(fileStream, packagePartResource.GetStream())
        End Using ' end:using(fileStream) - Close and dispose fileStream.

        ' Add Relationship from the Document part to the Resource part
        packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)

    End Using ' end:using (Package package) - Close and dispose package.

End Sub ' end:CreatePackage()

'  --------------------------- CopyStream ---------------------------
''' <summary>
'''   Copies data from a source stream to a target stream.</summary>
''' <param name="source">
'''   The source stream to copy from.</param>
''' <param name="target">
'''   The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
    Const bufSize As Integer = &H1000
    Dim buf(bufSize - 1) As Byte
    Dim bytesRead As Integer = 0
    bytesRead = source.Read(buf, 0, bufSize)
    Do While bytesRead > 0
        target.Write(buf, 0, bytesRead)
        bytesRead = source.Read(buf, 0, bufSize)
End Sub ' end:CopyStream()

For the complete sample, see Writing a Package Sample.

.NET Framework
Available since 3.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top