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
- 派生
- 实现
示例
以下示例演示了创建 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”)定义源Package或PackagePart目标对象之间的关联。 A PackageRelationship 可以是两种类型之一,每个类型可以是两种形式之一:
包级关系(由 Package.CreateRelationship 方法创建)与以下任一 Package 关系相关联:
- 包中的目标部件。
- 包外部的目标资源。
部件级关系(由 PackagePart.CreateRelationship 方法创建)将源 PackagePart 与以下任一关系相关联:
- 包中的另一个目标部件。
- 包外部的目标资源。
关系的源 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) |
初始化使用给定Package的FileAccess类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| FileOpenAccess |
获取包的文件访问设置。 |
| PackageProperties |
获取包的核心属性。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此成员支持Windows Presentation Foundation(WPF)基础结构,不适用于应用程序使用。 请改用类型安全的 Dispose(Boolean) 方法。 |