1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.ysance.acceleo.module.evtforms.services;
import java.io.*;
public class FileUtils {
static final int BUFFER = 2048;
static String DOSSIER_SOURCE;
static String DOSSIER_DEST;
/**
* Copy a file from a source to a destination
* @param file
* @param from
* @param data
*/
public static void copyFile(String file, String from, byte data[]) {
try {
FileInputStream fi = new FileInputStream(DOSSIER_SOURCE + "/"
+ from + "/" + file);
FileOutputStream out = new FileOutputStream(DOSSIER_DEST + "/"
+ from + "/" + file);
BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.close();
fi.close();
origin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Copy a directory from a source to a destination
* @param in
* @param from
* @param dest
*/
public static void copyDirectory(String in, String from, String dest) {
File f = new File(in);
File fDest = new File(dest);
String files[] = f.list();
byte data[] = new byte[BUFFER];
for (String file : files) {
if (isFile(DOSSIER_SOURCE + "/"
+ (from.isEmpty() ? "" : from + "/") + file)) {
copyFile(file, from, data);
} else {
fDest.mkdir();
copyDirectory(DOSSIER_SOURCE + "/"
+ (from.isEmpty() ? "" : from + "/") + file,
from.isEmpty() ? file : from + "/" + file, DOSSIER_DEST
+ "/" + (from.isEmpty() ? "" : from + "/")
+ file);
}
}
}
public static boolean isFile(String in) {
return (new File(in)).isFile();
}
public static Boolean copyFile(String dossierSource, String dossierDest) {
try {
DOSSIER_SOURCE = dossierSource;
DOSSIER_DEST = dossierDest;
copyDirectory(DOSSIER_SOURCE, "", DOSSIER_DEST);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
} |
Partager