开发者

What is the purpose of Package Relationships in .NET?

开发者 https://www.devze.com 2023-01-30 08:46 出处:网络
What exactly is a Package Relationship (ZipPackage) in .NET? I know what a Package is but I\'m having trouble understanding the purpose of r开发者_开发问答elationships. Could you give examples of when

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消