findbugs: check return value of File.mkdir(s), and don't accidentally catch RuntimeException

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717923 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-12-04 10:33:28 +00:00
parent d34beb335f
commit c17754165a
1 changed files with 25 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
@ -28,6 +29,7 @@ import java.util.zip.ZipFile;
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
import org.apache.poi.util.IOUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
@ -49,13 +51,32 @@ public final class XSSFDump {
}
}
}
private static void createDirIfMissing(File directory) throws RuntimeException {
if (!directory.exists()) {
boolean dirWasCreated = directory.mkdir();
if (!dirWasCreated) {
throw new RuntimeException("Unable to create directory: " + directory);
}
}
}
private static void recursivelyCreateDirIfMissing(File directory) throws RuntimeException {
if (!directory.exists()) {
boolean dirsWereCreated = directory.mkdirs();
if (!dirsWereCreated) {
throw new RuntimeException("Unable to recursively create directory: " + directory);
}
}
}
public static void dump(ZipFile zip) throws Exception {
String zipname = zip.getName();
int sep = zipname.lastIndexOf('.');
File root = new File(zipname.substring(0, sep));
root.mkdir();
System.out.println("Dupming to directory " + root);
createDirIfMissing(root);
System.out.println("Dumping to directory " + root);
Enumeration<? extends ZipEntry> en = zip.entries();
while(en.hasMoreElements()){
@ -64,7 +85,7 @@ public final class XSSFDump {
int idx = name.lastIndexOf('/');
if(idx != -1){
File bs = new File(root, name.substring(0, idx));
bs.mkdirs();
recursivelyCreateDirIfMissing(bs);
}
File f = new File(root, entry.getName());
@ -76,7 +97,7 @@ public final class XSSFDump {
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
xml.save(out, options);
} catch (Exception e){
} catch (XmlException e) {
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
IOUtils.copy(zip.getInputStream(entry), out);
}