package org.apache.poi.extractor;
import static org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.getCurrentUserPassword;
-import static org.apache.poi.poifs.crypt.EncryptionInfo.ENCRYPTION_INFO_ENTRY;
+import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
import java.io.File;
import java.io.IOException;
}
POIFSFileSystem poifs = new POIFSFileSystem(is);
- boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
+ DirectoryNode root = poifs.getRoot();
+ boolean isOOXML = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
- return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
+ return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(root, password));
}
public static POITextExtractor createExtractor(File file) throws IOException {
POIFSFileSystem poifs = new POIFSFileSystem(file, true);
try {
- boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
- return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
+ DirectoryNode root = poifs.getRoot();
+ boolean isOOXML = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
+ return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(root, password));
} catch (IOException | RuntimeException e) {
IOUtils.closeQuietly(poifs);
throw e;
public static POITextExtractor createExtractor(final DirectoryNode root, String password) throws IOException {
// Encrypted OOXML files go inside OLE2 containers, is this one?
- if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
+ if (root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
return wp(FileMagic.OOXML, w -> w.create(root, password));
} else {
return wp(FileMagic.OLE2, w -> w.create(root, password));
package org.apache.poi.sl.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
-import static org.apache.poi.poifs.crypt.EncryptionInfo.ENCRYPTION_INFO_ENTRY;
+import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
import java.io.BufferedInputStream;
import java.io.File;
}
POIFSFileSystem poifs = new POIFSFileSystem(is);
- boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
+ DirectoryNode root = poifs.getRoot();
+ boolean isOOXML = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
}
package org.apache.poi.ss.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
-import static org.apache.poi.poifs.crypt.EncryptionInfo.ENCRYPTION_INFO_ENTRY;
+import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
import java.io.BufferedInputStream;
import java.io.File;
import org.apache.poi.EmptyFileException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
*/
public static Workbook create(final DirectoryNode root, String password) throws IOException {
// Encrypted OOXML files go inside OLE2 containers, is this one?
- if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
+ if (root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
return wp(FileMagic.OOXML, w -> w.create(root, password));
} else {
return wp(FileMagic.OLE2, w -> w.create(root, password));
}
POIFSFileSystem poifs = new POIFSFileSystem(is);
- boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
+ DirectoryNode root = poifs.getRoot();
+ boolean isOOXML = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
- return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
+ return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(root, password));
}
/**
final boolean ooxmlEnc;
try (POIFSFileSystem fs = new POIFSFileSystem(file, true)) {
DirectoryNode root = fs.getRoot();
- ooxmlEnc = root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
+ ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
}
return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
}
public XSSFWorkbook create(DirectoryNode root, String password) throws IOException {
try (InputStream stream = DocumentFactoryHelper.getDecryptedStream(root, password)) {
return create(stream);
- } finally {
- // as we processed the full stream already, we can close the filesystem here
- // otherwise file handles are leaked
- root.getFileSystem().close();
+ // don't close root.getFileSystem() otherwise the container filesystem becomes invalid
}
}
package org.apache.poi.xssf.usermodel;
+import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.poi.POIDataSamples;
import org.apache.poi.common.usermodel.HyperlinkType;
-import org.apache.poi.hslf.usermodel.HSLFObjectData;
-import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.util.ZipSecureFile;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.sl.usermodel.ObjectShape;
+import org.apache.poi.sl.usermodel.Slide;
+import org.apache.poi.sl.usermodel.SlideShow;
+import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
int activeSheet = wb.getActiveSheetIndex();
}
}
-
+
+
+
@Test
public void testXLSXinPPT() throws Exception {
- try (HSLFSlideShow ppt = new HSLFSlideShow(
- HSLFTestDataSamples.openSampleFileStream("testPPT_oleWorkbook.ppt"))) {
- assertEquals(1, ppt.getEmbeddedObjects().length);
+ try (SlideShow<?,?> ppt = SlideShowFactory.create(
+ POIDataSamples.getSlideShowInstance().openResourceAsStream("testPPT_oleWorkbook.ppt"))) {
- HSLFObjectData data = ppt.getEmbeddedObjects()[0];
- assertEquals(null, data.getFileName());
+ Slide<?, ?> slide = ppt.getSlides().get(1);
+ ObjectShape<?,?> oleShape = (ObjectShape<?,?>)slide.getShapes().get(2);
+
+ org.apache.poi.sl.usermodel.ObjectData data = oleShape.getObjectData();
+ assertNull(data.getFileName());
// Will be OOXML wrapped in OLE2, not directly SpreadSheet
POIFSFileSystem fs = new POIFSFileSystem(data.getInputStream());
- assertEquals(true, fs.getRoot().hasEntry("Package"));
- assertEquals(false, fs.getRoot().hasEntry("Workbook"));
+ assertTrue(fs.getRoot().hasEntry(OOXML_PACKAGE));
+ assertFalse(fs.getRoot().hasEntry("Workbook"));
+
// Can fetch Package to get OOXML
- DocumentInputStream dis = new DocumentInputStream((DocumentEntry)fs.getRoot().getEntry("Package"));
- try (OPCPackage pkg = OPCPackage.open(dis)) {
- XSSFWorkbook wb = new XSSFWorkbook(pkg);
+ DirectoryNode root = fs.getRoot();
+ DocumentEntry docEntry = (DocumentEntry) root.getEntry(OOXML_PACKAGE);
+ try (DocumentInputStream dis = new DocumentInputStream(docEntry);
+ OPCPackage pkg = OPCPackage.open(dis);
+ XSSFWorkbook wb = new XSSFWorkbook(pkg)) {
assertEquals(1, wb.getNumberOfSheets());
- wb.close();
}
// Via the XSSF Factory
assertEquals(1, wb.getNumberOfSheets());
}
+
// Or can open via the normal Factory, as stream or OLE2
- /* this part test is currently broken - https://bz.apache.org/bugzilla/show_bug.cgi?id=64817
try (Workbook wb = WorkbookFactory.create(fs)) {
assertEquals(1, wb.getNumberOfSheets());
}
try (Workbook wb = WorkbookFactory.create(data.getInputStream())) {
assertEquals(1, wb.getNumberOfSheets());
}
- */
}
}
}