Package Class

Represents a container that can store multiple data objects.

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

public abstract class Package : IDisposable
public abstract class Package implements IDisposable
public abstract class Package implements IDisposable
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.

Package is an abstract class that can be used to organize objects into a single entity of a defined physical format for portability and efficient access.

A ZIP file is the primary physical format for the Package. Other Package implementations might use other physical formats such as an XML document, a database, or Web service.

Like a file system, items contained in a Package are referenced in a hierarchical organization of folders and files.

While Package itself is an abstract class, the ZipPackage derived class is used as default by the Open method.

A PackagePart ("part") is the abstract class which represents an object that is stored in a Package.

A PackageRelationship ("relationship") defines an association between a source Package or PackagePart, and a target object. A PackageRelationship can be one of two types, each of which can be one of two forms.

  • Package-level relationship (created by Package.CreateRelationship)

    • Between a Package to a target part in the package.

    • Between a Package to a target resource outside the package.

  • Part-level relationship (created by PackagePart.CreateRelationship)

    • Between a source PackagePart to another target part in the package.

    • Between a source PackagePart to a target resource outside the package.

The relationship's source Package or source PackagePart is considered the "owner" of the relationship. When the source object is deleted, all the relationships owned by the source object are also deleted. The process of creating or deleting a relationship does not physically change either the source and target objects in any way.

A PackageDigitalSignature ("digital signature") is a composition of parts and relationships representing a digital signature included with a Package. The digital signature identifies the originator and validates that the signed parts and relationships contained in the Package have not been modified.

Packages also support Digital Rights Management (DRM) which allows content elements in a Package to be encrypted with specific access rights granted to authorized users.

Based the Package architecture, an XpsDocument is a package type designed for storing documents based on the open XML Paper Specification (XPS).

Microsoft .NET Framework version 3.0 uses packages to store content, resources, and relationships for pages and documents using a standard ZIP file by default. Like with any ZIP file, your application can use the System.IO.Packaging classes to store and optionally protect any type or number of data files in a single efficient to access container.

For more information see the Open Packaging Conventions (OPC) specification available for download at

The following example shows the basic steps in creating a Package. In this example, a package is created to contain a document together with a graphic image that is displayed as part of the document. (This is like an HTML file which has an <IMG> tag that references an external image file.) Two PackageRelationship elements are also included in the package. The first, a "package-level" relationship, defines the document part as the package's root element. A second, "part-level" relationship defines the association between the document part (the "source" of the part-level relationship) and its use of the image part (the "target" of the part-level relationship). For the complete sample, see Writing a Package Sample.

//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = 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 package =
        Package.Open(packagePath, FileMode.Create))
        // Add the Document part to the Package
        PackagePart packagePartDocument =

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add a Package Relationship to the Document Part

        // Add a Resource Part to the Package
        PackagePart packagePartResource =

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
            CopyStream(fileStream, packagePartResource.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add Relationship from the Document part to the Resource part
                                new Uri(@"../resources/image1.jpg",

    }// end:using (Package package) - Close and dispose package.

}// 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 static void CopyStream(Stream source, Stream target)
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()


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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions