christian@5022: package de.intevation.flys.utils; christian@5022: christian@5022: import de.intevation.artifacts.common.utils.FileTools; christian@5022: import de.intevation.flys.artifacts.states.FloodMapState; christian@5022: christian@5022: import java.io.File; christian@5022: import java.io.IOException; christian@5022: christian@5022: /** christian@5022: * Various utility methods for handling of files and directories. christian@5022: * @author Christian Lins christian@5022: */ christian@5022: public class FileUtils { christian@5022: christian@5022: public static boolean extractZipfile(File archive, File dir) { christian@5022: try { christian@5022: File tmpDir = new File(dir, "usr_tmp"); christian@5022: FileTools.extractArchive(archive, tmpDir); christian@5022: FileUtils.moveFiles(tmpDir, dir); christian@5022: christian@5022: return true; christian@5022: } christian@5022: catch (IOException ioe) { christian@5022: FloodMapState.logger.warn("Zip archive " + dir + "/" + FloodMapState.WSPLGEN_USER_ZIP + " could not be extracted."); christian@5022: return false; christian@5022: } christian@5022: } christian@5022: christian@5022: public static void moveFiles(File source, final File target) christian@5022: throws IOException christian@5022: { christian@5022: if (!source.exists()) { christian@5022: return; christian@5022: } christian@5022: if (!target.exists()) { christian@5022: target.mkdir(); christian@5022: } christian@5022: FileTools.walkTree(source, new FileTools.FileVisitor() { christian@5022: @Override christian@5022: public boolean visit(File file) { christian@5022: if (!file.isDirectory()) { christian@5022: String name = file.getName(); christian@5022: String suffix = ""; christian@5022: int pos = name.lastIndexOf('.'); christian@5022: if (pos > 0 && pos < name.length() - 1) { christian@5022: suffix = name.substring(pos + 1); christian@5022: } christian@5022: else { christian@5022: return true; christian@5022: } christian@5022: try { christian@5022: FileTools.copyFile(file, new File(target, FloodMapState.WSPLGEN_USER_FILENAME + "." + suffix)); christian@5022: } christian@5022: catch (IOException ioe) { christian@5022: FloodMapState.logger.warn ("Error while copying file " + file.getName()); christian@5022: return true; christian@5022: } christian@5022: } christian@5022: return true; christian@5022: } christian@5022: }); christian@5022: christian@5022: FileTools.deleteRecursive(source); christian@5022: } christian@5022: christian@5022: }