Exemple #1
0
        protected virtual void CreatePackage(Stream stream)
        {
            using (Package package = ZipPackage.Open(stream, FileMode.Create))
            {
                Uri         collectionUri = new Uri("/Content/Collection.xml", UriKind.Relative);
                PackagePart part          = package.CreatePart(collectionUri, System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Maximum);

                //Serialize the model into the part stream
                XmlFormatter formatter = Formatter;
                formatter.Shallow = false;

                formatter.Serialize(part.GetStream(), Model);

                package.CreateRelationship(part.Uri, TargetMode.Internal, "Main Document");

                //Loop through all resources and create parts
                foreach (string key in Formatter.Resources.Keys)
                {
                    Uri           resourceUri = new Uri(key, UriKind.Relative);
                    ResourceEntry entry       = Formatter.Resources[key];

                    //Need to use a valid mime type otherwise a null reference error occurs
                    PackagePart resourcePart = package.CreatePart(resourceUri, entry.GetMimeType(), CompressionOption.Maximum);
                    entry.SaveResourceStream(resourcePart.GetStream());
                    PackageRelationship relationship = package.CreateRelationship(resourcePart.Uri, TargetMode.Internal, "Resource");
                }

                //Close the package and save all the underlying streams
                package.Close();
            }
        }