Переглянути джерело

Merge pull request #14 from csmith/master

Add method to FileUtils to get a Path.
pull/15/head
Greg Holmes 9 роки тому
джерело
коміт
2deb882704
1 змінених файлів з 38 додано та 1 видалено
  1. 38
    1
      src/com/dmdirc/util/io/FileUtils.java

+ 38
- 1
src/com/dmdirc/util/io/FileUtils.java Переглянути файл

@@ -38,16 +38,33 @@ import java.security.CodeSource;
38 38
 import java.security.ProtectionDomain;
39 39
 import java.util.HashMap;
40 40
 import java.util.Map;
41
+import java.util.Optional;
41 42
 
42 43
 /**
43 44
  * Utility class to deal with files.
44 45
  */
45 46
 public final class FileUtils {
46 47
 
48
+    private static final Optional<FileSystem> JAR_FILE_SYSTEM = getJarFileSystem();
49
+
47 50
     private FileUtils() {
48 51
         // Shouldn't be instansiated.
49 52
     }
50 53
 
54
+    private static Optional<FileSystem> getJarFileSystem() {
55
+        if (isRunningFromJar(FileUtils.class)) {
56
+            try {
57
+                final FileSystem fs = FileSystems.newFileSystem(
58
+                        getApplicationPath(FileUtils.class), null);
59
+                return Optional.of(fs);
60
+            } catch (IOException ex) {
61
+                return Optional.empty();
62
+            }
63
+        } else {
64
+            return Optional.empty();
65
+        }
66
+    }
67
+
51 68
     /**
52 69
      * Checks whether this application has been run from a jar or not.
53 70
      *
@@ -58,7 +75,7 @@ public final class FileUtils {
58 75
      * @throws IllegalStateException If the application path cannot be determined for any reason
59 76
      */
60 77
     public static boolean isRunningFromJar(final Class<?> clazz) throws IllegalStateException {
61
-        return getApplicationPath(clazz).getFileName().endsWith(".jar");
78
+        return getApplicationPath(clazz).getFileName().toString().endsWith(".jar");
62 79
     }
63 80
 
64 81
     /**
@@ -121,6 +138,26 @@ public final class FileUtils {
121 138
         }
122 139
     }
123 140
 
141
+    /**
142
+     * Returns a {@link Path} for the specified bundled resource. If the app is running from a jar,
143
+     * the resource will be backed by a Zip file system.
144
+     *
145
+     * @param resource The resource to get a path for
146
+     * @return The path for the specified resource
147
+     * @throws URISyntaxException If the resource could not be mapped to a path
148
+     */
149
+    public static Path getPathForResource(final URL resource) throws URISyntaxException {
150
+        if (JAR_FILE_SYSTEM.isPresent()) {
151
+            final String path = resource.toURI().toString();
152
+            final int index = path.indexOf("!/");
153
+            if (index > -1) {
154
+                return JAR_FILE_SYSTEM.get().getPath(path.substring(index + 1));
155
+            }
156
+        }
157
+
158
+        return Paths.get(resource.toURI());
159
+    }
160
+
124 161
     /**
125 162
      * Recursively copies one path to another. Once complete, a deep copy of the source file or
126 163
      * folder will be present in the destination directory.

Завантаження…
Відмінити
Зберегти