當前位置: 首頁>>代碼示例>>C#>>正文


C# PackagePart.GetStream方法代碼示例

本文整理匯總了C#中System.IO.Packaging.PackagePart.GetStream方法的典型用法代碼示例。如果您正苦於以下問題:C# PackagePart.GetStream方法的具體用法?C# PackagePart.GetStream怎麽用?C# PackagePart.GetStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.IO.Packaging.PackagePart的用法示例。


在下文中一共展示了PackagePart.GetStream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExtractContentFile

        private static void ExtractContentFile(string rootFolder, PackagePart contentFile)
        {
            // Initially create file under the folder specified
            string contentFilePath = contentFile.Uri.OriginalString.Replace('/', Path.DirectorySeparatorChar);

            if (contentFilePath.StartsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
            {
                contentFilePath = contentFilePath.TrimStart(Path.DirectorySeparatorChar);
            }

            contentFilePath = Path.Combine(rootFolder, contentFilePath);

            //Check for the folder already exists. If not then create that folder
            if (!Directory.Exists(Path.GetDirectoryName(contentFilePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(contentFilePath));
            }

            using (var newFileStream = File.Create(contentFilePath))
            {
                newFileStream.Close();
                var content = new byte[contentFile.GetStream().Length];
                contentFile.GetStream().Read(content, 0, content.Length);
                File.WriteAllBytes(contentFilePath, content);
            }
        }
開發者ID:MYOB-Technology,項目名稱:AccountRight_SampleApp_CSharp,代碼行數:26,代碼來源:CompressionUtil.cs

示例2: GetContentsAsXml

    /// <summary>
    /// Returns the part contents in xml
    /// </summary>
    /// <param name="part">System.IO.Packaging.Packagepart</param>
    /// <returns></returns>
    static XElement GetContentsAsXml(PackagePart part)
    {
        XNamespace pkg = 
           "http://schemas.microsoft.com/office/2006/xmlPackage";
        if (part.ContentType.EndsWith("xml"))
        {
            using (Stream partstream = part.GetStream())
            using (StreamReader streamReader = new StreamReader(partstream))
            {
                string streamString = streamReader.ReadToEnd();
                XElement newXElement = 
                    new XElement(pkg + "part", new XAttribute(pkg + "name", part.Uri), 
                        new XAttribute(pkg + "contentType", part.ContentType), 
                        new XElement(pkg + "xmlData", XElement.Parse(streamString)));
                return newXElement;
            }
         }
        else
        {
            using (Stream str = part.GetStream())
            using (BinaryReader binaryReader = new BinaryReader(str))
            {
                int len = (int)binaryReader.BaseStream.Length;
                byte[] byteArray = binaryReader.ReadBytes(len);
                // the following expression creates the base64String, then chunks
                // it to lines of 76 characters long
                string base64String = (System.Convert.ToBase64String(byteArray))
                    .Select
                    (
                        (c, i) => new
                        {
                            Character = c,
                            Chunk = i / 76
                        }
                    )
                    .GroupBy(c => c.Chunk)
                    .Aggregate(
                        new StringBuilder(),
                        (s, i) =>
                            s.Append(
                                i.Aggregate(
                                    new StringBuilder(),
                                    (seed, it) => seed.Append(it.Character),
                                    sb => sb.ToString()
                                )
                            )
                            .Append(Environment.NewLine),
                        s => s.ToString()
                    );

                return new XElement(pkg + "part",
                    new XAttribute(pkg + "name", part.Uri),
                    new XAttribute(pkg + "contentType", part.ContentType),
                    new XAttribute(pkg + "compression", "store"),
                    new XElement(pkg + "binaryData", base64String)
                );
            }
        }
    }
開發者ID:EdiCarlos,項目名稱:MyPractices,代碼行數:64,代碼來源:OpcHelper.cs

示例3: ExcelPicture

        internal ExcelPicture(ExcelDrawings drawings, XmlNode node)
            : base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name")
        {
            XmlNode picNode = node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip", drawings.NameSpaceManager);
            if (picNode != null)
            {
                RelPic = drawings.Part.GetRelationship(picNode.Attributes["r:embed"].Value);
                UriPic = PackUriHelper.ResolvePartUri(drawings.UriDrawing, RelPic.TargetUri);

                Part = drawings.Part.Package.GetPart(UriPic);
                FileInfo f = new FileInfo(UriPic.OriginalString);
                ContentType = GetContentType(f.Extension);
                _image = Image.FromStream(Part.GetStream());
                ImageConverter ic=new ImageConverter();
                var iby=(byte[])ic.ConvertTo(_image, typeof(byte[]));
                var ii = _drawings._package.LoadImage(iby, UriPic, Part);
                ImageHash = ii.Hash;

                string relID = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@r:id");
                if (!string.IsNullOrEmpty(relID))
                {
                    HypRel = drawings.Part.GetRelationship(relID);
                    if (HypRel.TargetUri.IsAbsoluteUri)
                    {
                        _hyperlink = new ExcelHyperLink(HypRel.TargetUri.AbsoluteUri);
                    }
                    else
                    {
                        _hyperlink = new ExcelHyperLink(HypRel.TargetUri.OriginalString, UriKind.Relative);
                    }
                    ((ExcelHyperLink)_hyperlink).ToolTip = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@tooltip");
                }
            }
        }
開發者ID:pruiz,項目名稱:EPPlus,代碼行數:34,代碼來源:ExcelPicture.cs

示例4: UpdatePackageManifest

        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);
        }
開發者ID:dolly22,項目名稱:CloudServicePackageTools,代碼行數:26,代碼來源:ServiceDescriptionVisitor.cs

示例5: TrySignPackagePart

        private static bool TrySignPackagePart(PackagePart packagePart, string keyPath, string keyPassword, bool signedPackage)
        {
            if (packagePart.Uri.ToString().EndsWith(".exe")
                || packagePart.Uri.ToString().EndsWith(".dll"))
            {
                string tempPath = Path.GetTempFileName();
                try
                {
                    using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        packagePart.GetStream().CopyTo(stream);
                    }

                    if (!SigningHelper.GetAssemblyInfo(tempPath).IsSigned)
                    {
                        signedPackage = true;

                        SigningHelper.SignAssembly(tempPath, keyPath ?? string.Empty, keyPassword ?? string.Empty);

                        using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Read))
                        {
                            stream.CopyTo(packagePart.GetStream(FileMode.Create, FileAccess.Write));
                        }
                    }
                }
                finally
                {
                    File.Delete(tempPath);
                }
            }
            return signedPackage;
        }
開發者ID:MyGet,項目名稱:webhooks-sign-package,代碼行數:32,代碼來源:PackageSigner.cs

示例6: UpdateServiceDescriptionPart

        protected override void UpdateServiceDescriptionPart(PackagePart serviceDescriptionPart)
        {
            var definitionSerializer = new XmlSerializer(typeof(ServiceDefinition));

            var packageStream = serviceDescriptionPart.GetStream(FileMode.Open, FileAccess.ReadWrite);
            var serviceDefinition = definitionSerializer.Deserialize(packageStream) as ServiceDefinition;

            UpdateServiceDefinition(serviceDefinition);

            // rewind package stream
            packageStream.Seek(0, SeekOrigin.Begin);
            packageStream.SetLength(0);

            var writerSettings = new XmlWriterSettings()
            {
                Indent = true,
                CloseOutput = false
            };

            // write back to output
            using (var writer = XmlWriter.Create(packageStream, writerSettings))
            {
                definitionSerializer.Serialize(writer, serviceDefinition);
            }
        }
開發者ID:dolly22,項目名稱:CloudServicePackageTools,代碼行數:25,代碼來源:XmlServiceDescriptionVisitor.cs

示例7: CopyPart

 private static void CopyPart(PackagePart oldPart, PackagePart newPart)
 {
     using (Stream oldPartStream = oldPart.GetStream(),
                   newPartStream = newPart.GetStream(FileMode.OpenOrCreate))
     {
         oldPartStream.CopyTo(newPartStream);
     }
 }
開發者ID:tonyedgecombe,項目名稱:Deinterleave,代碼行數:8,代碼來源:OPCCopier.cs

示例8: ExtractPart

        void ExtractPart(PackagePart part, string destinationPath)
        {
            fileSystem.EnsureDirectoryExists(Path.GetDirectoryName(destinationPath));

            using (var packageFileStream = part.GetStream())
            using (var destinationFileStream = fileSystem.OpenFile(destinationPath, FileMode.Create))
            {
                packageFileStream.CopyTo(destinationFileStream);
                destinationFileStream.Flush();
            }
        }
開發者ID:bjewell52,項目名稱:Calamari,代碼行數:11,代碼來源:ExtractAzureCloudServicePackageConvention.cs

示例9: ZipPackageFile

        public ZipPackageFile(PackagePart part) 
            : base(UriUtility.GetPath(part.Uri))
        {
            Debug.Assert(part != null, "part should not be null");

            byte[] buffer;
            using (Stream partStream = part.GetStream())
            {
                buffer = partStream.ReadAllBytes();
            }
            _streamFactory = () => new MemoryStream(buffer);
        }
開發者ID:campersau,項目名稱:NuGetPackageExplorer,代碼行數:12,代碼來源:ZipPackageFile.cs

示例10: GetFileHash

 public string GetFileHash(PackagePart part)
 {
     using (var inputStream = part.GetStream())
     using (var md5 = new MD5CryptoServiceProvider())
     {   var hash = md5.ComputeHash(inputStream);
         var sb = new StringBuilder();
         foreach (var b in hash)
         {
             sb.Append(string.Format("{0:X2}", b));
         }
         return sb.ToString();
     }
 }
開發者ID:irium,項目名稱:Pepita,代碼行數:13,代碼來源:IntegrationTests.cs

示例11: ZipPackageFile

        public ZipPackageFile(PackagePart part)
        {
            Debug.Assert(part != null, "part should not be null");

            byte[] buffer;
            using (Stream partStream = part.GetStream()) {
                using (var stream = new MemoryStream()) {
                    partStream.CopyTo(stream);
                    buffer = stream.ToArray();
                }
            }
            _path = UriUtility.GetPath(part.Uri);
            _streamFactory = () => new MemoryStream(buffer);
        }
開發者ID:jacksonh,項目名稱:nuget,代碼行數:14,代碼來源:ZipPackageFile.cs

示例12: UpdateServiceDescriptionPackage

        private void UpdateServiceDescriptionPackage(PackagePart servicePackageDescriptionPart)
        {
            // open service description package
            var descPackage = Package.Open(servicePackageDescriptionPart.GetStream(FileMode.Open, FileAccess.ReadWrite), FileMode.Open, FileAccess.ReadWrite);
            var descPackageRelation = descPackage.GetRelationship("SERVICEDESCRIPTION");
            var descPackagePart = descPackage.GetPart(descPackageRelation.TargetUri);

            // update service description content
            UpdateServiceDescriptionPart(descPackagePart);

            // update inner package manifest
            UpdatePackageManifest(descPackage, descPackagePart);
            descPackage.Flush();
            descPackage.Close();
        }
開發者ID:dolly22,項目名稱:CloudServicePackageTools,代碼行數:15,代碼來源:ServiceDescriptionVisitor.cs

示例13: Extract

        private static void Extract(PackagePart part, string outPath)
        {
            string outFileName = Path.Combine(outPath, part.Uri.OriginalString.Substring(1));

            if (!Directory.Exists(Path.GetDirectoryName(outFileName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outFileName));
            }

            using (System.IO.FileStream outFileStream = new System.IO.FileStream(outFileName, FileMode.Create))
            {
                using (Stream inFileStream = part.GetStream())
                {
                    CopyStream(inFileStream, outFileStream);
                }
            }
        }
開發者ID:cvietor,項目名稱:mvcmef,代碼行數:17,代碼來源:ZipUtil.cs

示例14: ExcelDrawings

        public ExcelDrawings(ExcelPackage xlPackage, ExcelWorksheet sheet)
        {
            _drawingsXml = new XmlDocument();
                _drawingsXml.PreserveWhitespace = true;
                _drawings = new Dictionary<int, ExcelDrawing>();

                XmlNode node = sheet.WorksheetXml.SelectSingleNode("//d:drawing", sheet.NameSpaceManager);
                CreateNSM();
                if (node != null)
                {
                    PackageRelationship drawingRelation = sheet.Part.GetRelationship(node.Attributes["r:id"].Value);
                    _uriDrawing = PackUriHelper.ResolvePartUri(sheet.WorksheetUri, drawingRelation.TargetUri);

                    _part = xlPackage.Package.GetPart(_uriDrawing);
                    _drawingsXml.Load(_part.GetStream());

                    AddDrawings();
                }
        }
開發者ID:huoxudong125,項目名稱:EPPlus,代碼行數:19,代碼來源:ExcelDrawings.cs

示例15: CopyObfuscatedFontPart

        private static void CopyObfuscatedFontPart(PackagePart oldPart, PackagePart newPart)
        {
            var guidArray = GetGUID(oldPart.Uri);

            using (Stream oldPartStream = oldPart.GetStream(),
                          newPartStream = newPart.GetStream(FileMode.OpenOrCreate),
                          memoryStream = new MemoryStream())
            {
                oldPartStream.CopyTo(memoryStream);

                for (int i = 0; i < 32; i++)
                {
                    (memoryStream as MemoryStream).GetBuffer()[i] ^= guidArray[15 - (i % 16)];
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.CopyTo(newPartStream);
            }
        }
開發者ID:tonyedgecombe,項目名稱:Deinterleave,代碼行數:19,代碼來源:OPCCopier.cs


注:本文中的System.IO.Packaging.PackagePart.GetStream方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。