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:
parent
93497e5b0e
commit
17a4680cee
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user