ZipArchiveEntry getNextEntry() throws IOException { if (!(in instanceof ZipArchiveInputStream)) { throw new IllegalStateException("getNextEntry() is only allowed for stream based zip processing."); } try { entry = ((ZipArchiveInputStream) in).getNextZipEntry(); return entry; } catch (ZipException ze) { if (ze.getMessage().startsWith("Unexpected record signature")) { throw new NotOfficeXmlFileException( "No valid entries or contents found, this is not a valid OOXML (Office Open XML) file", ze); } throw ze; } }
/** * 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; } }
/** * Constructor. Opens a Zip based Open XML document from a File. * * @param file * The file to open or create. * @param access * The package access mode. * @throws InvalidOperationException If the zip file cannot be opened. */ ZipPackage(File file, PackageAccess access) throws InvalidOperationException { super(access); ZipEntrySource ze; try { final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR ze = new ZipFileZipEntrySource(zipFile); } catch (IOException e) { // probably not happening with write access - not sure how to handle the default read-write access ... if (access == PackageAccess.WRITE) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e); } if ("java.util.zip.ZipException: archive is not a ZIP archive".equals(e.getMessage())) { throw new NotOfficeXmlFileException("archive is not a ZIP archive", e); } LOG.log(POILogger.ERROR, "Error in zip file "+file+" - falling back to stream processing (i.e. ignoring zip central directory)"); ze = openZipEntrySourceStream(file); } this.zipArchive = ze; }
throw new NotOfficeXmlFileException( "No valid entries or contents found, this is not a valid OOXML " + "(Office Open XML) file");
ZipArchiveEntry getNextEntry() throws IOException { if (!(in instanceof ZipArchiveInputStream)) { throw new IllegalStateException("getNextEntry() is only allowed for stream based zip processing."); } try { entry = ((ZipArchiveInputStream) in).getNextZipEntry(); return entry; } catch (ZipException ze) { if (ze.getMessage().startsWith("Unexpected record signature")) { throw new NotOfficeXmlFileException( "No valid entries or contents found, this is not a valid OOXML (Office Open XML) file", ze); } throw ze; } }
/** * 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; } }
/** * Constructor. Opens a Zip based Open XML document from a File. * * @param file * The file to open or create. * @param access * The package access mode. * @throws InvalidOperationException If the zip file cannot be opened. */ ZipPackage(File file, PackageAccess access) throws InvalidOperationException { super(access); ZipEntrySource ze; try { final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR ze = new ZipFileZipEntrySource(zipFile); } catch (IOException e) { // probably not happening with write access - not sure how to handle the default read-write access ... if (access == PackageAccess.WRITE) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e); } if ("java.util.zip.ZipException: archive is not a ZIP archive".equals(e.getMessage())) { throw new NotOfficeXmlFileException("archive is not a ZIP archive", e); } LOG.log(POILogger.ERROR, "Error in zip file "+file+" - falling back to stream processing (i.e. ignoring zip central directory)"); ze = openZipEntrySourceStream(file); } this.zipArchive = ze; }
throw new NotOfficeXmlFileException( "No valid entries or contents found, this is not a valid OOXML " + "(Office Open XML) file");