public VBAMacroReader(InputStream rstream) throws IOException { InputStream is = FileMagic.prepareToCheckMagic(rstream); FileMagic fm = FileMagic.valueOf(is); if (fm == FileMagic.OLE2) { fs = new POIFSFileSystem(is); } else { openOOXML(is); } }
/** * @return does this ObjectData have an associated POIFS Directory Entry? * (Not all do, those that don't have a data portion) */ default boolean hasDirectoryEntry() { try (final InputStream is = FileMagic.prepareToCheckMagic(getInputStream())) { FileMagic fm = FileMagic.valueOf(is); return fm == FileMagic.OLE2; } catch (IOException e) { POILogger LOG = POILogFactory.getLogger(ObjectData.class); LOG.log(POILogger.WARN, "Can't determine filemagic of ole stream", e); return false; } }
/** * Opens the specified stream as a secure zip * * @param stream * The stream to open. * @return The zip stream freshly open. */ @SuppressWarnings("resource") public static ZipArchiveThresholdInputStream openZipStream(InputStream stream) throws IOException { // Peek at the first few bytes to sanity check InputStream checkedStream = FileMagic.prepareToCheckMagic(stream); verifyZipHeader(checkedStream); // Open as a proper zip stream return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(checkedStream)); }
/** * Verifies that the given stream starts with a Zip structure. * * Warning - this will consume the first few bytes of the stream, * you should push-back or reset the stream after use! */ private static void verifyZipHeader(InputStream stream) throws NotOfficeXmlFileException, IOException { InputStream is = FileMagic.prepareToCheckMagic(stream); FileMagic fm = FileMagic.valueOf(is); switch (fm) { case OLE2: throw new OLE2NotOfficeXmlFileException( "The supplied data appears to be in the OLE2 Format. " + "You are calling the part of POI that deals with OOXML "+ "(Office Open XML) Documents. You need to call a different " + "part of POI to process this data (eg HSSF instead of XSSF)"); case XML: throw new NotOfficeXmlFileException( "The supplied data appears to be a raw XML file. " + "Formats such as Office 2003 XML are not supported"); default: // Don't check for a Zip header, as to maintain backwards // compatibility we need to let them seek over junk at the // start before beginning processing. break; } }
InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is);
P extends TextParagraph<S,P,? extends TextRun> > SlideShow<S,P> create(InputStream inp, String password) throws IOException, EncryptedDocumentException { InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is);
@Override public boolean hasDirectoryEntry() { InputStream is = null; try { is = getObjectPart().getInputStream(); is = FileMagic.prepareToCheckMagic(is); return FileMagic.valueOf(is) == FileMagic.OLE2; } catch (IOException e) { LOG.log(POILogger.WARN, "can't determine if directory entry exists", e); return false; } finally { IOUtils.closeQuietly(is); } }
public static POITextExtractor createExtractor(InputStream inp) throws IOException, OpenXML4JException, XmlException { InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is); switch (fm) { case OLE2: POIFSFileSystem fs = new POIFSFileSystem(is); boolean isEncrypted = fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY); return isEncrypted ? createEncryptedOOXMLExtractor(fs) : createExtractor(fs); case OOXML: return createExtractor(OPCPackage.open(is)); default: throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); } }
try (final InputStream is = FileMagic.prepareToCheckMagic(readObjectDataRaw())) { final FileMagic fm = FileMagic.valueOf(is); if (fm == FileMagic.OLE2) {
/** * Takes an InputStream, verifies that it's not RTF or PDF, builds a * POIFSFileSystem from it, and returns that. */ public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException { InputStream is = FileMagic.prepareToCheckMagic(istream); FileMagic fm = FileMagic.valueOf(is); if (fm != FileMagic.OLE2) { throw new IllegalArgumentException("The document is really a "+fm+" file"); } return new POIFSFileSystem(is); }
public VBAMacroReader(InputStream rstream) throws IOException { InputStream is = FileMagic.prepareToCheckMagic(rstream); FileMagic fm = FileMagic.valueOf(is); if (fm == FileMagic.OLE2) { fs = new POIFSFileSystem(is); } else { openOOXML(is); } }
/** * @return does this ObjectData have an associated POIFS Directory Entry? * (Not all do, those that don't have a data portion) */ default boolean hasDirectoryEntry() { try (final InputStream is = FileMagic.prepareToCheckMagic(getInputStream())) { FileMagic fm = FileMagic.valueOf(is); return fm == FileMagic.OLE2; } catch (IOException e) { POILogger LOG = POILogFactory.getLogger(ObjectData.class); LOG.log(POILogger.WARN, "Can't determine filemagic of ole stream", e); return false; } }
/** * Verifies that the given stream starts with a Zip structure. * * Warning - this will consume the first few bytes of the stream, * you should push-back or reset the stream after use! */ private static void verifyZipHeader(InputStream stream) throws NotOfficeXmlFileException, IOException { InputStream is = FileMagic.prepareToCheckMagic(stream); FileMagic fm = FileMagic.valueOf(is); switch (fm) { case OLE2: throw new OLE2NotOfficeXmlFileException( "The supplied data appears to be in the OLE2 Format. " + "You are calling the part of POI that deals with OOXML "+ "(Office Open XML) Documents. You need to call a different " + "part of POI to process this data (eg HSSF instead of XSSF)"); case XML: throw new NotOfficeXmlFileException( "The supplied data appears to be a raw XML file. " + "Formats such as Office 2003 XML are not supported"); default: // Don't check for a Zip header, as to maintain backwards // compatibility we need to let them seek over junk at the // start before beginning processing. break; } }
/** * Opens the specified stream as a secure zip * * @param stream * The stream to open. * @return The zip stream freshly open. */ @SuppressWarnings("resource") public static ZipArchiveThresholdInputStream openZipStream(InputStream stream) throws IOException { // Peek at the first few bytes to sanity check InputStream checkedStream = FileMagic.prepareToCheckMagic(stream); verifyZipHeader(checkedStream); // Open as a proper zip stream return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(checkedStream)); }
InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is);
P extends TextParagraph<S,P,? extends TextRun> > SlideShow<S,P> create(InputStream inp, String password) throws IOException, EncryptedDocumentException { InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is);
@Override public boolean hasDirectoryEntry() { InputStream is = null; try { is = getObjectPart().getInputStream(); is = FileMagic.prepareToCheckMagic(is); return FileMagic.valueOf(is) == FileMagic.OLE2; } catch (IOException e) { LOG.log(POILogger.WARN, "can't determine if directory entry exists", e); return false; } finally { IOUtils.closeQuietly(is); } }
@Override public ExcelReader create(String fileName, final InputStream is) throws ExcelParseException { boolean useFile = fileName != null; if (useFile && is != null) { throw new IllegalArgumentException("Only one argument can be non-null"); } InputStream tempStream = null; try { tempStream = FileMagic.prepareToCheckMagic(useFile ? new FileInputStream(fileName) : is); // Opening the file by name is preferred because using an InputStream has a higher memory footprint than using a File if (isXlsx(tempStream)) { return useFile ? new SAXReader(fileName) : new SAXReader(tempStream); } else { return useFile ? new EventReader(fileName) : new EventReader(tempStream); } } catch (IOException e) { throw new ExcelParseException(e); } finally { IOUtils.closeQuietly(tempStream); } } /**
public static POITextExtractor createExtractor(InputStream inp) throws IOException, OpenXML4JException, XmlException { InputStream is = FileMagic.prepareToCheckMagic(inp); FileMagic fm = FileMagic.valueOf(is); switch (fm) { case OLE2: POIFSFileSystem fs = new POIFSFileSystem(is); boolean isEncrypted = fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY); return isEncrypted ? createEncryptedOOXMLExtractor(fs) : createExtractor(fs); case OOXML: return createExtractor(OPCPackage.open(is)); default: throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); } }
try (final InputStream is = FileMagic.prepareToCheckMagic(readObjectDataRaw())) { final FileMagic fm = FileMagic.valueOf(is); if (fm == FileMagic.OLE2) {