ZipPackage 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
实现抽象 Package 基类的派生子类 - ZipPackage 类将 ZIP 存档用作容器存储。 此类不能被继承。
public ref class ZipPackage sealed : System::IO::Packaging::Package
public sealed class ZipPackage : System.IO.Packaging.Package
type ZipPackage = class
inherit Package
Public NotInheritable Class ZipPackage
Inherits Package
- 继承
示例
此示例演示如何创建基本 ZipPackage。
该示例创建一个包,其中包含由包级别 PackageRelationship定义为包的根元素的单个文档部件。
该包还包含一个图像部件,另 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
注解
包。Open方法默认使用ZipPackage容器。
属性
FileOpenAccess |
获取包的文件访问设置。 (继承自 Package) |
PackageProperties |
获取包的核心属性。 (继承自 Package) |
方法
Close() |
保存并关闭包和所有基础部件流。 (继承自 Package) |
CreatePart(Uri, String) |
使用给定的 URI 和内容类型创建新的未压缩部件。 (继承自 Package) |
CreatePart(Uri, String, CompressionOption) |
使用给定的 URI、内容类型和压缩选项创建新部件。 (继承自 Package) |
CreatePartCore(Uri, String, CompressionOption) |
在派生类中重写时,会在包中创建一个新部件。 (继承自 Package) |
CreateRelationship(Uri, TargetMode, String) |
使用给定的 URI、目标模式和关系类型创建与部件的包级别关系。 (继承自 Package) |
CreateRelationship(Uri, TargetMode, String, String) |
创建与具有给定 URI、目标模式、关系类型和标识符的部件的包级关系, (ID) 。 (继承自 Package) |
DeletePart(Uri) |
使用包中给定的 URI 删除部件。 (继承自 Package) |
DeletePartCore(Uri) |
在派生类中重写时,会使用给定的 URI 删除部件。 (继承自 Package) |
DeleteRelationship(String) |
删除包级别关系。 (继承自 Package) |
Dispose(Boolean) |
刷新并保存所有部件和关系的内容,关闭包,并释放所有资源。 (继承自 Package) |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
Flush() |
保存包中包含的所有部件和关系的内容。 (继承自 Package) |
FlushCore() |
在派生类中重写时,会将所有部件和关系的内容保存到派生类存储区中。 (继承自 Package) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetPart(Uri) |
返回具有给定 URI 的部件。 (继承自 Package) |
GetPartCore(Uri) |
在派生类中重写时,会返回由给定 URI 寻址的部件。 (继承自 Package) |
GetParts() |
返回包中所有部件的集合。 (继承自 Package) |
GetPartsCore() |
在派生类中重写时,会返回包中所有部件的数组。 (继承自 Package) |
GetRelationship(String) |
返回具有给定标识符的包级别关系。 (继承自 Package) |
GetRelationships() |
返回所有包级别关系的集合。 (继承自 Package) |
GetRelationshipsByType(String) |
返回与给定的 RelationshipType 匹配的所有包级别关系的集合。 (继承自 Package) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
PartExists(Uri) |
指示具有给定 URI 的部件是否在包中。 (继承自 Package) |
RelationshipExists(String) |
指示具有给定 ID 的包级别关系是否包含在包中。 (继承自 Package) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
IDisposable.Dispose() |
此成员支持Windows Presentation Foundation (WPF) 基础结构,不适合应用程序使用。 改用类型安全的 Dispose(Boolean) 方法。 (继承自 Package) |