// so we don't modify the current position of the stream
File file = stream.getFile();
// Use a tagged stream to distinguish between real I/O problems
// and parse errors thrown as IOExceptions by POI.
TaggedInputStream tagged = new TaggedInputStream(
new BufferedInputStream(new FileInputStream(file)));
try {
// POIFSFileSystem might try close the stream
POIFSFileSystem fs =
new POIFSFileSystem(new CloseShieldInputStream(tagged));
// Optimize a possible later parsing process by keeping
// a reference to the already opened POI file system
stream.setOpenContainer(fs);
Set<String> names = new HashSet<String>();
for (Entry entry : fs.getRoot()) {
names.add(entry.getName());
}
return names;
} catch (IOException e) {
// Was this a real I/O problem?
tagged.throwIfCauseOf(e);
// Parse error in POI, so we don't know the file type
return Collections.emptySet();
} catch (RuntimeException e) {
// Another problem in POI
return Collections.emptySet();
} finally {
tagged.close();
}
}