/*
* Concept profile generation tool suite
* Copyright (C) 2015 Biosemantics Group, Erasmus University Medical Center,
* Rotterdam, The Netherlands
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.erasmusmc.utilities;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DirectoryUtilities {
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
public static void copyDirectory(File sourceLocation , File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
public static List<File> getFileListing(File dir) {
List<File> result = new ArrayList<File>();
for(String filename: dir.list())
result.add(new File(dir, filename));
return result;
}
private static List<String> getPathList(File f) {
List<String> l = new ArrayList<String>();
File r;
r = f.getAbsoluteFile();
while(r != null) {
l.add(r.getName());
r = r.getParentFile();
}
return l;
}
private static String matchPathLists(List<String> r,List<String> f) {
int i;
int j;
String s;
// start at the beginning of the lists
// iterate while both lists are equal
s = "";
i = r.size()-1;
j = f.size()-1;
// first eliminate common root
while((i >= 0)&&(j >= 0)&&(r.get(i).equals(f.get(j)))) {
i--;
j--;
}
// for each remaining level in the home path, add a ..
for(;i>=0;i--) {
s += ".." + File.separator;
}
// for each level in the file path, add the path
for(;j>=1;j--) {
s += f.get(j) + File.separator;
}
// file name
s += f.get(j);
return s;
}
public static String getRelativePath(File home,File f){
return matchPathLists(getPathList(home),getPathList(f));
}
}