Skip to content

Commit 2af3ae0

Browse files
authored
1 parent cb1b9fe commit 2af3ae0

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

tika-core/src/main/java/org/apache/tika/extractor/RUnpackExtractor.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.io.InputStream;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26+
import java.nio.file.StandardCopyOption;
2627

27-
import org.apache.commons.io.input.CloseShieldInputStream;
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030
import org.xml.sax.ContentHandler;
@@ -56,6 +56,7 @@ public class RUnpackExtractor extends ParsingEmbeddedDocumentExtractor {
5656

5757
private EmbeddedBytesSelector embeddedBytesSelector = EmbeddedBytesSelector.ACCEPT_ALL;
5858

59+
private final EmbeddedStreamTranslator embeddedStreamTranslator = new DefaultEmbeddedStreamTranslator();
5960
private long bytesExtracted = 0;
6061
private final long maxEmbeddedBytesForExtraction;
6162

@@ -113,13 +114,20 @@ private void parseWithBytes(TikaInputStream stream, ContentHandler handler, Meta
113114
throws TikaException, IOException, SAXException {
114115
//TODO -- improve the efficiency of this so that we're not
115116
//literally writing out a file per request
116-
Path p = stream.getPath();
117-
try {
118-
//warp in CloseShieldInputStream to ensure that a misbehaving parser isn't closing
119-
//the stream and thereby deleting the temp file.
120-
parse(CloseShieldInputStream.wrap(stream), handler, metadata);
117+
Path tmp = Files.createTempFile("tika-tmp-", ".bin");
118+
if (embeddedStreamTranslator.shouldTranslate(stream, metadata)) {
119+
Files.copy(embeddedStreamTranslator.translate(stream, metadata), tmp, StandardCopyOption.REPLACE_EXISTING);
120+
} else {
121+
Files.copy(stream, tmp, StandardCopyOption.REPLACE_EXISTING);
122+
}
123+
try (TikaInputStream tmpTis = TikaInputStream.get(tmp)) {
124+
parse(tmpTis, handler, metadata);
121125
} finally {
122-
storeEmbeddedBytes(p, metadata);
126+
try {
127+
storeEmbeddedBytes(tmp, metadata);
128+
} finally {
129+
Files.delete(tmp);
130+
}
123131
}
124132
}
125133

0 commit comments

Comments
 (0)