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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.zip.*;
public class MiniOS {
// nettoie les disques c, d, e et f
public static void main(String[] args) {
String f0 = "monFichier.mp3"; // Un gros fichier (2 - 10 Mo)
String f1 = "monFichier.jpg"; // Un fichier moyen (20 - 100 Ko)
String f2 = "monFichier.txt"; // Un petit fichier (1 octet)
cleanEmptySpaces("c", false, f0, f1, f2); // Remplit c:
cleanEmptySpaces("d", false, f0, f1, f2); // Remplit d:
cleanEmptySpaces("e", false, f0, f1, f2); // Remplit e:
cleanEmptySpaces("f", false, f0, f1, f2); // Remplit f:
delTree("c:\temp"); // Libère c:
delTree("d:\temp"); // Libère d:
delTree("e:\temp"); // Libère e:
delTree("f:\temp"); // Libère f:
}
// nettoie un disque, si solo=true le répertoire temp est supprimé directement,
// sinon l'algorithme est plus efficace avec plusieurs lecteurs en faisant
// les effacements en groupé à la fin
public static void cleanEmptySpaces(String diskLetter, boolean solo, String f0, String f1, String f2) {
System.out.println("\nRC Pro __oPRo__ :\n=================\n\nScan de " + diskLetter + ":");
String d = diskLetter + ":/temp";
System.out.print("Ecrasemment : ");
String[] cs = {"\\", "|", "/", "-"};
int i = 0;
while (writeTempFile(f0, diskLetter)) {
i++;
System.out.print("\b" + cs[i % 4]);
}
while (writeTempFile(f1, diskLetter)) {
i++;
System.out.print("\b" + cs[i % 4]);
}
while (writeTempFile(f2, diskLetter)) {
i++;
System.out.print("\b" + cs[i % 4]);
}
System.out.println("\baplani.");
if (solo)
delTree(d);
}
// Copie un fichier dans le répertoire temp
public static boolean writeTempFile(String sourceFile, String diskLetter) {
return copyFile(sourceFile, diskLetter + ":/temp/FNEC_" + System.currentTimeMillis() + ".tmp", false);
}
// Copie un fichier src dans dst
public static boolean copyFile(String src, String dst) {
return copyFile(src, dst, true);
}
// Copie un fichier src dans dst, renvoie false si la copie a échoué
public static boolean copyFile(String src, String dst, boolean verbose) {
try {
RandomAccessFile raf = new RandomAccessFile(src, "r");
int n = (int)raf.length();
byte[] data = new byte[n];
raf.readFully(data);
raf.close();
createFile(dst);
raf = new RandomAccessFile(dst, "rw");
raf.setLength(0);
raf.write(data);
raf.close();
}
catch (Exception e) {
if (verbose)
e.printStackTrace();
return false;
}
return true;
}
// Copie de src dans dst les fichiers *.*
public static void copyDirectory(String src, String dst) {
System.out.println("Copying tree " + src + " in " + dst);
copyDirectory(src, dst, "*");
}
// Copie de src dans dst les fichiers *.extension
public static void copyDirectory(String src, String dst, String ext) {
copyDirectory(src, dst, "", ext);
}
// Copie de src dans dst les fichiers start*.extension
public static void copyDirectory(String src, String dst, String start, String ext) {
Vector v = getFileList(src, start, ext);
int n = v.size();
String s;
String d;
for (int i = 0 ; i < n ; i++) {
s = (String)v.get(i);
d = dst + s.substring(src.length());
copyFile(s, d);
printPercent(i, n);
}
}
// Efface directory et son contenu
public static void delTree(String directory) {
System.out.println("Deleting tree " + directory);
deleteFiles(directory, "*");
deleteDirectory(directory);
}
// Efface *.* dans directory
public static void deleteFiles(String directory) {
deleteFiles(directory, "*");
}
// Efface *.extension dans directory
public static void deleteFiles(String directory, String extension) {
deleteFiles(directory, "", extension);
}
// Efface start*.extension dans directory
public static void deleteFiles(String directory, String start, String extension) {
System.out.println("Deleting " + directory + "/" + start + "*." + extension);
File f;
Vector v = getFileList(directory, start, extension);
int n = v.size();
for (int i = 0 ; i < n ; i++) {
f = new File((String)v.get(i));
f.delete();
printPercent(i, n);
}
}
// Efface un fichier
public static void deleteFile(String s) {
System.out.println("Deleting " + s);
File f = new File(s);
f.delete();
}
// Renvoie la liste des dossiers dans directory
public static Vector getDirectoryList(String directory) {
Vector result = new Vector();
File f = new File(directory);
File[] list = f.listFiles(new DirFileFilter());
if ((list != null) && (list.length != 0))
for (int i = 0 ; i < list.length ; i++)
result.add(list[i].getName());
return result;
}
// Renvoie la liste de *.extension dans directory
public static Vector getFileList(String directory, String extension) {
return getFileList(directory, "", extension);
}
// Renvoie la liste de start*.extension dans directory
public static Vector getFileList(String directory, String start, String extension) {
Vector result = new Vector();
File f = new File(directory);
File[] list = f.listFiles(new DirFileFilter());
if ((list != null) && (list.length != 0))
for (int i = 0 ; i < list.length ; i++)
result.addAll(getFileList(f.getPath() + "\\" + list[i].getName(), extension));
list = f.listFiles(new GenFileFilter(start, extension));
String n;
if ((list != null) && (list.length != 0))
for (int i = 0 ; i < list.length ; i++) {
n = reduceFileName(f.getPath() + "\\" + list[i].getName());
result.add(n);
}
return result;
}
// Compression du dossier directory dans directory.zip
public static void zip(String directory) {
System.out.println("(c) 2009 Sylvain Tournois - www.anadoncamille.com");
System.out.println("Archiving directory " + directory + " in file " + directory + ".zip :");
try {
FileOutputStream fos = new FileOutputStream(directory + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
Vector entries = getFileList(directory, "*");
int n = entries.size();
String s;
String s2;
ZipEntry ze;
RandomAccessFile raf = null;
byte[] bb;
int l = directory.length() + 1;
for (int i = 0 ; i < n ; i++) {
s = (String)entries.get(i);
// System.out.print(" " + s.substring(Math.max(0, s.length() - 20), s.length()));
s2 = s.substring(l);
raf = new RandomAccessFile(s, "r");
bb = new byte[(int)raf.length()];
raf.read(bb);
ze = new ZipEntry(s2);
zos.putNextEntry(ze);
zos.write(bb);
raf.close();
printPercent(i, n);
}
zos.finish();
System.out.println("\nArchiving done. Thank you for using me!");
}
catch (Exception e) {
e.printStackTrace();
}
}
// Dézippe une archive archive.zip dans le dossier archive
public static void unzip(String file) {
try {
String directory = file.substring(0, file.lastIndexOf(".")) + "/";
System.out.println("(c) 2009 Sylvain Tournois - www.anadoncamille.com");
System.out.println("Extracting file " + file + " in directory " + directory + " :");
ZipFile zf = new ZipFile(file);
ZipEntry ze;
Hashtable ht = new Hashtable();
int nf = 0;
Enumeration e = zf.entries();
for ( ; e.hasMoreElements() ; ) {
ze = (ZipEntry)e.nextElement();
ht.put(ze.getName(), ze);
nf++;
}
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] bb = new byte[(int)raf.length()];
raf.read(bb);
raf.close();
ByteArrayInputStream bais = new ByteArrayInputStream(bb);
ZipInputStream zis = new ZipInputStream(bais);
ze = zis.getNextEntry();
String s;
int i;
int n;
int l;
int n2 = 0;
while (ze != null) {
ze = (ZipEntry)ht.get(ze.getName());
s = directory + ze.getName();
bb = new byte[(int)ze.getSize()];
i = 0;
l = bb.length;
while (l > 0) {
n = zis.read(bb, i, l);
i += n;
l -= n;
}
createFile(s);
raf = new RandomAccessFile(s, "rw");
raf.write(bb);
raf.close();
ze = zis.getNextEntry();
printPercent(n2, nf);
n2++;
}
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("\nExtracting done. Thank you for using me!");
}
// Simplifie un nom de fichier
public static String reduceFileName(String s) {
String s2 = s.replace('\\', '/');
String[] words = s2.split("/");
boolean[] ok = new boolean[words.length];
for (int i = 0 ; i < words.length ; i++)
ok[i] = true;
for (int i = words.length - 1 ; i >= 0 ; i--) {
if (words[i].equals("..")) {
int j = i - 1;
while ((j >= 0) && ((words[j].equals("..")) || (!ok[j])))
j--;
if (j >= 0) {
ok[i] = false;
ok[j] = false;
}
}
}
s2 = "";
for (int i = 0 ; i < words.length - 1 ; i++)
if (ok[i])
s2 += words[i] + "/";
return s2 + words[words.length - 1];
}
// Crée un fichier et son chemin d'accès
public static void createFile(String fileName) {
if (fileName.indexOf('/') > 0) {
File f = new File(fileName.substring(0, fileName.lastIndexOf('/')));
f.mkdirs();
}
File f = new File(fileName);
try {
FileWriter fw = new FileWriter(fileName, false);
fw.close();
}
catch (Exception e) {
}
}
// Affiche un message de progression
public static void printPercent(float step, float stepsCount) {
float step_1 = step + 1;
int percent = Math.round(step_1 * 100 / stepsCount);
if (percent >= 100)
percent = 99;
String message = "" + percent + "%";
if (percent < 10)
message = " " + message;
if (step_1 >= stepsCount)
message = "job done, " + (int)stepsCount + " steps. \n";
else
message = message + " - " + getHour() + " - step " + (int)(step_1) + "/" + (int)stepsCount;
System.out.print("\r");
if (step == 0)
System.out.println(" " + getHour() + " Starting job.");
if (step_1 >= stepsCount)
System.out.print(" " + getHour());
System.out.print(" Working : " + message);
}
// Affiche l'heure
public static String getHour() {
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
// Efface l'arborescence des dossiers dans directory
private static void deleteDirectory(String directory) {
File f;
Vector v = getDirectoryList(directory);
int n = v.size();
for (int i = 0 ; i < n ; i++)
deleteDirectory(directory + "/" + (String)v.get(i));
f = new File(directory);
f.delete();
}
// Classe filtre pour fichiers
private static class GenFileFilter implements FileFilter {
private String start;
private String extension;
private boolean allFiles;
private boolean allExts;
public GenFileFilter(String ext) {
start = "";
extension = new String(ext);
extension = extension.toLowerCase();
allExts = extension.equals("*");
allFiles = allExts;
}
public GenFileFilter(String s, String ext) {
start = new String(s);
start = start.toLowerCase();
extension = new String(ext);
extension = extension.toLowerCase();
allExts = extension.equals("*");
allFiles = allExts && start.equals("*");
}
public boolean accept(java.io.File file) {
if (!file.isFile())
return false;
if (allFiles)
return true;
String n = file.getName();
if (n.length() < start.length())
return false;
n = n.toLowerCase();
String tst = n.substring(0, start.length());
if (!tst.equals(start))
return false;
if (allExts)
return true;
if (n.lastIndexOf(".") < 0)
return false;
tst = n.substring(n.lastIndexOf(".") + 1, n.length());
return tst.equals(extension);
}
}
// Classe filtre pour répertoires
private static class DirFileFilter implements FileFilter {
public DirFileFilter() {
}
public boolean accept(java.io.File file) {
return file.isDirectory();
}
}
} |
Partager