public void TestOleNative() { POIFSFileSystem fs = new POIFSFileSystem(dataSamples.OpenResourceAsStream("oleObject1.bin")); Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject(fs); Assert.AreEqual("File1.svg", ole.Label); Assert.AreEqual("D:\\Documents and Settings\\rsc\\My Documents\\file1.svg", ole.Command); }
public void TestFiles() { FileStream[] files = { // bug 51891 POIDataSamples.GetPOIFSInstance().GetFile("multimedia.doc"), // tika bug 1072 POIDataSamples.GetPOIFSInstance().GetFile("20-Force-on-a-current-S00.doc"), // other files Containing ole10native records ... POIDataSamples.GetDocumentInstance().GetFile("Bug53380_3.doc"), POIDataSamples.GetDocumentInstance().GetFile("Bug47731.doc") }; foreach (FileStream f in files) { NPOIFSFileSystem fs = new NPOIFSFileSystem(f, true); List <Entry> entries = new List <Entry>(); FindOle10(entries, fs.Root, "/", ""); foreach (Entry e in entries) { MemoryStream bosExp = new MemoryStream(); Stream is1 = ((DirectoryNode)e.Parent).CreateDocumentInputStream(e); IOUtils.Copy(is1, bosExp); is1.Close(); Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)e.Parent); MemoryStream bosAct = new MemoryStream(); ole.WriteOut(bosAct); //assertThat(bosExp.ToByteArray(), EqualTo(bosAct.ToByteArray())); Assert.IsTrue(Arrays.Equals(bosExp.ToArray(), bosAct.ToArray())); } fs.Close(); } }
private static void ExtractFile(EmbeddedObjectPart part, string destinationFolderPath) { // Determine the file name and destination path of the binary, // structured storage file. string binaryFileName = Path.GetFileName(part.Uri.ToString()); string binaryFilePath = Path.Combine(destinationFolderPath, binaryFileName); // Ensure the destination directory exists. Directory.CreateDirectory(destinationFolderPath); // Copy part contents to structured storage file. using (Stream partStream = part.GetStream()) using (FileStream fileStream = File.Create(binaryFilePath)) { partStream.CopyTo(fileStream); } // Extract the embedded file from the structured storage file. Ole10Native.ExtractFile(binaryFilePath, destinationFolderPath); // Remove the structured storage file. File.Delete(binaryFilePath); }
/// <summary> /// This method will extract and save the data from the given <see cref="storage"/> node to the <see cref="outputFolder"/> /// </summary> /// <param name="storage">The <see cref="CFStorage"/> node</param> /// <param name="outputFolder">The outputFolder</param> /// <param name="fileName">The fileName to use, null when the fileName is unknown</param> /// <returns>Returns the name of the created file that or null if there was nothing to export within the given <see cref="storage"/> node.</returns> /// <exception cref="Exceptions.OEFileIsPasswordProtected">Raised when a WordDocument, WorkBook or PowerPoint Document stream is password protected</exception> public static string SaveFromStorageNode(CFStorage storage, string outputFolder, string fileName) { Logger.WriteToLog($"Saving CFStorage to output folder '{outputFolder}' with file name {fileName}"); var contents = storage.TryGetStream("CONTENTS"); if (contents != null) { Logger.WriteToLog("CONTENTS stream found"); if (contents.Size <= 0) { Logger.WriteToLog("CONTENTS stream is empty"); return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } const string delimiter = "%DocumentOle:"; var documentOleFileName = GetDelimitedStringFromData(delimiter, contents.GetData()); if (documentOleFileName != null) { if (!documentOleFileName.Equals(string.Empty)) { fileName = Path.GetFileName(documentOleFileName); } contents.SetData(contents.GetData().Skip(delimiter.Length * 2 + documentOleFileName.Length).ToArray()); } return(SaveByteArrayToFile(contents.GetData(), FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } var package = storage.TryGetStream("Package"); if (package != null) { Logger.WriteToLog("Package stream found"); if (package.Size <= 0) { Logger.WriteToLog("Package stream is empty"); return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } return(SaveByteArrayToFile(package.GetData(), FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } var embeddedOdf = storage.TryGetStream("EmbeddedOdf"); if (embeddedOdf != null) { Logger.WriteToLog("EmbeddedOdf stream found"); // The embedded object is an Embedded ODF file if (embeddedOdf.Size <= 0) { Logger.WriteToLog("EmbeddedOdf stream is empty"); return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } return(SaveByteArrayToFile(embeddedOdf.GetData(), FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } if (storage.TryGetStream("\x0001Ole10Native") != null) { Logger.WriteToLog("Ole10Native stream found"); var ole10Native = new Ole10Native(storage); Logger.WriteToLog($"Ole10Native stream format is '{ole10Native.Format}'"); if (ole10Native.Format == OleFormat.File) { return(SaveByteArrayToFile(ole10Native.NativeData, FileManager.FileExistsMakeNew(Path.Combine(outputFolder, ole10Native.FileName)))); } Logger.WriteToLog("Ole10Native is ignored"); return(null); } if (storage.TryGetStream("WordDocument") != null) { Logger.WriteToLog("WordDocument stream found"); // The embedded object is a Word file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded Word document.doc"; } return(SaveStorageTreeToCompoundFile(storage, FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } if (storage.TryGetStream("Workbook") != null) { Logger.WriteToLog("Workbook stream found"); // The embedded object is an Excel file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded Excel document.xls"; } Excel.SetWorkbookVisibility(storage); return(SaveStorageTreeToCompoundFile(storage, FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } if (storage.TryGetStream("PowerPoint Document") != null) { Logger.WriteToLog("PowerPoint Document stream found"); // The embedded object is a PowerPoint file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded PowerPoint document.ppt"; } return(SaveStorageTreeToCompoundFile(storage, FileManager.FileExistsMakeNew(Path.Combine(outputFolder, fileName)))); } return(null); }
/// <summary> /// This method will extract and save the data from the given <see cref="storage"/> node to the <see cref="outputFolder"/> /// </summary> /// <param name="storage">The <see cref="CFStorage"/> node</param> /// <param name="outputFolder">The outputFolder</param> /// <param name="fileName">The fileName to use, null when the fileName is unknown</param> /// <returns></returns> /// <exception cref="OEFileIsPasswordProtected">Raised when a WordDocument, WorkBook or PowerPoint Document stream is password protected</exception> public static string SaveFromStorageNode(CFStorage storage, string outputFolder, string fileName) { if (storage.ExistsStream("CONTENTS")) { var contents = storage.GetStream("CONTENTS"); if (contents.Size <= 0) { return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } return(SaveByteArrayToFile(contents.GetData(), Path.Combine(outputFolder, fileName))); } if (storage.ExistsStream("Package")) { var package = storage.GetStream("Package"); if (package.Size <= 0) { return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } return(SaveByteArrayToFile(package.GetData(), Path.Combine(outputFolder, fileName))); } if (storage.ExistsStream("EmbeddedOdf")) { // The embedded object is an Embedded ODF file var package = storage.GetStream("EmbeddedOdf"); if (package.Size <= 0) { return(null); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = DefaultEmbeddedObjectName; } return(SaveByteArrayToFile(package.GetData(), Path.Combine(outputFolder, fileName))); } if (storage.ExistsStream("\x0001Ole10Native")) { var ole10Native = new Ole10Native(storage); return(ole10Native.Format == OleFormat.File ? SaveByteArrayToFile(ole10Native.NativeData, Path.Combine(outputFolder, ole10Native.FileName)) : null); } if (storage.ExistsStream("WordDocument")) { // The embedded object is a Word file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded Word document.doc"; } return(SaveStorageTreeToCompoundFile(storage, Path.Combine(outputFolder, fileName))); } if (storage.ExistsStream("Workbook")) { // The embedded object is an Excel file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded Excel document.xls"; } Excel.SetWorkbookVisibility(storage); return(SaveStorageTreeToCompoundFile(storage, Path.Combine(outputFolder, fileName))); } if (storage.ExistsStream("PowerPoint Document")) { // The embedded object is a PowerPoint file if (string.IsNullOrWhiteSpace(fileName)) { fileName = "Embedded PowerPoint document.ppt"; } return(SaveStorageTreeToCompoundFile(storage, Path.Combine(outputFolder, fileName))); } return(null); }
public void TestReallyEmbedSomething() { HSSFWorkbook wb = new HSSFWorkbook(); ISheet sheet = wb.CreateSheet(); HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch; byte[] pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png"); byte[] picturePPT = POIDataSamples.GetSlideShowInstance().ReadFile("clock.jpg"); int imgIdx = wb.AddPicture(pictureData, PictureType.PNG); POIFSFileSystem pptPoifs = GetSamplePPT(); int pptIdx = wb.AddOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt"); POIFSFileSystem xlsPoifs = GetSampleXLS(); int imgPPT = wb.AddPicture(picturePPT, PictureType.JPEG); int xlsIdx = wb.AddOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls"); int txtIdx = wb.AddOlePackage(GetSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt"); int rowoffset = 5; int coloffset = 5; ICreationHelper ch = wb.GetCreationHelper(); HSSFClientAnchor anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(2 + coloffset), 1 + rowoffset, 0, 0, (short)(3 + coloffset), 5 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, pptIdx, imgPPT); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(5 + coloffset), 1 + rowoffset, 0, 0, (short)(6 + coloffset), 5 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, xlsIdx, imgIdx); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(3 + coloffset), 10 + rowoffset, 0, 0, (short)(5 + coloffset), 11 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, txtIdx, imgIdx); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(1 + coloffset), -2 + rowoffset, 0, 0, (short)(7 + coloffset), 14 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); HSSFSimpleShape circle = patriarch.CreateSimpleShape(anchor); circle.ShapeType = (/*setter*/ HSSFSimpleShape.OBJECT_TYPE_OVAL); circle.IsNoFill = (/*setter*/ true); if (false) { FileStream fos = new FileStream("embed.xls", FileMode.Create); wb.Write(fos); fos.Close(); } wb = HSSFTestDataSamples.WriteOutAndReadBack(wb as HSSFWorkbook); MemoryStream bos = new MemoryStream(); HSSFObjectData od = wb.GetAllEmbeddedObjects()[0]; Ole10Native ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); bos = new MemoryStream(); pptPoifs.WriteFileSystem(bos); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray())); od = wb.GetAllEmbeddedObjects()[1]; ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); bos = new MemoryStream(); xlsPoifs.WriteFileSystem(bos); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray())); od = wb.GetAllEmbeddedObjects()[2]; ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, GetSampleTXT())); }