none
Issue while creating package relationship in .net 4 (System.IO.Packaging) RRS feed

  • Question

  • The following code will generate a invalid package file. The internal part \_rels\content1.data.rels will be corrupted.

    At soon as I use the CompressionOption.NotCompressed instead of CompressionOption.Normal then the problem is solved. Is there something I'm doing wrong or this is a .Net internal bug? 

    This is compiled with .Net 4

    using System;
    using System.IO;
    using System.IO.Packaging;
    using System.Net.Mime;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (Stream s = File.Open(args[0],FileMode.Create,FileAccess.ReadWrite))
                {
                    Package package = Package.Open(s, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    PackagePart c1 = package.CreatePart(new Uri("/content1.data",UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);//This produces a corrupt package part
                    PackagePart c2 = package.CreatePart(new Uri("/content2.data", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.NotCompressed);
                    PackagePart res1 = package.CreatePart(new Uri("/resource1.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
                    PackagePart res2 = package.CreatePart(new Uri("/resource2.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
                    c1.CreateRelationship(res1.Uri, TargetMode.Internal, "relationshipType");
                    c2.CreateRelationship(res2.Uri, TargetMode.Internal, "relationshipType");
                    package.Flush();
                }
            }
        }
    }

    Wednesday, August 6, 2014 1:12 PM

Answers

  • Hello,

    >> Is there something I'm doing wrong or this is a .Net internal bug?

    What package type do you want to generate? If zip, you could try with below code without using the Stream:

    using (Package package = Package.Open("Output.zip", FileMode.OpenOrCreate))
    
                {
    
                    PackagePart c1 = package.CreatePart(new Uri("/content1.data", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);//This produces a corrupt package part
    
                    PackagePart c2 = package.CreatePart(new Uri("/content2.data", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.NotCompressed);
    
                    PackagePart res1 = package.CreatePart(new Uri("/resource1.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
    
                    PackagePart res2 = package.CreatePart(new Uri("/resource2.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
    
                    c1.CreateRelationship(res1.Uri, TargetMode.Internal, "relationshipType");
    
                    c2.CreateRelationship(res2.Uri, TargetMode.Internal, "relationshipType");
    
                    package.Flush();
    
                }
    

    It seems that the Stream is not incompatible with this Package class.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 7, 2014 2:56 AM
    Moderator

All replies

  • Hello,

    >> Is there something I'm doing wrong or this is a .Net internal bug?

    What package type do you want to generate? If zip, you could try with below code without using the Stream:

    using (Package package = Package.Open("Output.zip", FileMode.OpenOrCreate))
    
                {
    
                    PackagePart c1 = package.CreatePart(new Uri("/content1.data", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);//This produces a corrupt package part
    
                    PackagePart c2 = package.CreatePart(new Uri("/content2.data", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.NotCompressed);
    
                    PackagePart res1 = package.CreatePart(new Uri("/resource1.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
    
                    PackagePart res2 = package.CreatePart(new Uri("/resource2.res", UriKind.Relative), MediaTypeNames.Application.Octet, CompressionOption.Normal);
    
                    c1.CreateRelationship(res1.Uri, TargetMode.Internal, "relationshipType");
    
                    c2.CreateRelationship(res2.Uri, TargetMode.Internal, "relationshipType");
    
                    package.Flush();
    
                }
    

    It seems that the Stream is not incompatible with this Package class.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 7, 2014 2:56 AM
    Moderator
  • Thank, it work now
    Friday, August 8, 2014 2:53 PM