コード例 #1
0
        private void UpdatePackageManifest(Package package, PackagePart updatedPart)
        {
            if (package == null)
                throw new ArgumentNullException(nameof(package));
            if (updatedPart == null)
                throw new ArgumentNullException(nameof(updatedPart));
            if (package.FileOpenAccess != FileAccess.ReadWrite)
                throw new InvalidOperationException("Package must be open for reading and writing");

            var manifestRelation = package.GetRelationship("MANIFEST");
            var manifestPart = package.GetPart(manifestRelation.TargetUri);

            // parse manifest
            var manifest = new PackageManifest(manifestPart, null);

            // rehash updated part
            var csDefPart = manifest.Items.FirstOrDefault(i => i.PartUri == updatedPart.Uri);
            if (csDefPart == null)
                throw new InvalidOperationException(string.Format("Unable to find part '{0}' in package manifest", updatedPart.Uri));

            csDefPart.Hash = manifest.HashAlgorithm.ComputeHash(updatedPart.GetStream(FileMode.Open, FileAccess.Read)); ;
            csDefPart.ModifiedDate = DateTime.UtcNow;

            var manifestStream = manifestPart.GetStream(FileMode.Open, FileAccess.Write);
            manifest.WriteToStream(manifestStream);
        }