抽象
Package 基本クラスの派生サブクラスを実装します。
ZipPackage クラスは、コンテナ ストアとして ZIP アーカイブを使用します。このクラスは継承できません。
名前空間: System.IO.Packaging
アセンブリ: WindowsBase (windowsbase.dll 内)
Public NotInheritable Class ZipPackage
Inherits Package
Dim instance As ZipPackage
public sealed class ZipPackage : Package
public ref class ZipPackage sealed : public Package
public final class ZipPackage extends Package
public final class ZipPackage extends Package
| このマネージ クラスは XAML では使用できません。 |
Package.Open メソッドは、既定で 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()
サンプル全体については、「パッケージの書き込みのサンプル」を参照してください。
System.Object
System.IO.Packaging.Package
System.IO.Packaging.ZipPackage
この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
.NET Framework
サポート対象 : 3.0