Package 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个可以存储多个数据对象的容器。
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 (“关系”) 定义源Package或PackagePart与目标对象之间的关联。 可以是 PackageRelationship 两种类型之一,每种类型可以是两种形式之一:
由 Package.CreateRelationship 方法创建的包级关系 () 将 关联 Package 到以下任一项:
包中的目标部件。
包外部的目标资源。
由 PackagePart.CreateRelationship 方法创建的部件级关系 () 将源 PackagePart 与以下任一项相关联:
包中的另一个目标部件。
包外部的目标资源。
关系的源 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) |
初始化使用给定的 Package 的 FileAccess 类的新实例。 |
Package(FileAccess, Boolean) |
初始化使用给定 Package 和流选项的 FileAccess 类的新实例。 |
属性
FileOpenAccess |
获取包的文件访问设置。 |
PackageProperties |
获取包的核心属性。 |
方法
显式接口实现
IDisposable.Dispose() |
此成员支持Windows Presentation Foundation (WPF) 基础结构,不适合应用程序使用。 改用类型安全的 Dispose(Boolean) 方法。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈