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 つのドキュメント パーツを含むパッケージを作成します。

パッケージには、ソース ドキュメント パーツとターゲット イメージ パーツの間の関連付けを定義するイメージ パーツと 2 つ目の 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.Openメソッドでは、既定ZipPackageコンテナーが使用されます。

プロパティ

名前 説明
FileOpenAccess

パッケージのファイル アクセス設定を取得します。

(継承元 Package)
PackageProperties

パッケージのコア プロパティを取得します。

(継承元 Package)

メソッド

名前 説明
Close()

パッケージに加えて、基になるすべてのパーツ ストリームを保存して閉じます。

(継承元 Package)
CreatePart(Uri, String, CompressionOption)

特定の URI、コンテンツ タイプ、圧縮オプションを使用して新しいパーツを作成します。

(継承元 Package)
CreatePart(Uri, String)

指定された URI とコンテンツ タイプを使用して、新しい非圧縮パーツを作成します。

(継承元 Package)
CreatePartCore(Uri, String, CompressionOption)

派生クラスでオーバーライドされた場合は、パッケージに新しいパーツを作成します。

(継承元 Package)
CreateRelationship(Uri, TargetMode, String, String)

特定の URI、ターゲット モード、リレーションシップの種類、識別子 (ID) を持つパーツへのパッケージ レベルのリレーションシップを作成します。

(継承元 Package)
CreateRelationship(Uri, TargetMode, 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)

適用対象

こちらもご覧ください