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でパッケージのルート要素として定義される 1 つのドキュメント パーツを含むパッケージを作成します。

パッケージには、ソース ドキュメント パーツとターゲット イメージ パーツの間の関連付けを定義する 1 つ目 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)

指定した ID を持つパッケージ レベル リレーションシップを返します。

(継承元 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)

適用対象

こちらもご覧ください