/// <summary> /// Tries to get the part with the specified <paramref name="partUri"/>. /// </summary> /// <param name="partUri">The URI of the part to get.</param> /// <param name="part">The part with the specified <paramref name="partUri"/> if it exists; otherwise, null.</param> /// <returns>true if a part with the specified <paramref name="partUri"/> exists; otherwise, false.</returns> internal bool TryGetPart(Uri partUri, out ZipPackagePart part) { var partExists = this.PartExists(partUri); part = partExists ? Parts.Single(x => x.Key.Equals(GetUriKey(partUri.OriginalString), StringComparison.InvariantCultureIgnoreCase)).Value : null; return(partExists); }
internal ZipPackagePart CreatePart(Uri partUri, string contentType, CompressionLevel compressionLevel) { if (PartExists(partUri)) { throw (new InvalidOperationException("Part already exist")); } var part = new ZipPackagePart(this, partUri, contentType, compressionLevel); _contentTypes.Add(GetUriKey(part.Uri.OriginalString), new ContentType(contentType, false, part.Uri.OriginalString)); Parts.Add(GetUriKey(part.Uri.OriginalString), part); return(part); }
internal void Save(Stream stream) { var enc = Encoding.UTF8; ZipOutputStream os = new ZipOutputStream(stream, true); os.EnableZip64 = Zip64Option.AsNecessary; os.CompressionLevel = (OfficeOpenXml.Packaging.Ionic.Zlib.CompressionLevel)_compression; /**** ContentType****/ var entry = os.PutNextEntry("[Content_Types].xml"); byte[] b = enc.GetBytes(GetContentTypeXml()); os.Write(b, 0, b.Length); /**** Top Rels ****/ _rels.WriteZip(os, $"_rels/.rels"); ZipPackagePart ssPart = null; foreach (var part in Parts.Values) { if (part.ContentType != ContentTypes.contentTypeSharedString) { part.WriteZip(os); } else { ssPart = part; } } //Shared strings must be saved after all worksheets. The ss dictionary is populated when that workheets are saved (to get the best performance). if (ssPart != null) { ssPart.WriteZip(os); } os.Flush(); os.Close(); os.Dispose(); //return ms; }
internal ImageInfo LoadImage(byte[] image, Uri uri, Packaging.ZipPackagePart imagePart) { #if (Core) var hashProvider = SHA1.Create(); #else var hashProvider = new SHA1CryptoServiceProvider(); #endif var hash = BitConverter.ToString(hashProvider.ComputeHash(image)).Replace("-", ""); if (_images.ContainsKey(hash)) { _images[hash].RefCount++; } else { _images.Add(hash, new ImageInfo() { Uri = uri, RefCount = 1, Hash = hash, Part = imagePart }); } return(_images[hash]); }
internal ZipPackagePart CreatePart(Uri partUri, string contentType, CompressionLevel compressionLevel, string extension = null) { if (PartExists(partUri)) { throw (new InvalidOperationException("Part already exist")); } var part = new ZipPackagePart(this, partUri, contentType, compressionLevel); if (string.IsNullOrEmpty(extension)) { _contentTypes.Add(GetUriKey(part.Uri.OriginalString), new ContentType(contentType, false, part.Uri.OriginalString)); } else { if (!_contentTypes.ContainsKey(extension)) { _contentTypes.Add(extension, new ContentType(contentType, true, extension)); } } Parts.Add(GetUriKey(part.Uri.OriginalString), part); return(part); }
internal MemoryStream Save() { var ms = new MemoryStream(); var enc = Encoding.UTF8; ZipOutputStream os = new ZipOutputStream(ms, true); os.CompressionLevel = (Ionic.Zlib.CompressionLevel)_compression; /**** ContentType****/ var entry = os.PutNextEntry("[Content_Types].xml"); byte[] b = enc.GetBytes(GetContentTypeXml()); os.Write(b, 0, b.Length); /**** Top Rels ****/ _rels.WriteZip(os, "_rels\\.rels"); ZipPackagePart ssPart = null; foreach (var part in Parts.Values) { if (part.ContentType != ExcelPackage.contentTypeSharedString) { part.WriteZip(os); } else { ssPart = part; } } //Shared strings must be saved after all worksheets. The ss dictionary is populated when that workheets are saved (to get the best performance). if (ssPart != null) { ssPart.WriteZip(os); } os.Flush(); os.Close(); os.Dispose(); return(ms); }
/// <summary> /// Create or read the XML for the workbook. /// </summary> private void CreateWorkbookXml(XmlNamespaceManager namespaceManager) { if (_package.Package.PartExists(WorkbookUri)) { _workbookXml = _package.GetXmlFromUri(WorkbookUri); } else { // create a new workbook part and add to the package Packaging.ZipPackagePart partWorkbook = _package.Package.CreatePart(WorkbookUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", _package.Compression); // create the workbook _workbookXml = new XmlDocument(namespaceManager.NameTable); _workbookXml.PreserveWhitespace = ExcelPackage.preserveWhitespace; // create the workbook element XmlElement wbElem = _workbookXml.CreateElement("workbook", ExcelPackage.schemaMain); // Add the relationships namespace wbElem.SetAttribute("xmlns:r", ExcelPackage.schemaRelationships); _workbookXml.AppendChild(wbElem); // create the bookViews and workbooks element XmlElement bookViews = _workbookXml.CreateElement("bookViews", ExcelPackage.schemaMain); wbElem.AppendChild(bookViews); XmlElement workbookView = _workbookXml.CreateElement("workbookView", ExcelPackage.schemaMain); bookViews.AppendChild(workbookView); // save it to the package StreamWriter stream = new StreamWriter(partWorkbook.GetStream(FileMode.Create, FileAccess.Write)); _workbookXml.Save(stream); //stream.Close(); _package.Package.Flush(); } }
/// <summary> /// Saves the XmlDocument into the package at the specified Uri. /// </summary> /// <param name="uri">The Uri of the component</param> /// <param name="xmlDoc">The XmlDocument to save</param> internal void SavePart(Uri uri, XmlDocument xmlDoc) { Packaging.ZipPackagePart part = this.Package.GetPart(uri); xmlDoc.Save(part.GetStream(FileMode.Create, FileAccess.Write)); }
internal ZipPackage(Stream stream) { bool hasContentTypeXml = false; if (stream == null || stream.Length == 0) { AddNew(); } else { var rels = new Dictionary <string, string>(); stream.Seek(0, SeekOrigin.Begin); using (ZipInputStream zip = new ZipInputStream(stream)) { var e = zip.GetNextEntry(); if (e == null) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } while (e != null) { GetDirSeparator(e); if (e.UncompressedSize > 0) { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); if (e.FileName.Equals("[content_types].xml", StringComparison.OrdinalIgnoreCase)) { AddContentTypes(Encoding.UTF8.GetString(b)); hasContentTypeXml = true; } else if (e.FileName.Equals($"_rels{_dirSeparator}.rels", StringComparison.OrdinalIgnoreCase)) { ReadRelation(Encoding.UTF8.GetString(b), ""); } else { if (e.FileName.EndsWith(".rels", StringComparison.OrdinalIgnoreCase)) { rels.Add(GetUriKey(e.FileName), Encoding.UTF8.GetString(b)); } else { var part = new ZipPackagePart(this, e); part.Stream = RecyclableMemory.GetStream(); part.Stream.Write(b, 0, b.Length); Parts.Add(GetUriKey(e.FileName), part); } } } e = zip.GetNextEntry(); } if (_dirSeparator == '0') { _dirSeparator = '/'; } foreach (var p in Parts) { string name = Path.GetFileName(p.Key); string extension = Path.GetExtension(p.Key); string relFile = string.Format("{0}_rels/{1}.rels", p.Key.Substring(0, p.Key.Length - name.Length), name); if (rels.ContainsKey(relFile)) { p.Value.ReadRelation(rels[relFile], p.Value.Uri.OriginalString); } if (_contentTypes.ContainsKey(p.Key)) { p.Value.ContentType = _contentTypes[p.Key].Name; } else if (extension.Length > 1 && _contentTypes.ContainsKey(extension.Substring(1))) { p.Value.ContentType = _contentTypes[extension.Substring(1)].Name; } } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } zip.Close(); zip.Dispose(); } } }
internal ExcelChart(ExcelDrawings drawings, XmlNode node, Uri uriChart, ZipPackagePart part, XmlDocument chartXml, XmlNode chartNode) : base(drawings, node, "xdr:graphicFrame/xdr:nvGraphicFramePr/xdr:cNvPr/@name") { UriChart = uriChart; Part = part; ChartXml = chartXml; _chartNode = chartNode; InitChartLoad(drawings, chartNode); ChartType = GetChartType(chartNode.LocalName); }
internal ZipPackage(Stream stream) { bool hasContentTypeXml = false; if (stream == null || stream.Length == 0) { AddNew(); } else { var rels = new Dictionary<string, string>(); stream.Seek(0, SeekOrigin.Begin); using (ZipInputStream zip = new ZipInputStream(stream)) { var e = zip.GetNextEntry(); while (e != null) { if (e.UncompressedSize > 0) { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); if (e.FileName.Equals("[content_types].xml", StringComparison.InvariantCultureIgnoreCase)) { AddContentTypes(Encoding.UTF8.GetString(b)); hasContentTypeXml = true; } else if (e.FileName.Equals("_rels/.rels", StringComparison.InvariantCultureIgnoreCase)) { ReadRelation(Encoding.UTF8.GetString(b), ""); } else { if (e.FileName.EndsWith(".rels", StringComparison.InvariantCultureIgnoreCase)) { rels.Add(GetUriKey(e.FileName), Encoding.UTF8.GetString(b)); } else { var part = new ZipPackagePart(this, e); part.Stream = new MemoryStream(); part.Stream.Write(b, 0, b.Length); Parts.Add(GetUriKey(e.FileName), part); } } } else { } e = zip.GetNextEntry(); } foreach (var p in Parts) { string name = Path.GetFileName(p.Key); string extension = Path.GetExtension(p.Key); string relFile = string.Format("{0}_rels/{1}.rels", p.Key.Substring(0, p.Key.Length - name.Length), name); if (rels.ContainsKey(relFile)) { p.Value.ReadRelation(rels[relFile], p.Value.Uri.OriginalString); } if (_contentTypes.ContainsKey(p.Key)) { p.Value.ContentType = _contentTypes[p.Key].Name; } else if (extension.Length > 1 && _contentTypes.ContainsKey(extension.Substring(1))) { p.Value.ContentType = _contentTypes[extension.Substring(1)].Name; } } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } zip.Close(); } } }
internal ZipPackagePart CreatePart(Uri partUri, string contentType, CompressionLevel compressionLevel) { if (PartExists(partUri)) { throw (new InvalidOperationException("Part already exist")); } var part = new ZipPackagePart(this, partUri, contentType, compressionLevel); _contentTypes.Add(GetUriKey(part.Uri.OriginalString), new ContentType(contentType, false, part.Uri.OriginalString)); Parts.Add(GetUriKey(part.Uri.OriginalString), part); return part; }
internal ZipPackage(Stream stream) { bool hasContentTypeXml = false; if (stream == null || stream.Length == 0) { AddNew(); } else { var rels = new Dictionary <string, string>(); stream.Seek(0, SeekOrigin.Begin); using (ZipInputStream zip = new ZipInputStream(stream)) { var e = zip.GetNextEntry(); while (e != null) { if (e.UncompressedSize > 0) { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); if (e.FileName.ToLower() == "[content_types].xml") { AddContentTypes(Encoding.UTF8.GetString(b)); hasContentTypeXml = true; } else if (e.FileName.ToLower() == "_rels/.rels") { ReadRelation(Encoding.UTF8.GetString(b), ""); } else { if (e.FileName.ToLower().EndsWith(".rels")) { rels.Add(GetUriKey(e.FileName), Encoding.UTF8.GetString(b)); } else { var part = new ZipPackagePart(this, e); part.Stream = new MemoryStream(b); Parts.Add(GetUriKey(e.FileName), part); } } } else { } e = zip.GetNextEntry(); } foreach (var p in Parts) { FileInfo fi = new FileInfo(p.Key); string relFile = string.Format("{0}_rels/{1}.rels", p.Key.Substring(0, p.Key.Length - fi.Name.Length), fi.Name); if (rels.ContainsKey(relFile)) { p.Value.ReadRelation(rels[relFile], p.Value.Uri.OriginalString); } if (_contentTypes.ContainsKey(p.Key)) { p.Value.ContentType = _contentTypes[p.Key].Name; } else if (fi.Extension.Length > 1 && _contentTypes.ContainsKey(fi.Extension.Substring(1))) { p.Value.ContentType = _contentTypes[fi.Extension.Substring(1)].Name; } } if (!hasContentTypeXml) { throw (new FileFormatException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } if (!hasContentTypeXml) { throw (new FileFormatException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } zip.Close(); } } }
internal ExcelBubbleChart(ExcelDrawings drawings, XmlNode node, Uri uriChart, ZipPackagePart part, XmlDocument chartXml, XmlNode chartNode) : base(drawings, node, uriChart, part, chartXml, chartNode) { _chartSeries = new ExcelBubbleChartSeries(this, _drawings.NameSpaceManager, _chartNode, false); //SetTypeProperties(); }