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); 70 return Paths.get(new URI(spec)).toAbsolutePath(); 71 } catch (URISyntaxException e) { 72 throw new IllegalArgumentException(e.getMessage(), e); 73 } 74 } 75 76 private boolean ensureFile(Path path) { 77 try { 78 BasicFileAttributes attrs = 79 Files.readAttributes(path, BasicFileAttributes.class); 80 if (!attrs.isRegularFile()) 81 throw new UnsupportedOperationException(); 82 return true; 83 } catch (IOException ioe) { 84 return false; 85 } 86 } 87 88 @Override 89 public FileSystem newFileSystem(URI uri, Map<String, ?> env) 90 throws IOException 91 { 92 Path path = uriToPath(uri); 93 synchronized(filesystems) { 94 Path realPath = null; 95 if (ensureFile(path)) { 96 realPath = path.toRealPath(); 97 if (filesystems.containsKey(realPath)) 98 throw new FileSystemAlreadyExistsException(); 99 } 100 ZipFileSystem zipfs = null; 101 try { 102 zipfs = new ZipFileSystem(this, path, env); 103 } catch (ZipException ze) { 104 String pname = path.toString(); 105 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 106 throw ze; 107 // assume NOT a zip/jar file 108 throw new UnsupportedOperationException(); 109 } 110 filesystems.put(realPath, zipfs); 111 return zipfs; 112 } 113 } 114 115 @Override 116 public FileSystem newFileSystem(Path path, Map<String, ?> env) 117 throws IOException 118 { 119 if (path.getFileSystem() != FileSystems.getDefault()) { 120 throw new UnsupportedOperationException(); 121 } 122 ensureFile(path); 123 try { 124 return new ZipFileSystem(this, path, env); 125 } catch (ZipException ze) { 126 String pname = path.toString(); 127 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 128 throw ze; 129 throw new UnsupportedOperationException(); 130 } 131 } 132 133 @Override 134 public Path getPath(URI uri) { 135 136 String spec = uri.getSchemeSpecificPart(); 137 int sep = spec.indexOf("!/"); 138 if (sep == -1) 139 throw new IllegalArgumentException("URI: " 140 + uri 141 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR"); 142 return getFileSystem(uri).getPath(spec.substring(sep + 1)); 143 } 144 145 146 @Override 147 public FileSystem getFileSystem(URI uri) { 148 synchronized (filesystems) { 149 ZipFileSystem zipfs = null; 150 try { 151 zipfs = filesystems.get(uriToPath(uri).toRealPath()); 152 } catch (IOException x) { 153 // ignore the ioe from toRealPath(), return FSNFE 154 } 155 if (zipfs == null) | 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); 70 } 71 return Paths.get(new URI(spec)).toAbsolutePath(); 72 } catch (URISyntaxException e) { 73 throw new IllegalArgumentException(e.getMessage(), e); 74 } 75 } 76 77 private boolean ensureFile(Path path) { 78 try { 79 BasicFileAttributes attrs = 80 Files.readAttributes(path, BasicFileAttributes.class); 81 if (!attrs.isRegularFile()) 82 throw new UnsupportedOperationException(); 83 return true; 84 } catch (IOException ioe) { 85 return false; 86 } 87 } 88 89 @Override 90 public FileSystem newFileSystem(URI uri, Map<String, ?> env) 91 throws IOException 92 { 93 Path path = uriToPath(uri); 94 synchronized(filesystems) { 95 Path realPath = null; 96 if (ensureFile(path)) { 97 realPath = path.toRealPath(); 98 if (filesystems.containsKey(realPath)) 99 throw new FileSystemAlreadyExistsException(); 100 } 101 ZipFileSystem zipfs = null; 102 try { 103 zipfs = new ZipFileSystem(this, path, env); 104 } catch (ZipException ze) { 105 String pname = path.toString(); 106 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 107 throw ze; 108 // assume NOT a zip/jar file 109 throw new UnsupportedOperationException(); 110 } 111 if (realPath == null) { // newly created 112 realPath = path.toRealPath(); 113 } 114 filesystems.put(realPath, zipfs); 115 return zipfs; 116 } 117 } 118 119 @Override 120 public FileSystem newFileSystem(Path path, Map<String, ?> env) 121 throws IOException 122 { 123 if (path.getFileSystem() != FileSystems.getDefault()) { 124 throw new UnsupportedOperationException(); 125 } 126 ensureFile(path); 127 try { 128 return new ZipFileSystem(this, path, env); 129 } catch (ZipException ze) { 130 String pname = path.toString(); 131 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 132 throw ze; 133 throw new UnsupportedOperationException(); 134 } 135 } 136 137 @Override 138 public Path getPath(URI uri) { 139 String spec = uri.getSchemeSpecificPart(); 140 int sep = spec.indexOf("!/"); 141 if (sep == -1) 142 throw new IllegalArgumentException("URI: " 143 + uri 144 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR"); 145 return getFileSystem(uri).getPath(spec.substring(sep + 1)); 146 } 147 148 149 @Override 150 public FileSystem getFileSystem(URI uri) { 151 synchronized (filesystems) { 152 ZipFileSystem zipfs = null; 153 try { 154 zipfs = filesystems.get(uriToPath(uri).toRealPath()); 155 } catch (IOException x) { 156 // ignore the ioe from toRealPath(), return FSNFE 157 } 158 if (zipfs == null) |