2012-06-25 17:29:16 +02:00
|
|
|
package processing.app.helpers;
|
|
|
|
|
|
|
|
import java.io.File;
|
2012-10-18 16:49:14 +02:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2012-06-25 17:29:16 +02:00
|
|
|
import java.io.IOException;
|
2012-10-19 10:35:48 +02:00
|
|
|
import java.util.Random;
|
2012-06-25 17:29:16 +02:00
|
|
|
|
|
|
|
public class FileUtils {
|
2012-10-18 16:49:14 +02:00
|
|
|
|
2012-06-25 17:29:16 +02:00
|
|
|
/**
|
2012-10-18 16:49:14 +02:00
|
|
|
* Checks, whether the child directory is a subdirectory of the base directory.
|
2012-06-25 17:29:16 +02:00
|
|
|
*
|
|
|
|
* @param base
|
|
|
|
* the base directory.
|
|
|
|
* @param child
|
|
|
|
* the suspected child directory.
|
|
|
|
* @return true, if the child is a subdirectory of the base directory.
|
|
|
|
*/
|
2012-07-03 17:26:15 +02:00
|
|
|
public static boolean isSubDirectory(File base, File child) {
|
|
|
|
try {
|
|
|
|
base = base.getCanonicalFile();
|
|
|
|
child = child.getCanonicalFile();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-25 17:29:16 +02:00
|
|
|
|
|
|
|
File parentFile = child;
|
|
|
|
while (parentFile != null) {
|
|
|
|
if (base.equals(parentFile)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
parentFile = parentFile.getParentFile();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-18 16:49:14 +02:00
|
|
|
|
|
|
|
public static void copy(File sourceFolder, File destFolder) throws IOException {
|
|
|
|
for (File file : sourceFolder.listFiles()) {
|
|
|
|
File destFile = new File(destFolder, file.getName());
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
if (!destFile.mkdir()) {
|
|
|
|
throw new IOException("Unable to create folder: " + destFile);
|
|
|
|
}
|
|
|
|
copy(file, destFile);
|
|
|
|
} else {
|
|
|
|
FileInputStream fis = null;
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(file);
|
|
|
|
fos = new FileOutputStream(destFile);
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int readBytes = -1;
|
|
|
|
while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
|
|
|
|
fos.write(buf, 0, readBytes);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (fis != null) {
|
|
|
|
fis.close();
|
|
|
|
}
|
|
|
|
if (fos != null) {
|
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void recursiveDelete(File file) {
|
2012-10-19 10:35:48 +02:00
|
|
|
if (file == null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-18 16:49:14 +02:00
|
|
|
if (file.isDirectory()) {
|
|
|
|
for (File current : file.listFiles()) {
|
|
|
|
if (current.isDirectory()) {
|
|
|
|
recursiveDelete(current);
|
|
|
|
} else {
|
|
|
|
current.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
2012-10-19 10:35:48 +02:00
|
|
|
public static File createTempFolder() throws IOException {
|
|
|
|
File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "arduino_" + new Random().nextInt(1000000));
|
|
|
|
if (!tmpFolder.mkdir()) {
|
|
|
|
throw new IOException("Unable to create temp folder " + tmpFolder);
|
|
|
|
}
|
|
|
|
return tmpFolder;
|
|
|
|
}
|
|
|
|
|
2013-01-16 14:04:30 +01:00
|
|
|
//
|
|
|
|
// Compute relative path to "target" from a directory "origin".
|
|
|
|
//
|
|
|
|
// If "origin" is not absolute, it is relative from the current directory.
|
|
|
|
// If "target" is not absolute, it is relative from "origin".
|
|
|
|
//
|
|
|
|
// by Shigeru KANEMOTO at SWITCHSCIENCE.
|
|
|
|
//
|
|
|
|
public static String relativePath(String origin, String target) {
|
|
|
|
try {
|
|
|
|
origin = (new File(origin)).getCanonicalPath();
|
|
|
|
File targetFile = new File(target);
|
|
|
|
if (targetFile.isAbsolute())
|
|
|
|
target = targetFile.getCanonicalPath();
|
|
|
|
else
|
|
|
|
target = (new File(origin, target)).getCanonicalPath();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (origin.equals(target)) {
|
|
|
|
// origin and target is identical.
|
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (origin.equals(File.separator)) {
|
|
|
|
// origin is root.
|
|
|
|
return "." + target;
|
|
|
|
}
|
|
|
|
|
|
|
|
String prefix = "";
|
|
|
|
String root = File.separator;
|
|
|
|
|
|
|
|
if (System.getProperty("os.name").indexOf("Windows") != -1) {
|
|
|
|
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
|
|
|
|
// Windows UNC path not supported.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
char originLetter = origin.charAt(0);
|
|
|
|
char targetLetter = target.charAt(0);
|
|
|
|
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
|
|
|
|
// Windows only
|
|
|
|
if (originLetter != targetLetter) {
|
|
|
|
// Drive letters differ
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = "" + originLetter + ':';
|
|
|
|
root = prefix + File.separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
String relative = "";
|
|
|
|
while (!target.startsWith(origin + File.separator)) {
|
|
|
|
origin = (new File(origin)).getParent();
|
|
|
|
if (origin.equals(root))
|
|
|
|
origin = prefix;
|
|
|
|
relative += "..";
|
|
|
|
relative += File.separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return relative + target.substring(origin.length() + 1);
|
|
|
|
}
|
2012-06-25 17:29:16 +02:00
|
|
|
}
|