|
25 | 25 | import java.io.*; |
26 | 26 | import java.nio.file.Files; |
27 | 27 | import java.util.Enumeration; |
28 | | -import java.util.Vector; |
29 | 28 | import java.util.zip.*; |
30 | 29 |
|
31 | 30 | import processing.core.PApplet; |
@@ -379,61 +378,55 @@ static public long calcFolderSize(File folder) { |
379 | 378 | } |
380 | 379 |
|
381 | 380 |
|
382 | | -// /** |
383 | | -// * Recursively creates a list of all files within the specified folder, |
384 | | -// * and returns a list of their relative paths. |
385 | | -// * Ignores any files/folders prefixed with a dot. |
386 | | -// */ |
387 | | -// static public String[] listFiles(String path, boolean relative) { |
388 | | -// return listFiles(new File(path), relative); |
389 | | -// } |
390 | | - |
391 | | - |
| 381 | + /** |
| 382 | + * Recursively creates a list of all files within the specified folder, |
| 383 | + * and returns a list of their relative paths. |
| 384 | + * Ignores any files/folders prefixed with a dot. |
| 385 | + * @param relative true return relative paths instead of absolute paths |
| 386 | + */ |
392 | 387 | static public String[] listFiles(File folder, boolean relative) { |
393 | | - String path = folder.getAbsolutePath(); |
394 | | - Vector<String> vector = new Vector<String>(); |
395 | | - listFiles(relative ? (path + File.separator) : "", path, null, vector); |
396 | | - String outgoing[] = new String[vector.size()]; |
397 | | - vector.copyInto(outgoing); |
398 | | - return outgoing; |
| 388 | + return listFiles(folder, relative, null); |
399 | 389 | } |
400 | 390 |
|
401 | 391 |
|
402 | | - static public String[] listFiles(File folder, boolean relative, String extension) { |
403 | | - String path = folder.getAbsolutePath(); |
404 | | - Vector<String> vector = new Vector<String>(); |
| 392 | + static public String[] listFiles(File folder, boolean relative, |
| 393 | + String extension) { |
405 | 394 | if (extension != null) { |
406 | 395 | if (!extension.startsWith(".")) { |
407 | 396 | extension = "." + extension; |
408 | 397 | } |
409 | 398 | } |
410 | | - listFiles(relative ? (path + File.separator) : "", path, extension, vector); |
411 | | - String outgoing[] = new String[vector.size()]; |
412 | | - vector.copyInto(outgoing); |
413 | | - return outgoing; |
| 399 | + |
| 400 | + StringList list = new StringList(); |
| 401 | + listFilesImpl(folder, relative, extension, list); |
| 402 | + |
| 403 | + if (relative) { |
| 404 | + String[] outgoing = new String[list.size()]; |
| 405 | + // remove the slash (or backslash) as well |
| 406 | + int prefixLength = folder.getAbsolutePath().length() + 1; |
| 407 | + for (int i = 0; i < outgoing.length; i++) { |
| 408 | + outgoing[i] = list.get(i).substring(prefixLength); |
| 409 | + } |
| 410 | + return outgoing; |
| 411 | + } |
| 412 | + return list.array(); |
414 | 413 | } |
415 | 414 |
|
416 | 415 |
|
417 | | - static protected void listFiles(String basePath, |
418 | | - String path, String extension, |
419 | | - Vector<String> vector) { |
420 | | - File folder = new File(path); |
421 | | - String[] list = folder.list(); |
422 | | - if (list != null) { |
423 | | - for (String item : list) { |
424 | | - if (item.charAt(0) == '.') continue; |
425 | | - if (extension == null || item.toLowerCase().endsWith(extension)) { |
426 | | - File file = new File(path, item); |
427 | | - String newPath = file.getAbsolutePath(); |
428 | | - if (newPath.startsWith(basePath)) { |
429 | | - newPath = newPath.substring(basePath.length()); |
430 | | - } |
431 | | - // only add if no ext or match |
432 | | - if (extension == null || item.toLowerCase().endsWith(extension)) { |
433 | | - vector.add(newPath); |
434 | | - } |
435 | | - if (file.isDirectory()) { // use absolute path |
436 | | - listFiles(basePath, file.getAbsolutePath(), extension, vector); |
| 416 | + static void listFilesImpl(File folder, boolean relative, |
| 417 | + String extension, StringList list) { |
| 418 | + File[] items = folder.listFiles(); |
| 419 | + if (items != null) { |
| 420 | + for (File item : items) { |
| 421 | + String name = item.getName(); |
| 422 | + if (name.charAt(0) != '.') { |
| 423 | + if (item.isDirectory()) { |
| 424 | + listFilesImpl(item, relative, extension, list); |
| 425 | + |
| 426 | + } else { // a file |
| 427 | + if (extension == null || name.endsWith(extension)) { |
| 428 | + list.append(item.getAbsolutePath()); |
| 429 | + } |
437 | 430 | } |
438 | 431 | } |
439 | 432 | } |
|
0 commit comments