Package Package Package Package Class

定義

表示可以存放多個資料物件的容器。Represents a container that can store multiple data objects.

public ref class Package abstract : IDisposable
public abstract class Package : IDisposable
type Package = class
    interface IDisposable
Public MustInherit Class Package
Implements IDisposable
繼承
PackagePackagePackagePackage
衍生
實作

範例

下列範例示範建立的基本步驟PackageThe following example shows the basic steps for creating a Package. 在此範例中,封裝會建立包含文件以及圖形影像是顯示為文件的一部分。In this example, a package is created to contain a document together with a graphic image that is displayed as part of the document. (這是類似於案例中具有 HTML 檔<i m g > 標記,參考外部影像檔。)兩個PackageRelationship元素也包含在封裝中。(This is similar to the case in which an HTML file has an <IMG> tag that references an external image file.) Two PackageRelationship elements are also included in the package. 第一天,「 封裝層級 」 關聯性會定義文件組件為套件的根項目。The first, a "package-level" relationship, defines the document part as the package's root element. 第二,「 組件層級 」 的關聯性定義文件組件 (「 來源 」 的組件層關係) 和其使用的映像組件 (""做為目標的組件層級關聯性) 之間的關聯。A second, "part-level" relationship defines the association between the document part (the "source" of the part-level relationship) and its use of the image part (the "target" of the part-level relationship). 如需完整的範例,請參閱寫入封裝範例For the complete sample, see Writing a Package Sample.

//  -------------------------- 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 ' 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 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 ' end:CopyStream()

備註

Package 是抽象類別,可用來將物件組織在單一實體的定義實體,可攜性和高效率的存取。Package is an abstract class that can be used to organize objects into a single entity of a defined physical format for portability and efficient access.

ZIP 檔案是主要實體格式PackageA ZIP file is the primary physical format for the Package. 其他Package實作可能使用其他實體的格式,例如 XML 文件時,資料庫,或 Web 服務。Other Package implementations might use other physical formats such as an XML document, a database, or Web service.

項目包含在檔案系統,例如Package在階層式組織中的資料夾與檔案參考。Like a file system, items contained in a Package are referenced in a hierarchical organization of folders and files.

雖然Package本身是抽象類別,ZipPackage衍生的類別做為預設的 Open方法。Although Package itself is an abstract class, the ZipPackage derived class is used as default by the Open method.

A PackagePart (「 組件 」) 是抽象類別,代表儲存在物件PackageA PackagePart ("part") is the abstract class that represents an object that is stored in a Package.

A PackageRelationship (「 關聯性 」) 會定義來源之間的關聯PackagePackagePart和目標物件。A PackageRelationship ("relationship") defines an association between a source Package or PackagePart and a target object. APackageRelationship可以是下列其中一種,每一個都可以有兩種形式:A PackageRelationship can be one of two types, each of which can be one of two forms:

關聯性的來源Package或來源PackagePart會被視為 「 擁有者 」 的關聯性。The relationship's source Package or source PackagePart is considered the "owner" of the relationship. 刪除來源物件時,會一併刪除來源物件所擁有的所有關聯性。When the source object is deleted, all the relationships owned by the source object are also deleted. 建立或刪除關聯性的程序不會實際變更以任何方式的來源或目標物件。The process of creating or deleting a relationship does not physically change either the source or target objects in any way.

A PackageDigitalSignature (「 數位簽章 」) 是組件和關係代表隨附的數位簽章的組合PackageA PackageDigitalSignature ("digital signature") is a composition of parts and relationships representing a digital signature included with a Package. 數位簽章識別建立者,並驗證已簽署組件和關聯性包含在Package尚未經過修改。The digital signature identifies the originator and validates that the signed parts and relationships contained in the Package have not been modified.

封裝也支援數位版權管理 (DRM) 可讓內容中的項目Package加密與特定存取權限授與授權的使用者。Packages also support Digital Rights Management (DRM) which allows content elements in a Package to be encrypted with specific access rights granted to authorized users.

根據Package架構中,XpsDocument是設計用來儲存文件開啟為基礎的封裝類型XML Paper Specification (XPS)Based on the Package architecture, an XpsDocument is a package type designed for storing documents based on the open XML Paper Specification (XPS).

.NET Framework 會使用套件來儲存內容、 資源和網頁和依預設使用標準的 ZIP 檔案的文件的關聯性。The .NET Framework uses packages to store content, resources, and relationships for pages and documents using a standard ZIP file by default. 可以使用任何 ZIP 檔案中,您的應用程式System.IO.Packaging類別來儲存並選擇性地保護任何類型或單一的有效存取容器中的資料檔案數目。As with any ZIP file, your application can use the System.IO.Packaging classes to store and optionally protect any type or number of data files in a single efficient-to-access container.

如需詳細資訊,請參閱開放封裝慣例 (OPC) 規格可以從下載https://go.microsoft.com/fwlink/?LinkID=71255For more information, see the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.

建構函式

Package(FileAccess) Package(FileAccess) Package(FileAccess) Package(FileAccess)

將使用特定 PackageFileAccess 類別的新執行個體初始化。Initializes a new instance of the Package class that uses a given FileAccess.

Package(FileAccess, Boolean) Package(FileAccess, Boolean) Package(FileAccess, Boolean) Package(FileAccess, Boolean)

將使用特定 Package 及資料流選項之 FileAccess 類別的新執行個體初始化。Initializes a new instance of the Package class that uses a given FileAccess and streaming option.

屬性

FileOpenAccess FileOpenAccess FileOpenAccess FileOpenAccess

取得套件的檔案存取設定。Gets the file access setting for the package.

PackageProperties PackageProperties PackageProperties PackageProperties

取得套件的核心屬性。Gets the core properties of the package.

方法

Close() Close() Close() Close()

儲存並關閉套件,以及所有基礎組件資料流。Saves and closes the package plus all underlying part streams.

CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String)

以特定 URI 及內容類型建立新的未壓縮組件。Creates a new uncompressed part with a given URI and content type.

CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption)

以特定 URI、內容類型及壓縮選項建立新組件。Creates a new part with a given URI, content type, and compression option.

CreatePartCore(Uri, String, CompressionOption) CreatePartCore(Uri, String, CompressionOption) CreatePartCore(Uri, String, CompressionOption) CreatePartCore(Uri, String, CompressionOption)

在衍生類別中覆寫時,請在套件中建立新的組件。When overridden in a derived class, creates a new part in the package.

CreateRelationship(Uri, TargetMode, String) CreateRelationship(Uri, TargetMode, String) CreateRelationship(Uri, TargetMode, String) CreateRelationship(Uri, TargetMode, String)

以特定 URI、目標模式及關聯性類型建立套件層級關係。Creates a package-level relationship to a part with a given URI, target mode, and relationship type.

CreateRelationship(Uri, TargetMode, String, String) CreateRelationship(Uri, TargetMode, String, String) CreateRelationship(Uri, TargetMode, String, String) CreateRelationship(Uri, TargetMode, String, String)

以特定 URI、目標模式、關聯性類型及識別項 (ID)identifier (ID) 建立套件層級關係。Creates a package-level relationship to a part with a given URI, target mode, relationship type, and 識別項 (ID)identifier (ID).

DeletePart(Uri) DeletePart(Uri) DeletePart(Uri) DeletePart(Uri)

刪除套件中具有特定 URI 的組件。Deletes a part with a given URI from the package.

DeletePartCore(Uri) DeletePartCore(Uri) DeletePartCore(Uri) DeletePartCore(Uri)

在衍生類別中覆寫時,刪除具有特定 URI 的組件。When overridden in a derived class, deletes a part with a given URI.

DeleteRelationship(String) DeleteRelationship(String) DeleteRelationship(String) DeleteRelationship(String)

刪除套件層級關係。Deletes a package-level relationship.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

清除並儲存所有組件與關聯性的內容、關閉套件,然後釋放所有的資源。Flushes and saves the content of all parts and relationships, closes the package, and releases all resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Flush() Flush() Flush() Flush()

儲存套件包含的所有組件及關聯性的內容。Saves the contents of all parts and relationships that are contained in the package.

FlushCore() FlushCore() FlushCore() FlushCore()

在衍生類別中覆寫時,將所有組件與關聯性的內容儲存到衍生類別存放區。When overridden in a derived class, saves the content of all parts and relationships to the derived class store.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetPart(Uri) GetPart(Uri) GetPart(Uri) GetPart(Uri)

傳回具有特定 URI 的組件。Returns the part with a given URI.

GetPartCore(Uri) GetPartCore(Uri) GetPartCore(Uri) GetPartCore(Uri)

在衍生類別中覆寫時,傳回由特定 URI 定址的組件。When overridden in a derived class, returns the part addressed by a given URI.

GetParts() GetParts() GetParts() GetParts()

傳回套件中所有組件的集合。Returns a collection of all the parts in the package.

GetPartsCore() GetPartsCore() GetPartsCore() GetPartsCore()

在衍生類別中覆寫時,傳回套件中所有組件的陣列。When overridden in a derived class, returns an array of all the parts in the package.

GetRelationship(String) GetRelationship(String) GetRelationship(String) GetRelationship(String)

傳回具有特定識別項的套件層級關係。Returns the package-level relationship with a given identifier.

GetRelationships() GetRelationships() GetRelationships() GetRelationships()

傳回所有套件層級關係的集合。Returns a collection of all the package-level relationships.

GetRelationshipsByType(String) GetRelationshipsByType(String) GetRelationshipsByType(String) GetRelationshipsByType(String)

傳回符合特定 RelationshipType 的所有套件層級關係的集合。Returns a collection of all the package-level relationships that match a given RelationshipType.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Open(Stream) Open(Stream) Open(Stream) Open(Stream)

開啟位於特定 IO 資料流上的套件。Opens a package on a given IO stream.

Open(Stream, FileMode) Open(Stream, FileMode) Open(Stream, FileMode) Open(Stream, FileMode)

使用特定 IO 資料流及檔案模式開啟套件。Opens a package with a given IO stream and file mode.

Open(Stream, FileMode, FileAccess) Open(Stream, FileMode, FileAccess) Open(Stream, FileMode, FileAccess) Open(Stream, FileMode, FileAccess)

使用特定 IO 資料流、檔案模式及檔案存取設定開啟套件。Opens a package with a given IO stream, file mode, and file access setting.

Open(String) Open(String) Open(String) Open(String)

開啟特定路徑與檔名的封裝。Opens a package at a given path and file name.

Open(String, FileMode) Open(String, FileMode) Open(String, FileMode) Open(String, FileMode)

使用特定檔案模式開啟位於特定路徑的套件。Opens a package at a given path using a given file mode.

Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess)

使用特定檔案模式及檔案存取設定,開啟位於特定路徑的套件。Opens a package at a given path using a given file mode and file access setting.

Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare)

使用特定檔案模式、檔案存取及檔案共用設定,開啟位於特定路徑的套件。Opens a package at a given path using a given file mode, file access, and file share setting.

PartExists(Uri) PartExists(Uri) PartExists(Uri) PartExists(Uri)

指出套件中是否存在具有特定 URI 的組件。Indicates whether a part with a given URI is in the package.

RelationshipExists(String) RelationshipExists(String) RelationshipExists(String) RelationshipExists(String)

指出套件中是否包含具有特定 ID 的套件層級關係。Indicates whether a package-level relationship with a given ID is contained in the package.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但不建議用於應用程式。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended for application use. 請改用類型安全的 Dispose(Boolean) 方法。Use the type-safe Dispose(Boolean) method instead.

適用於

另請參閱