DumpPOIFS: Create a separate named sub-directory for each file to not overwrite each other when passing in multiple files for dumping, check for failing mkdirs()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1737031 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-03-29 18:44:11 +00:00
parent 992adb9c16
commit 33cf4c310b

View File

@ -46,46 +46,48 @@ public class POIFSDump {
} }
boolean dumpProps = false, dumpMini = false; boolean dumpProps = false, dumpMini = false;
for (int i = 0; i < args.length; i++) { for (String filename : args) {
if (args[i].equalsIgnoreCase("-dumprops") || if (filename.equalsIgnoreCase("-dumprops") ||
args[i].equalsIgnoreCase("-dump-props") || filename.equalsIgnoreCase("-dump-props") ||
args[i].equalsIgnoreCase("-dump-properties")) { filename.equalsIgnoreCase("-dump-properties")) {
dumpProps = true; dumpProps = true;
continue; continue;
} }
if (args[i].equalsIgnoreCase("-dumpmini") || if (filename.equalsIgnoreCase("-dumpmini") ||
args[i].equalsIgnoreCase("-dump-mini") || filename.equalsIgnoreCase("-dump-mini") ||
args[i].equalsIgnoreCase("-dump-ministream") || filename.equalsIgnoreCase("-dump-ministream") ||
args[i].equalsIgnoreCase("-dump-mini-stream")) { filename.equalsIgnoreCase("-dump-mini-stream")) {
dumpMini = true; dumpMini = true;
continue; continue;
} }
System.out.println("Dumping " + args[i]); System.out.println("Dumping " + filename);
FileInputStream is = new FileInputStream(args[i]); FileInputStream is = new FileInputStream(filename);
NPOIFSFileSystem fs = new NPOIFSFileSystem(is); NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
is.close(); is.close();
DirectoryEntry root = fs.getRoot(); DirectoryEntry root = fs.getRoot();
File file = new File(root.getName()); File file = new File(new File(filename).getName(), root.getName());
file.mkdir(); if (!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " + file);
}
dump(root, file); dump(root, file);
if (dumpProps) { if (dumpProps) {
HeaderBlock header = fs.getHeaderBlock(); HeaderBlock header = fs.getHeaderBlock();
dump(fs, header.getPropertyStart(), "properties", file); dump(fs, header.getPropertyStart(), "properties", file);
} }
if (dumpMini) { if (dumpMini) {
NPropertyTable props = fs.getPropertyTable(); NPropertyTable props = fs.getPropertyTable();
int startBlock = props.getRoot().getStartBlock(); int startBlock = props.getRoot().getStartBlock();
if (startBlock == POIFSConstants.END_OF_CHAIN) { if (startBlock == POIFSConstants.END_OF_CHAIN) {
System.err.println("No Mini Stream in file"); System.err.println("No Mini Stream in file");
} else { } else {
dump(fs, startBlock, "mini-stream", file); dump(fs, startBlock, "mini-stream", file);
} }
} }
fs.close(); fs.close();
} }
} }
@ -108,7 +110,9 @@ public class POIFSDump {
} else if (entry instanceof DirectoryEntry){ } else if (entry instanceof DirectoryEntry){
DirectoryEntry dir = (DirectoryEntry)entry; DirectoryEntry dir = (DirectoryEntry)entry;
File file = new File(parent, entry.getName()); File file = new File(parent, entry.getName());
file.mkdir(); if(!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " + file);
}
dump(dir, file); dump(dir, file);
} else { } else {
System.err.println("Skipping unsupported POIFS entry: " + entry); System.err.println("Skipping unsupported POIFS entry: " + entry);