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 與下列兩者產生關聯:
套件中的另一個目標元件。
套件外部的目標資源。
關聯性的來源或來源PackagePackagePart會被視為關聯性的「擁有者」。 刪除來源物件時,也會刪除來源物件所擁有的所有關聯性。 建立或刪除關聯性的程式不會以任何方式實際變更來源或目標物件。
PackageDigitalSignature (「數位簽名」) 是元件和關聯性的組成,代表 隨附Package的數字簽名。 數位簽名會識別來源器,並驗證 中 Package 所包含的已簽署元件和關聯性尚未修改。
套件也支援數位版權管理 (DRM) ,讓中 Package 的內容元素能夠使用授與授權使用者的特定訪問許可權進行加密。
根據 Package 架構,是一種套件類型, XpsDocument 其設計目的是根據開啟 的 XML 紙張規格來儲存檔, (XPS) 。
.NET Framework 預設會使用標準 ZIP 檔案來儲存頁面和文件的內容、資源和關聯性。 如同任何 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 |
取得套件的核心屬性。 |
方法
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、目標模式、關聯性類型和識別碼 (識別碼) 之元件的套件層級關聯性。 |
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) 方法。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應