Browse Source

Hack FileUtils to provide copycontents methods.

pull/20/head
Greg Holmes 9 years ago
parent
commit
f84c8a1b16
1 changed files with 45 additions and 3 deletions
  1. 45
    3
      src/com/dmdirc/util/io/FileUtils.java

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

110
         return Paths.get(uri);
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
      * Recursively copies the resources specified by the given URL to the destination folder.
127
      * Recursively copies the resources specified by the given URL to the destination folder.
115
      * Existing files will be replaced.
128
      * Existing files will be replaced.
119
      * @throws IOException If the resource couldn't be copied.
132
      * @throws IOException If the resource couldn't be copied.
120
      */
133
      */
121
     public static void copyResources(final URL source, final Path destination) throws IOException {
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
         try {
140
         try {
123
             final String path = source.toURI().toString();
141
             final String path = source.toURI().toString();
124
             final int index = path.indexOf("!/");
142
             final int index = path.indexOf("!/");
126
                 final String file = path.substring(0, index);
144
                 final String file = path.substring(0, index);
127
                 final Map<String, String> env = new HashMap<>();
145
                 final Map<String, String> env = new HashMap<>();
128
                 try (final FileSystem fs = FileSystems.newFileSystem(URI.create(file), env)) {
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
                             StandardCopyOption.REPLACE_EXISTING);
148
                             StandardCopyOption.REPLACE_EXISTING);
131
                 }
149
                 }
132
             } else {
150
             } else {
133
-                copyRecursively(Paths.get(source.toURI()), destination,
151
+                doCopyRecursively(Paths.get(source.toURI()), destination, contents,
134
                         StandardCopyOption.REPLACE_EXISTING);
152
                         StandardCopyOption.REPLACE_EXISTING);
135
             }
153
             }
136
         } catch (URISyntaxException ex) {
154
         } catch (URISyntaxException ex) {
158
         return Paths.get(resource.toURI());
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
      * Recursively copies one path to another. Once complete, a deep copy of the source file or
194
      * Recursively copies one path to another. Once complete, a deep copy of the source file or
163
      * folder will be present in the destination directory.
195
      * folder will be present in the destination directory.
169
      */
201
      */
170
     public static void copyRecursively(final Path source, final Path destination,
202
     public static void copyRecursively(final Path source, final Path destination,
171
             final CopyOption... options) throws IOException {
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
         if (Files.isDirectory(source)) {
216
         if (Files.isDirectory(source)) {
175
             Files.createDirectories(destinationPath);
217
             Files.createDirectories(destinationPath);

Loading…
Cancel
Save