.NET 中包关系的目的是什么?

发布于 2024-10-07 16:32:15 字数 81 浏览 0 评论 0原文

.NET 中的包关系 (ZipPackage) 到底是什么?我知道什么是包,但我无法理解关系的目的。您能否举例说明我何时想要使用它们以及它们的用途?

What exactly is a Package Relationship (ZipPackage) in .NET? I know what a Package is but I'm having trouble understanding the purpose of relationships. Could you give examples of when I would want to use them and what they are good for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-10-14 16:32:15

来自 MSDN (包类):

Package 是一个抽象类,可用于将对象组织成已定义物理格式的单个实体,以实现可移植性和高效访问。

和:

PackageRelationship(“关系”)定义源 Package 或 PackagePart 与目标对象之间的关联。 PackageRelationship 可以是两种类型之一,每种类型都可以是两种形式之一:

* A package-level relationship (created by the Package.CreateRelationship method) relates a Package to either:
      o A target part in the package.
      o A target resource outside the package.
* A part-level relationship (created by the PackagePart.CreateRelationship method) relates a source PackagePart to either:
      o Another target part in the package.
      o A target resource outside the package.

因此,在创建 Package 时,如果您想表明一个对象与该对象的另一部分之间存在关系,对于包,您应该使用 PackageRelationship 来指示它是什么类型的关系。

ZipPackage 还可用于处理 Open Office XML 文件 - 在此用例中,有时指示对象之间的关系很有用。请参阅此处

From MSDN (Package Class):

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.

And:

A PackageRelationship ("relationship") defines an association between a source Package or PackagePart and a target object. A PackageRelationship can be one of two types, each of which can be one of two forms:

* A package-level relationship (created by the Package.CreateRelationship method) relates a Package to either:
      o A target part in the package.
      o A target resource outside the package.
* A part-level relationship (created by the PackagePart.CreateRelationship method) relates a source PackagePart to either:
      o Another target part in the package.
      o A target resource outside the package.

So, when creating a Package, if you want to indicate that there is a relationship between an object to another part of the package, you should use a PackageRelationship to indicate what kind of relationship it is.

A ZipPackage can also be used to work with Open Office XML files - in this use case, it is sometime useful to indicate relationships of objects to each other. See here.

別甾虛僞 2024-10-14 16:32:15

基本上,它是将事物连接在一起,以便部署它们,更重要的是同时取消部署。

举例来说,您有一个程序,并且有一个它使用的 GIF 包。然后,您希望 GIF 在安装程序时可用,并且希望在卸载程序时清理它们。

Basically it is to connect things together so that they are deployed, and more importantly un-deployed at the same time.

Say for example you have a program and you have a package of GIFs that it uses. Then you want the GIFs to be available when your program is installed, and you want them to be cleaned up when the program is uninstalled.

烟花肆意 2024-10-14 16:32:15

在我看来,好的例子都是 使用 OPC 的文件格式

以及用于 opc 容器的 C# 示例带有预览图像的功能将被实现 fe:

using System.IO.Compression;
using System.IO.Packaging;

using (var package = Package.Open( your_Zip/OPC_File_Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
    // Add the Preview Image to the OPC container
    var imagePart = package.CreatePart( PackUriHelper.CreatePartUri(new Uri("preview.png", UriKind.Relative)), "image/png");
    package.CreateRelationship( imagePart.Uri, TargetMode.Internal, OpcConstants.RelationshipTypes.PREVIEWIMAGE, "preview");

    using(var imageStream = imagePart.GetStream( FileMode.Open, FileAccess.Write ))
    {
        imageStream.Write( your_preview_Image_Byte_array, 0, your_preview_Image_Byte_array.Length);
    }
}


   private byte[] GetPreviewImage(System.IO.Packaging.Package package)
   {
        if (!package.RelationshipExists("preview"))
        {
            return null;
        }

        var previewRel = package.GetRelationship("preview");
        var previewUri = PackUriHelper.CreatePartUri(previewRel.TargetUri);

        var previewImagePart = package.GetPart(previewUri);
        using (var previewImageStream = previewImagePart.GetStream(FileMode.Open, FileAccess.Read))
        using (var ms = new MemoryStream())
        {
            previewImageStream.CopyTo(ms);
            return ms.ToArray();
        }
    }

并且要将您的 png 转换为字节数组,您可以执行 this

因此,好处是,您可以随后使用定义的关系 ID (="preview") 来访问您的预览图像一个opc容器

In my opinion good examples are all file formats using OPC

And an example in c# for an opc container with an preview image would be implemented f.e.:

using System.IO.Compression;
using System.IO.Packaging;

using (var package = Package.Open( your_Zip/OPC_File_Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
    // Add the Preview Image to the OPC container
    var imagePart = package.CreatePart( PackUriHelper.CreatePartUri(new Uri("preview.png", UriKind.Relative)), "image/png");
    package.CreateRelationship( imagePart.Uri, TargetMode.Internal, OpcConstants.RelationshipTypes.PREVIEWIMAGE, "preview");

    using(var imageStream = imagePart.GetStream( FileMode.Open, FileAccess.Write ))
    {
        imageStream.Write( your_preview_Image_Byte_array, 0, your_preview_Image_Byte_array.Length);
    }
}


   private byte[] GetPreviewImage(System.IO.Packaging.Package package)
   {
        if (!package.RelationshipExists("preview"))
        {
            return null;
        }

        var previewRel = package.GetRelationship("preview");
        var previewUri = PackUriHelper.CreatePartUri(previewRel.TargetUri);

        var previewImagePart = package.GetPart(previewUri);
        using (var previewImageStream = previewImagePart.GetStream(FileMode.Open, FileAccess.Read))
        using (var ms = new MemoryStream())
        {
            previewImageStream.CopyTo(ms);
            return ms.ToArray();
        }
    }

And to convert your png to an byte array you could do this

So the benefit would be, you could afterwards use your defined Relationship-Id (="preview") to access your previewImage in an opc container

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文