ZipPackage 類別

定義

實作抽象 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

範例

此範例示範如何建立基本 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、目標模式、關聯性類型和識別碼之元件 (識別碼) 的套件層級關聯性。

(繼承來源 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)

適用於

另請參閱