Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/utils/FileUtils.java @ 5022:a9243df307b1 mapgenfix
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Tue, 19 Feb 2013 13:41:20 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FileUtils.java Tue Feb 19 13:41:20 2013 +0100 @@ -0,0 +1,66 @@ +package de.intevation.flys.utils; + +import de.intevation.artifacts.common.utils.FileTools; +import de.intevation.flys.artifacts.states.FloodMapState; + +import java.io.File; +import java.io.IOException; + +/** + * Various utility methods for handling of files and directories. + * @author <a href="mailto:christian.lins@intevation.de">Christian Lins</a> + */ +public class FileUtils { + + public static boolean extractZipfile(File archive, File dir) { + try { + File tmpDir = new File(dir, "usr_tmp"); + FileTools.extractArchive(archive, tmpDir); + FileUtils.moveFiles(tmpDir, dir); + + return true; + } + catch (IOException ioe) { + FloodMapState.logger.warn("Zip archive " + dir + "/" + FloodMapState.WSPLGEN_USER_ZIP + " could not be extracted."); + return false; + } + } + + public static void moveFiles(File source, final File target) + throws IOException + { + if (!source.exists()) { + return; + } + if (!target.exists()) { + target.mkdir(); + } + FileTools.walkTree(source, new FileTools.FileVisitor() { + @Override + public boolean visit(File file) { + if (!file.isDirectory()) { + String name = file.getName(); + String suffix = ""; + int pos = name.lastIndexOf('.'); + if (pos > 0 && pos < name.length() - 1) { + suffix = name.substring(pos + 1); + } + else { + return true; + } + try { + FileTools.copyFile(file, new File(target, FloodMapState.WSPLGEN_USER_FILENAME + "." + suffix)); + } + catch (IOException ioe) { + FloodMapState.logger.warn ("Error while copying file " + file.getName()); + return true; + } + } + return true; + } + }); + + FileTools.deleteRecursive(source); + } + +}