From: Maxim Valyanskiy Date: Thu, 14 Jan 2010 08:56:35 +0000 (+0000) Subject: ExtractorFactory: save OOXML stream into temporary file before text extraction -... X-Git-Tag: REL_3_7_BETA1~137 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4e3c970131b9ac56802c2a518e1b7dd9cfe39a86;p=poi.git ExtractorFactory: save OOXML stream into temporary file before text extraction - this reduces memory usage and allows temporary file cleanup git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@899123 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java index 24a2632be6..394313cdca 100644 --- a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java @@ -16,13 +16,7 @@ ==================================================================== */ package org.apache.poi.extractor; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.PushbackInputStream; +import java.io.*; import java.util.ArrayList; import java.util.Iterator; @@ -30,6 +24,8 @@ import org.apache.poi.POIOLE2TextExtractor; import org.apache.poi.POITextExtractor; import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLTextExtractor; +import org.apache.poi.util.TempFile; +import org.apache.poi.util.IOUtils; import org.apache.poi.hdgf.extractor.VisioTextExtractor; import org.apache.poi.hpbf.extractor.PublisherTextExtractor; import org.apache.poi.hslf.extractor.PowerPointExtractor; @@ -87,9 +83,22 @@ public class ExtractorFactory { if(POIFSFileSystem.hasPOIFSHeader(inp)) { return createExtractor(new POIFSFileSystem(inp)); } + if(POIXMLDocument.hasOOXMLHeader(inp)) { - return createExtractor(OPCPackage.open(inp)); - } + File file = TempFile.createTempFile("poi-ooxml-", ".tmp"); + + try { + FileOutputStream out = new FileOutputStream(file); + IOUtils.copy(inp, out); + out.close(); + + return createExtractor(OPCPackage.open(file.getPath())); + } finally { + if (file.exists()) { + file.delete(); + } + } + } throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); }