36 import java.nio.file.*; 37 import java.nio.file.DirectoryStream.Filter; 38 import java.nio.file.attribute.BasicFileAttributes; 39 import java.nio.file.attribute.FileAttribute; 40 import java.nio.file.attribute.FileAttributeView; 41 import java.nio.file.spi.FileSystemProvider; 42 import java.security.AccessController; 43 import java.security.PrivilegedActionException; 44 import java.security.PrivilegedExceptionAction; 45 import java.util.HashMap; 46 import java.util.Map; 47 import java.util.Set; 48 import java.util.concurrent.ExecutorService; 49 import java.util.zip.ZipException; 50 51 /** 52 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 53 */ 54 public class ZipFileSystemProvider extends FileSystemProvider { 55 56 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>(); 57 58 public ZipFileSystemProvider() {} 59 60 @Override 61 public String getScheme() { 62 return "jar"; 63 } 64 65 protected Path uriToPath(URI uri) { 66 String scheme = uri.getScheme(); 67 if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) { 68 throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'"); 69 } 70 try { 71 // only support legacy JAR URL syntax jar:{uri}!/{entry} for now 72 String spec = uri.getRawSchemeSpecificPart(); 73 int sep = spec.indexOf("!/"); 74 if (sep != -1) { 75 spec = spec.substring(0, sep); 87 if (!attrs.isRegularFile()) 88 throw new UnsupportedOperationException(); 89 return true; 90 } catch (IOException ioe) { 91 return false; 92 } 93 } 94 95 @Override 96 public FileSystem newFileSystem(URI uri, Map<String, ?> env) 97 throws IOException 98 { 99 Path path = uriToPath(uri); 100 synchronized(filesystems) { 101 Path realPath = null; 102 if (ensureFile(path)) { 103 realPath = path.toRealPath(); 104 if (filesystems.containsKey(realPath)) 105 throw new FileSystemAlreadyExistsException(); 106 } 107 ZipFileSystem zipfs; 108 try { 109 if (env.containsKey("multi-release")) { 110 zipfs = new JarFileSystem(this, path, env); 111 } else { 112 zipfs = new ZipFileSystem(this, path, env); 113 } 114 } catch (ZipException ze) { 115 String pname = path.toString(); 116 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 117 throw ze; 118 // assume NOT a zip/jar file 119 throw new UnsupportedOperationException(); 120 } 121 if (realPath == null) { // newly created 122 realPath = path.toRealPath(); 123 } 124 filesystems.put(realPath, zipfs); 125 return zipfs; 126 } 127 } 128 129 @Override 130 public FileSystem newFileSystem(Path path, Map<String, ?> env) 131 throws IOException 132 { 133 ensureFile(path); 134 try { 135 ZipFileSystem zipfs; 136 if (env.containsKey("multi-release")) { 137 zipfs = new JarFileSystem(this, path, env); 138 } else { 139 zipfs = new ZipFileSystem(this, path, env); 140 } 141 return zipfs; 142 } catch (ZipException ze) { 143 String pname = path.toString(); 144 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 145 throw ze; 146 throw new UnsupportedOperationException(); 147 } 148 } 149 150 @Override 151 public Path getPath(URI uri) { 152 String spec = uri.getSchemeSpecificPart(); 153 int sep = spec.indexOf("!/"); 154 if (sep == -1) 155 throw new IllegalArgumentException("URI: " 156 + uri 157 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR"); 158 return getFileSystem(uri).getPath(spec.substring(sep + 1)); 159 } 160 161 162 @Override 163 public FileSystem getFileSystem(URI uri) { 164 synchronized (filesystems) { 165 ZipFileSystem zipfs = null; 166 try { 167 zipfs = filesystems.get(uriToPath(uri).toRealPath()); | 36 import java.nio.file.*; 37 import java.nio.file.DirectoryStream.Filter; 38 import java.nio.file.attribute.BasicFileAttributes; 39 import java.nio.file.attribute.FileAttribute; 40 import java.nio.file.attribute.FileAttributeView; 41 import java.nio.file.spi.FileSystemProvider; 42 import java.security.AccessController; 43 import java.security.PrivilegedActionException; 44 import java.security.PrivilegedExceptionAction; 45 import java.util.HashMap; 46 import java.util.Map; 47 import java.util.Set; 48 import java.util.concurrent.ExecutorService; 49 import java.util.zip.ZipException; 50 51 /** 52 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 53 */ 54 public class ZipFileSystemProvider extends FileSystemProvider { 55 56 // Property used to specify the entry version to use for a multi-release JAR 57 protected static final String RELEASE_VERSION = "releaseVersion"; 58 // Original property used to specify the entry version to use for a 59 // multi-release JAR which is kept for backwards compatability. 60 protected static final String MULTI_RELEASE = "multi-release"; 61 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>(); 62 63 public ZipFileSystemProvider() {} 64 65 @Override 66 public String getScheme() { 67 return "jar"; 68 } 69 70 protected Path uriToPath(URI uri) { 71 String scheme = uri.getScheme(); 72 if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) { 73 throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'"); 74 } 75 try { 76 // only support legacy JAR URL syntax jar:{uri}!/{entry} for now 77 String spec = uri.getRawSchemeSpecificPart(); 78 int sep = spec.indexOf("!/"); 79 if (sep != -1) { 80 spec = spec.substring(0, sep); 92 if (!attrs.isRegularFile()) 93 throw new UnsupportedOperationException(); 94 return true; 95 } catch (IOException ioe) { 96 return false; 97 } 98 } 99 100 @Override 101 public FileSystem newFileSystem(URI uri, Map<String, ?> env) 102 throws IOException 103 { 104 Path path = uriToPath(uri); 105 synchronized(filesystems) { 106 Path realPath = null; 107 if (ensureFile(path)) { 108 realPath = path.toRealPath(); 109 if (filesystems.containsKey(realPath)) 110 throw new FileSystemAlreadyExistsException(); 111 } 112 ZipFileSystem zipfs = getZipFileSystem(path, env); 113 if (realPath == null) { // newly created 114 realPath = path.toRealPath(); 115 } 116 filesystems.put(realPath, zipfs); 117 return zipfs; 118 } 119 } 120 121 @Override 122 public FileSystem newFileSystem(Path path, Map<String, ?> env) 123 throws IOException 124 { 125 ensureFile(path); 126 ZipFileSystem zipfs = getZipFileSystem(path, env); 127 return zipfs; 128 } 129 130 private ZipFileSystem getZipFileSystem(Path path, Map<String, ?> env) throws IOException { 131 ZipFileSystem zipfs; 132 try { 133 if (env.containsKey(RELEASE_VERSION) || 134 env.containsKey(MULTI_RELEASE)) { 135 zipfs = new JarFileSystem(this, path, env); 136 } else { 137 zipfs = new ZipFileSystem(this, path, env); 138 } 139 } catch (ZipException ze) { 140 String pname = path.toString(); 141 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 142 throw ze; 143 throw new UnsupportedOperationException(); 144 } 145 return zipfs; 146 } 147 148 @Override 149 public Path getPath(URI uri) { 150 String spec = uri.getSchemeSpecificPart(); 151 int sep = spec.indexOf("!/"); 152 if (sep == -1) 153 throw new IllegalArgumentException("URI: " 154 + uri 155 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR"); 156 return getFileSystem(uri).getPath(spec.substring(sep + 1)); 157 } 158 159 160 @Override 161 public FileSystem getFileSystem(URI uri) { 162 synchronized (filesystems) { 163 ZipFileSystem zipfs = null; 164 try { 165 zipfs = filesystems.get(uriToPath(uri).toRealPath()); |