Sfoglia il codice sorgente

Hack FileUtils to provide copycontents methods.

pull/20/head
Greg Holmes 9 anni fa
parent
commit
f84c8a1b16
1 ha cambiato i file con 45 aggiunte e 3 eliminazioni
  1. 45
    3
      src/com/dmdirc/util/io/FileUtils.java

+ 45
- 3
src/com/dmdirc/util/io/FileUtils.java Vedi File

@@ -110,6 +110,19 @@ public final class FileUtils {
110 110
         return Paths.get(uri);
111 111
     }
112 112
 
113
+    /**
114
+     * Recursively copies the resources specified by the given URL to the destination folder.
115
+     * Existing files will be replaced.
116
+     *
117
+     * @param source The resource to be copied (file or folder).
118
+     * @param destination The destination folder to copy the resource to.
119
+     * @throws IOException If the resource couldn't be copied.
120
+     */
121
+    public static void copyResourcesContents(final URL source, final Path destination)
122
+            throws IOException {
123
+        doCopyResources(source, destination, true);
124
+    }
125
+
113 126
     /**
114 127
      * Recursively copies the resources specified by the given URL to the destination folder.
115 128
      * Existing files will be replaced.
@@ -119,6 +132,11 @@ public final class FileUtils {
119 132
      * @throws IOException If the resource couldn't be copied.
120 133
      */
121 134
     public static void copyResources(final URL source, final Path destination) throws IOException {
135
+        doCopyResources(source, destination, false);
136
+    }
137
+
138
+    private static void doCopyResources(final URL source, final Path destination,
139
+            final boolean contents) throws IOException {
122 140
         try {
123 141
             final String path = source.toURI().toString();
124 142
             final int index = path.indexOf("!/");
@@ -126,11 +144,11 @@ public final class FileUtils {
126 144
                 final String file = path.substring(0, index);
127 145
                 final Map<String, String> env = new HashMap<>();
128 146
                 try (final FileSystem fs = FileSystems.newFileSystem(URI.create(file), env)) {
129
-                    copyRecursively(fs.getPath(path.substring(index + 2)), destination,
147
+                    doCopyRecursively(fs.getPath(path.substring(index + 2)), destination, contents,
130 148
                             StandardCopyOption.REPLACE_EXISTING);
131 149
                 }
132 150
             } else {
133
-                copyRecursively(Paths.get(source.toURI()), destination,
151
+                doCopyRecursively(Paths.get(source.toURI()), destination, contents,
134 152
                         StandardCopyOption.REPLACE_EXISTING);
135 153
             }
136 154
         } catch (URISyntaxException ex) {
@@ -158,6 +176,20 @@ public final class FileUtils {
158 176
         return Paths.get(resource.toURI());
159 177
     }
160 178
 
179
+    /**
180
+     * Recursively copies the contents of one path to another. Once complete, a deep copy of the
181
+     * source file or folder will be present in the destination directory.
182
+     *
183
+     * @param source The path that should be copied.
184
+     * @param destination The directory to place copied files in.
185
+     * @param options Options to use when copying.
186
+     * @throws IOException If any files couldn't be copied.
187
+     */
188
+    public static void copyRecursivelyContents(final Path source, final Path destination,
189
+            final CopyOption... options) throws IOException {
190
+        doCopyRecursively(source, destination, true, options);
191
+    }
192
+
161 193
     /**
162 194
      * Recursively copies one path to another. Once complete, a deep copy of the source file or
163 195
      * folder will be present in the destination directory.
@@ -169,7 +201,17 @@ public final class FileUtils {
169 201
      */
170 202
     public static void copyRecursively(final Path source, final Path destination,
171 203
             final CopyOption... options) throws IOException {
172
-        final Path destinationPath = destination.resolve(source.getFileName().toString());
204
+        doCopyRecursively(source, destination, false, options);
205
+    }
206
+
207
+    private static void doCopyRecursively(final Path source, final Path destination,
208
+            final boolean contents, final CopyOption... options) throws IOException {
209
+        final Path destinationPath;
210
+        if (contents) {
211
+            destinationPath = destination;
212
+        } else {
213
+            destinationPath = destination.resolve(source.getFileName().toString());
214
+        }
173 215
 
174 216
         if (Files.isDirectory(source)) {
175 217
             Files.createDirectories(destinationPath);

Loading…
Annulla
Salva