Package 类

定义

表示一个可以存储多个数据对象的容器。

public ref class Package abstract : IDisposable
public abstract class Package : IDisposable
type Package = class
    interface IDisposable
Public MustInherit Class Package
Implements IDisposable
继承
Package
派生
实现

示例

以下示例演示创建 Package的基本步骤。 在此示例中,将创建一个包来包含文档以及显示为文档一部分的图形图像。 (这类似于 HTML 文件具有 <引用外部图像文件的 IMG> 标记的情况。) 包中也包含两 PackageRelationship 个元素。 第一个是“包级”关系,将文档部件定义为包的根元素。 第二个“部件级”关系定义文档部件 (部件级关系的“源”) 与图像部件的使用 (部件级别关系) 的“目标”之间的关联。 有关完整示例,请参阅 编写包示例

//  -------------------------- 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 =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // 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
        package.CreateRelationship(packagePartDocument.Uri,
                                   TargetMode.Internal,
                                   PackageRelationshipType);

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

        // 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
        packagePartDocument.CreateRelationship(
                                new Uri(@"../resources/image1.jpg",
                                UriKind.Relative),
                                TargetMode.Internal,
                                ResourceRelationshipType);
    }// 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()
'  -------------------------- 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


'  --------------------------- 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)
    Loop
End Sub

注解

Package 是一个抽象类,可用于将对象组织成已定义物理格式的单个实体,以便进行可移植性和高效访问。

ZIP 文件是 的主要物理格式 Package。 其他 Package 实现可能使用其他物理格式,例如 XML 文档、数据库或 Web 服务。

与文件系统一样,包含在 中的 Package 项在文件夹和文件的分层组织中引用。

虽然 Package 本身是一个抽象类, ZipPackage 但 派生类被 Open 方法用作默认值。

PackagePart (“part”) 是抽象类,它表示存储在 中的 Package对象。

PackageRelationship (“关系”) 定义源PackagePackagePart与目标对象之间的关联。 可以是 PackageRelationship 两种类型之一,每种类型可以是两种形式之一:

关系的源 Package 或源 PackagePart 被视为关系的“所有者”。 删除源对象时,源对象拥有的所有关系也会被删除。 创建或删除关系的过程不会以任何方式实际更改源对象或目标对象。

PackageDigitalSignature (“数字签名”) 是由部分和关系组成的,表示 包含在 中的Package数字签名。 数字签名标识发起方,并验证 中包含的 Package 已签名部分和关系是否未修改。

包还支持数字版权管理 (DRM) 允许使用授予授权用户的特定访问权限对 中 Package 的内容元素进行加密。

基于 Package 体系结构, 是一种包类型, XpsDocument 旨在存储基于开放 XML 纸张规范 (XPS) 的文档。

默认情况下,.NET Framework使用包来存储页面和文档的内容、资源和关系。 与任何 ZIP 文件一样,应用程序可以使用 System.IO.Packaging 类在单个高效访问容器中存储并选择性地保护任意类型或数量的数据文件。

有关详细信息,请参阅开放打包约定 (OPC) 规范,可从 https://www.ecma-international.org/publications-and-standards/standards/ecma-376/下载。

构造函数

Package(FileAccess)

初始化使用给定的 PackageFileAccess 类的新实例。

Package(FileAccess, Boolean)

初始化使用给定 Package 和流选项的 FileAccess 类的新实例。

属性

FileOpenAccess

获取包的文件访问设置。

PackageProperties

获取包的核心属性。

方法

Close()

保存并关闭包和所有基础部件流。

CreatePart(Uri, String)

使用给定的 URI 和内容类型创建新的未压缩部件。

CreatePart(Uri, String, CompressionOption)

使用给定的 URI、内容类型和压缩选项创建新部件。

CreatePartCore(Uri, String, CompressionOption)

在派生类中重写时,会在包中创建一个新部件。

CreateRelationship(Uri, TargetMode, String)

使用给定的 URI、目标模式和关系类型创建与部件的包级别关系。

CreateRelationship(Uri, TargetMode, String, String)

创建与具有给定 URI、目标模式、关系类型和标识符 (ID) 部件的包级关系。

DeletePart(Uri)

使用包中给定的 URI 删除部件。

DeletePartCore(Uri)

在派生类中重写时,会使用给定的 URI 删除部件。

DeleteRelationship(String)

删除包级别关系。

Dispose(Boolean)

刷新并保存所有部件和关系的内容,关闭包,并释放所有资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Flush()

保存包中包含的所有部件和关系的内容。

FlushCore()

在派生类中重写时,会将所有部件和关系的内容保存到派生类存储区中。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetPart(Uri)

返回具有给定 URI 的部件。

GetPartCore(Uri)

在派生类中重写时,会返回由给定 URI 寻址的部件。

GetParts()

返回包中所有部件的集合。

GetPartsCore()

在派生类中重写时,会返回包中所有部件的数组。

GetRelationship(String)

返回具有给定标识符的包级别关系。

GetRelationships()

返回所有包级别关系的集合。

GetRelationshipsByType(String)

返回与给定的 RelationshipType 匹配的所有包级别关系的集合。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Open(Stream)

在给定的 IO 流上打开包。

Open(Stream, FileMode)

使用给定的 IO 流和文件模式打开包。

Open(Stream, FileMode, FileAccess)

使用给定的 IO 流、文件模式和文件访问设置打开包。

Open(String)

打开位于给定路径和文件名的包。

Open(String, FileMode)

使用给定的文件模式打开位于给定路径的包。

Open(String, FileMode, FileAccess)

使用给定的文件模式和文件访问设置打开位于给定路径的包。

Open(String, FileMode, FileAccess, FileShare)

使用给定的文件模式、文件访问和文件共享设置打开位于给定路径的包。

PartExists(Uri)

指示具有给定 URI 的部件是否在包中。

RelationshipExists(String)

指示具有给定 ID 的包级别关系是否包含在包中。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IDisposable.Dispose()

此成员支持Windows Presentation Foundation (WPF) 基础结构,不适合应用程序使用。 改用类型安全的 Dispose(Boolean) 方法。

适用于

另请参阅