Package 类

定义

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

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

示例

以下示例演示了创建 a 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”)是表示存储在 a . 中的Package对象的抽象类。

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

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

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

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

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

.NET Framework 使用包来存储使用标准 ZIP 文件的页面和文档的内容、资源和关系。 与任何 ZIP 文件一样,应用程序可以使用 System.IO.Packaging 类在单一高效访问容器中存储和保护任何类型的或数量的数据文件。

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

构造函数

名称 说明
Package(FileAccess, Boolean)

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

Package(FileAccess)

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

属性

名称 说明
FileOpenAccess

获取包的文件访问设置。

PackageProperties

获取包的核心属性。

方法

名称 说明
Close()

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

CreatePart(Uri, String, CompressionOption)

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

CreatePart(Uri, String)

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

CreatePartCore(Uri, String, CompressionOption)

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

CreateRelationship(Uri, TargetMode, String, String)

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

CreateRelationship(Uri, TargetMode, String)

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

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, FileMode, FileAccess)

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

Open(Stream, FileMode)

打开具有给定 IO 流和文件模式的包。

Open(Stream)

在给定的 IO 流上打开包。

Open(String, FileMode, FileAccess, FileShare)

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

Open(String, FileMode, FileAccess)

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

Open(String, FileMode)

使用给定文件模式在给定路径处打开包。

Open(String)

在给定的路径和文件名处打开包。

PartExists(Uri)

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

RelationshipExists(String)

指示包中是否包含与给定 ID 的包级关系。

ToString()

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

(继承自 Object)

显式接口实现

名称 说明
IDisposable.Dispose()

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

适用于

另请参阅