private void GetXlsXSheetsProtection(ZipEntryFinder zipEntryFinder, Stream stream) { var openXmlFormat = OpenXmlFormatIdentifier.Resolve(stream); int sheetIndex = 1; string sheetPath = string.Format(@"xl\worksheets\sheet{0}.xml", sheetIndex); while (zipEntryFinder.HasEntry(sheetPath)) { ZipEntry zipEntry = zipEntryFinder.GetEntry(sheetPath); if (zipEntry == null) break; var zipFile = new ZipFile(stream) { IsStreamOwner = false }; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; CommonNamespaces commonNamespaces = new CommonNamespaces(new NameTable(), openXmlFormat); settings.NameTable = commonNamespaces.NameTable; XmlReader reader = XmlReader.Create(zipFile.GetInputStream(zipEntry), settings); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name.ToLowerInvariant() == "sheetprotection") { m_fileData.DocumentProtected = true; } } } sheetPath = string.Format(@"xl\worksheets\sheet{0}.xml", ++sheetIndex); } }
private void GetDocXDocumentProtection(ZipEntryFinder zipEntryFinder, Stream stream) { var openXmlFormat = OpenXmlFormatIdentifier.Resolve(stream); m_fileData.WritePasswordProtected = false; m_fileData.DocumentProtected = false; ZipEntry zipEntry = zipEntryFinder.GetEntry(@"word\settings.xml"); if (zipEntry == null) return; var zipFile = new ZipFile(stream) {IsStreamOwner = false}; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; CommonNamespaces commonNamespaces = new CommonNamespaces(new NameTable(), openXmlFormat); settings.NameTable = commonNamespaces.NameTable; XmlReader reader = XmlReader.Create(zipFile.GetInputStream(zipEntry), settings); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name.ToLowerInvariant() == "w:documentprotection") { string attValue = reader.GetAttribute("w:enforcement"); if (attValue == "1") { m_fileData.DocumentProtected = true; } } else if (reader.Name.ToLowerInvariant() == "w:writeprotection") { m_fileData.WritePasswordProtected = true; } } } }
private bool InitialiserMethodForXlsXFile() { if (!IsZipFile()) { return Workshare.Interop.Options.OptionApi.GetBool("UseDiskBasedFiles") ? InitialiseMethodForEncryptedXlsxFileOnDisk() : InitialiseMethodForEncryptedXlsxFile(); } using (Stream stream = m_fileData.BinaryFileData.AsStream()) using (ZipEntryFinder zipEntryFinder = new ZipEntryFinder(stream, false)) { if (!zipEntryFinder.HasEntry("[content_types].xml")) return false; ZipEntry zipEntry = zipEntryFinder.GetEntry(@"xl\workbook.xml"); if (zipEntry == null) return false; //Method to look at the XML within the document and determine the correct file type. //If we don't find a file type, then we assume that the type is ExcelSheetX FileType type = DetermineFileTypeForOfficeXMLFiles(zipEntryFinder, stream); switch (type) { case FileType.Unknown: type = FileType.ExcelSheetX; break; //Office 2003 compatibility packs don't support 2007 template types...so we don't either. case FileType.ExcelSheetTemplateX: case FileType.ExcelSheetMacroTemplateX: if (OfficeApplicationVersion.IsWord2003()) { type = FileType.Unknown; } break; } m_fileData.FileType = type; GetXlsXWriteProtection(zipEntry, stream); GetXlsXSheetsProtection(zipEntryFinder, stream); m_impl = new FileImpl(m_fileData); Logger.LogInfo(".xlsx;.xlsm;.xltx;.xltm file detected"); return true; } }
/// <summary> /// Looks at the Content_Types.xml within the document and looks for a specific line /// that specifies the document type /// </summary> /// <param name="zipEntryFinder">Access to the Office file contents</param> /// <param name="stream">The file we're inspecting</param> /// <returns>The file type found</returns> private FileType DetermineFileTypeForOfficeXMLFiles(ZipEntryFinder zipEntryFinder, Stream stream) { var openXmlFormat = OpenXmlFormatIdentifier.Resolve(stream); ZipEntry zipEntry = zipEntryFinder.GetEntry(@"[Content_Types].xml"); if (zipEntry == null) { return FileType.Unknown; } ZipFile zipFile = new ZipFile(stream); if (zipFile == null) { return FileType.Unknown; } zipFile.IsStreamOwner = false; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; CommonNamespaces commonNamespaces = new CommonNamespaces(new NameTable(), openXmlFormat); settings.NameTable = commonNamespaces.NameTable; XmlReader reader = XmlReader.Create(zipFile.GetInputStream(zipEntry), settings); FileType type = FileType.Unknown; while (reader.Read()) { if (reader.NodeType != XmlNodeType.Element) { continue; } if (!reader.Name.Equals("override", StringComparison.OrdinalIgnoreCase)) { continue; } string partName = reader.GetAttribute("PartName"); switch (partName) { case "/word/document.xml": type = FileType.WordDocumentX; break; case "/xl/workbook.xml": type = FileType.ExcelSheetX; break; case "/ppt/presentation.xml": type = FileType.PowerPointX; break; default: continue; } if (FindFormatTypeFromXml(reader, ref type)) { break; } } return type; }