8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.*;
29 import java.nio.channels.*;
30 import java.nio.file.*;
31 import java.nio.file.DirectoryStream.Filter;
32 import java.nio.file.attribute.*;
33 import java.nio.file.spi.FileSystemProvider;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.zip.ZipException;
40 import java.util.concurrent.ExecutorService;
41
42 /*
43 *
44 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
45 */
46
47 public class ZipFileSystemProvider extends FileSystemProvider {
48
49
50 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
51
52 public ZipFileSystemProvider() {}
53
54 @Override
55 public String getScheme() {
56 return "jar";
57 }
58
59 protected Path uriToPath(URI uri) {
60 String scheme = uri.getScheme();
61 if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
62 throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
63 }
64 try {
65 // only support legacy JAR URL syntax jar:{uri}!/{entry} for now
66 String spec = uri.getRawSchemeSpecificPart();
67 int sep = spec.indexOf("!/");
68 if (sep != -1) {
69 spec = spec.substring(0, sep);
185 @Override
186 public void copy(Path src, Path target, CopyOption... options)
187 throws IOException
188 {
189 toZipPath(src).copy(toZipPath(target), options);
190 }
191
192 @Override
193 public void createDirectory(Path path, FileAttribute<?>... attrs)
194 throws IOException
195 {
196 toZipPath(path).createDirectory(attrs);
197 }
198
199 @Override
200 public final void delete(Path path) throws IOException {
201 toZipPath(path).delete();
202 }
203
204 @Override
205 @SuppressWarnings("unchecked")
206 public <V extends FileAttributeView> V
207 getFileAttributeView(Path path, Class<V> type, LinkOption... options)
208 {
209 return ZipFileAttributeView.get(toZipPath(path), type);
210 }
211
212 @Override
213 public FileStore getFileStore(Path path) throws IOException {
214 return toZipPath(path).getFileStore();
215 }
216
217 @Override
218 public boolean isHidden(Path path) {
219 return toZipPath(path).isHidden();
220 }
221
222 @Override
223 public boolean isSameFile(Path path, Path other) throws IOException {
224 return toZipPath(path).isSameFile(other);
225 }
269 @Override
270 public InputStream newInputStream(Path path, OpenOption... options)
271 throws IOException
272 {
273 return toZipPath(path).newInputStream(options);
274 }
275
276 @Override
277 public OutputStream newOutputStream(Path path, OpenOption... options)
278 throws IOException
279 {
280 return toZipPath(path).newOutputStream(options);
281 }
282
283 @Override
284 @SuppressWarnings("unchecked") // Cast to A
285 public <A extends BasicFileAttributes> A
286 readAttributes(Path path, Class<A> type, LinkOption... options)
287 throws IOException
288 {
289 if (type == BasicFileAttributes.class || type == ZipFileAttributes.class)
290 return (A)toZipPath(path).getAttributes();
291 return null;
292 }
293
294 @Override
295 public Map<String, Object>
296 readAttributes(Path path, String attribute, LinkOption... options)
297 throws IOException
298 {
299 return toZipPath(path).readAttributes(attribute, options);
300 }
301
302 @Override
303 public Path readSymbolicLink(Path link) throws IOException {
304 throw new UnsupportedOperationException("Not supported.");
305 }
306
307 @Override
308 public void setAttribute(Path path, String attribute,
309 Object value, LinkOption... options)
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.nio.channels.AsynchronousFileChannel;
34 import java.nio.channels.FileChannel;
35 import java.nio.channels.SeekableByteChannel;
36 import java.nio.file.AccessMode;
37 import java.nio.file.CopyOption;
38 import java.nio.file.DirectoryStream;
39 import java.nio.file.DirectoryStream.Filter;
40 import java.nio.file.FileStore;
41 import java.nio.file.FileSystem;
42 import java.nio.file.FileSystemAlreadyExistsException;
43 import java.nio.file.FileSystemNotFoundException;
44 import java.nio.file.Files;
45 import java.nio.file.LinkOption;
46 import java.nio.file.OpenOption;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.nio.file.ProviderMismatchException;
50 import java.nio.file.attribute.BasicFileAttributes;
51 import java.nio.file.attribute.FileAttribute;
52 import java.nio.file.attribute.FileAttributeView;
53 import java.nio.file.attribute.PosixFileAttributes;
54 import java.nio.file.spi.FileSystemProvider;
55 import java.util.HashMap;
56 import java.util.Map;
57 import java.util.Set;
58 import java.util.concurrent.ExecutorService;
59 import java.util.zip.ZipException;
60
61 /**
62 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
63 */
64 public class ZipFileSystemProvider extends FileSystemProvider {
65
66 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
67
68 public ZipFileSystemProvider() {}
69
70 @Override
71 public String getScheme() {
72 return "jar";
73 }
74
75 protected Path uriToPath(URI uri) {
76 String scheme = uri.getScheme();
77 if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
78 throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
79 }
80 try {
81 // only support legacy JAR URL syntax jar:{uri}!/{entry} for now
82 String spec = uri.getRawSchemeSpecificPart();
83 int sep = spec.indexOf("!/");
84 if (sep != -1) {
85 spec = spec.substring(0, sep);
201 @Override
202 public void copy(Path src, Path target, CopyOption... options)
203 throws IOException
204 {
205 toZipPath(src).copy(toZipPath(target), options);
206 }
207
208 @Override
209 public void createDirectory(Path path, FileAttribute<?>... attrs)
210 throws IOException
211 {
212 toZipPath(path).createDirectory(attrs);
213 }
214
215 @Override
216 public final void delete(Path path) throws IOException {
217 toZipPath(path).delete();
218 }
219
220 @Override
221 public <V extends FileAttributeView> V
222 getFileAttributeView(Path path, Class<V> type, LinkOption... options)
223 {
224 return ZipFileAttributeView.get(toZipPath(path), type);
225 }
226
227 @Override
228 public FileStore getFileStore(Path path) throws IOException {
229 return toZipPath(path).getFileStore();
230 }
231
232 @Override
233 public boolean isHidden(Path path) {
234 return toZipPath(path).isHidden();
235 }
236
237 @Override
238 public boolean isSameFile(Path path, Path other) throws IOException {
239 return toZipPath(path).isSameFile(other);
240 }
284 @Override
285 public InputStream newInputStream(Path path, OpenOption... options)
286 throws IOException
287 {
288 return toZipPath(path).newInputStream(options);
289 }
290
291 @Override
292 public OutputStream newOutputStream(Path path, OpenOption... options)
293 throws IOException
294 {
295 return toZipPath(path).newOutputStream(options);
296 }
297
298 @Override
299 @SuppressWarnings("unchecked") // Cast to A
300 public <A extends BasicFileAttributes> A
301 readAttributes(Path path, Class<A> type, LinkOption... options)
302 throws IOException
303 {
304 if (type == BasicFileAttributes.class ||
305 type == PosixFileAttributes.class ||
306 type == ZipFileAttributes.class)
307 return (A)toZipPath(path).getAttributes();
308 return null;
309 }
310
311 @Override
312 public Map<String, Object>
313 readAttributes(Path path, String attribute, LinkOption... options)
314 throws IOException
315 {
316 return toZipPath(path).readAttributes(attribute, options);
317 }
318
319 @Override
320 public Path readSymbolicLink(Path link) throws IOException {
321 throw new UnsupportedOperationException("Not supported.");
322 }
323
324 @Override
325 public void setAttribute(Path path, String attribute,
326 Object value, LinkOption... options)
|