Skip to content

Commit 701323a

Browse files
authored
TIKA-4533 - third time's the charm -- further refinement (#2382)
1 parent 3c7660f commit 701323a

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.OutputStream;
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
27-
import java.nio.file.StandardCopyOption;
2827

2928
import org.slf4j.Logger;
3029
import org.slf4j.LoggerFactory;
@@ -113,30 +112,31 @@ public void parseEmbedded(
113112

114113
private void parseWithBytes(TikaInputStream tis, ContentHandler handler, Metadata metadata) throws TikaException, IOException, SAXException {
115114

116-
Path tmp = Files.createTempFile("tika-tmp-", ".bin");
115+
//trigger spool to disk
116+
Path rawBytes = tis.getPath();
117+
118+
//There may be a "translated" path for OLE2 etc
119+
Path translated = null;
117120
try {
118121
//translate the stream or not
119122
if (embeddedStreamTranslator.shouldTranslate(tis, metadata)) {
120-
try (OutputStream os = Files.newOutputStream(tmp)) {
123+
translated = Files.createTempFile("tika-tmp-", ".bin");
124+
try (OutputStream os = Files.newOutputStream(translated)) {
121125
embeddedStreamTranslator.translate(tis, metadata, os);
122126
}
123-
} else {
124-
Files.copy(tis, tmp, StandardCopyOption.REPLACE_EXISTING);
125-
}
126-
127-
//now do the parse
128-
if (tis.getOpenContainer() != null) {
129-
parse(tis, handler, metadata);
130-
} else {
131-
try (TikaInputStream tisTmp = TikaInputStream.get(tmp)) {
132-
parse(tisTmp, handler, metadata);
133-
}
134127
}
128+
parse(tis, handler, metadata);
135129
} finally {
136130
try {
137-
storeEmbeddedBytes(tmp, metadata);
131+
if (translated != null) {
132+
storeEmbeddedBytes(translated, metadata);
133+
} else {
134+
storeEmbeddedBytes(rawBytes, metadata);
135+
}
138136
} finally {
139-
Files.delete(tmp);
137+
if (translated != null) {
138+
Files.delete(translated);
139+
}
140140
}
141141
}
142142
}

tika-core/src/test/java/org/apache/tika/TikaTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ protected List<Metadata> getRecursiveMetadata(Path path, ParseContext context,
399399
}
400400
}
401401

402+
protected List<Metadata> getRecursiveMetadata(Path path, Parser parser, ParseContext parseContext,
403+
boolean suppressException) throws Exception {
404+
Metadata metadata = new Metadata();
405+
try (TikaInputStream tis = TikaInputStream.get(path, metadata)) {
406+
return getRecursiveMetadata(tis, parser, metadata, parseContext,
407+
suppressException);
408+
}
409+
}
410+
402411
protected List<Metadata> getRecursiveMetadata(Path path, Parser parser,
403412
boolean suppressException) throws Exception {
404413
Metadata metadata = new Metadata();

0 commit comments

Comments
 (0)