/** * 是否为XLSX格式的Excel文件(XSSF)<br> * XLSX文件主要用于Excel 2007+创建 * * @param in excel输入流 * @return 是否为XLSX格式的Excel文件(XSSF) */ public static boolean isXlsx(InputStream in) { if (false == in.markSupported()) { in = new BufferedInputStream(in); } try { return FileMagic.valueOf(in) == FileMagic.OOXML; } catch (IOException e) { throw new IORuntimeException(e); } } }
/** * 是否为XLSX格式的Excel文件(XSSF)<br> * XLSX文件主要用于Excel 2007+创建 * * @param in excel输入流 * @return 是否为XLSX格式的Excel文件(XSSF) */ public static boolean isXlsx(InputStream in) { if (false == in.markSupported()) { in = new BufferedInputStream(in); } try { return FileMagic.valueOf(in) == FileMagic.OOXML; } catch (IOException e) { throw new IORuntimeException(e); } } }
/** * Get the file magic of the supplied {@link File}<p> * * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean, * that the ZIP stream has leading junk bytes * * @param inp a file to be identified */ public static FileMagic valueOf(final File inp) throws IOException { try (FileInputStream fis = new FileInputStream(inp)) { final byte[] data = IOUtils.toByteArray(fis, 8); return FileMagic.valueOf(data); } }
/** * Checks that the supplied InputStream (which MUST * support mark and reset) has a OOXML (zip) header at the start of it.<p> * * If unsure if your InputStream does support mark / reset, * use {@link FileMagic#prepareToCheckMagic(InputStream)} to wrap it and make * sure to always use that, and not the original! * * @param inp An InputStream which supports either mark/reset * * @deprecated in 3.17-beta2, use {@link FileMagic#valueOf(InputStream)} == FileMagic.OOXML instead */ @Deprecated @Removal(version="4.0") public static boolean hasOOXMLHeader(InputStream inp) throws IOException { return FileMagic.valueOf(inp) == FileMagic.OOXML; } }
public static ExcelTypeEnum valueOf(InputStream inputStream){ try { if (!inputStream.markSupported()) { return null; } FileMagic fileMagic = FileMagic.valueOf(inputStream); if(FileMagic.OLE2.equals(fileMagic)){ return XLS; } if(FileMagic.OOXML.equals(fileMagic)){ return XLSX; } return null; } catch (IOException e) { throw new RuntimeException(e); } } }
/** * Get the file magic of the supplied InputStream (which MUST * support mark and reset).<p> * * If unsure if your InputStream does support mark / reset, * use {@link #prepareToCheckMagic(InputStream)} to wrap it and make * sure to always use that, and not the original!<p> * * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean, * that the ZIP stream has leading junk bytes * * @param inp An InputStream which supports either mark/reset */ public static FileMagic valueOf(InputStream inp) throws IOException { if (!inp.markSupported()) { throw new IOException("getFileMagic() only operates on streams which support mark(int)"); } // Grab the first 8 bytes byte[] data = IOUtils.peekFirst8Bytes(inp); return FileMagic.valueOf(data); }
/** * 是否为XLS格式的Excel文件(HSSF)<br> * XLS文件主要用于Excel 97~2003创建 * * @param in excel输入流 * @return 是否为XLS格式的Excel文件(HSSF) */ public static boolean isXls(InputStream in) { final PushbackInputStream pin = IoUtil.toPushbackStream(in, 8); try { return FileMagic.valueOf(pin) == FileMagic.OLE2; } catch (IOException e) { throw new IORuntimeException(e); } }
/** * 是否为XLS格式的Excel文件(HSSF)<br> * XLS文件主要用于Excel 97~2003创建 * * @param in excel输入流 * @return 是否为XLS格式的Excel文件(HSSF) */ public static boolean isXls(InputStream in) { final PushbackInputStream pin = IoUtil.toPushbackStream(in, 8); try { return FileMagic.valueOf(pin) == FileMagic.OLE2; } catch (IOException e) { throw new IORuntimeException(e); } }
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; } }
private void open(InputStream biffStream) throws IOException { BufferedInputStream bis = (biffStream instanceof BufferedInputStream) ? (BufferedInputStream)biffStream : new BufferedInputStream(biffStream, 8); if (FileMagic.valueOf(bis) == FileMagic.OLE2) { POIFSFileSystem poifs = new POIFSFileSystem(bis); try { open(poifs); toClose = poifs; // Fixed by GR, we should not close it here } finally { if (toClose == null) { poifs.close(); } } } else { ris = new RecordInputStream(bis); toClose = bis; prepare(); } }
FileMagic fm = FileMagic.valueOf(is);
> 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"); } }
final FileMagic fm = FileMagic.valueOf(is); if (fm == FileMagic.OLE2) { try (final POIFSFileSystem poifs = new POIFSFileSystem(is)) {
private static boolean hasPOIFSHeader(InputStream is) throws IOException { return FileMagic.valueOf(is) == FileMagic.OLE2; } }
public void close() throws IOException { objectPart.clear(); try (final OutputStream os = objectPart.getOutputStream()) { final ByteArrayInputStream bis = new ByteArrayInputStream(this.buf, 0, size()); final FileMagic fm = FileMagic.valueOf(this.buf); if (fm == FileMagic.OLE2) { try (final POIFSFileSystem poifs = new POIFSFileSystem(bis)) { poifs.getRoot().setStorageClsid(metaData.getClassID()); poifs.writeFilesystem(os); } } else if (metaData.getOleEntry() == null) { // OLE Name hasn't been specified, pass the input through os.write(this.buf, 0, size()); } else { try (final POIFSFileSystem poifs = new POIFSFileSystem()) { final ClassID clsId = metaData.getClassID(); if (clsId != null) { poifs.getRoot().setStorageClsid(clsId); } poifs.createDocument(bis, metaData.getOleEntry()); Ole10Native.createOleMarkerEntry(poifs); poifs.writeFilesystem(os); } } } } }
FileMagic fm = FileMagic.valueOf(data);