本文整理汇总了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);
}
}
示例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)
);
}
}
}
示例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");
}
}
}
示例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);
}
示例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;
}
示例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);
}
}
示例7: CopyPart
private static void CopyPart(PackagePart oldPart, PackagePart newPart)
{
using (Stream oldPartStream = oldPart.GetStream(),
newPartStream = newPart.GetStream(FileMode.OpenOrCreate))
{
oldPartStream.CopyTo(newPartStream);
}
}
示例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();
}
}
示例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);
}
示例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();
}
}
示例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);
}
示例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();
}
示例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);
}
}
}
示例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();
}
}
示例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);
}
}