1 /*
2 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.*;
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
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
274 @Override
275 public InputStream newInputStream(Path path, OpenOption... options)
276 throws IOException
277 {
278 return toZipPath(path).newInputStream(options);
279 }
280
281 @Override
282 public OutputStream newOutputStream(Path path, OpenOption... options)
283 throws IOException
284 {
285 return toZipPath(path).newOutputStream(options);
286 }
287
288 @Override
289 @SuppressWarnings("unchecked") // Cast to A
290 public <A extends BasicFileAttributes> A
291 readAttributes(Path path, Class<A> type, LinkOption... options)
292 throws IOException
293 {
294 if (type == BasicFileAttributes.class || type == ZipFileAttributes.class)
295 return (A)toZipPath(path).getAttributes();
296 return null;
297 }
298
299 @Override
300 public Map<String, Object>
301 readAttributes(Path path, String attribute, LinkOption... options)
302 throws IOException
303 {
304 return toZipPath(path).readAttributes(attribute, options);
305 }
306
307 @Override
308 public Path readSymbolicLink(Path link) throws IOException {
309 throw new UnsupportedOperationException("Not supported.");
310 }
311
312 @Override
313 public void setAttribute(Path path, String attribute,
314 Object value, LinkOption... options)
315 throws IOException
316 {
317 toZipPath(path).setAttribute(attribute, value, options);
318 }
319
320 //////////////////////////////////////////////////////////////
321 void removeFileSystem(Path zfpath, ZipFileSystem zfs) throws IOException {
322 synchronized (filesystems) {
323 Path tempPath = zfpath;
324 PrivilegedExceptionAction<Path> action = tempPath::toRealPath;
|
1 /*
2 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.*;
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.attribute.PosixFileAttributes;
42 import java.nio.file.spi.FileSystemProvider;
43 import java.security.AccessController;
44 import java.security.PrivilegedActionException;
45 import java.security.PrivilegedExceptionAction;
46 import java.util.HashMap;
47 import java.util.Map;
48 import java.util.Set;
49 import java.util.concurrent.ExecutorService;
50 import java.util.zip.ZipException;
51
52 /**
53 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
54 */
55 public class ZipFileSystemProvider extends FileSystemProvider {
56
57 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
58
59 public ZipFileSystemProvider() {}
60
61 @Override
115 } catch (ZipException ze) {
116 String pname = path.toString();
117 if (pname.endsWith(".zip") || pname.endsWith(".jar"))
118 throw ze;
119 // assume NOT a zip/jar file
120 throw new UnsupportedOperationException();
121 }
122 if (realPath == null) { // newly created
123 realPath = path.toRealPath();
124 }
125 filesystems.put(realPath, zipfs);
126 return zipfs;
127 }
128 }
129
130 @Override
131 public FileSystem newFileSystem(Path path, Map<String, ?> env)
132 throws IOException
133 {
134 ensureFile(path);
135 try {
136 ZipFileSystem zipfs;
137 if (env.containsKey("multi-release")) {
138 zipfs = new JarFileSystem(this, path, env);
139 } else {
140 zipfs = new ZipFileSystem(this, path, env);
141 }
142 return zipfs;
143 } catch (ZipException ze) {
144 String pname = path.toString();
145 if (pname.endsWith(".zip") || pname.endsWith(".jar"))
146 throw ze;
147 throw new UnsupportedOperationException();
148 }
149 }
150
151 @Override
152 public Path getPath(URI uri) {
153 String spec = uri.getSchemeSpecificPart();
154 int sep = spec.indexOf("!/");
155 if (sep == -1)
156 throw new IllegalArgumentException("URI: "
157 + uri
158 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
159 return getFileSystem(uri).getPath(spec.substring(sep + 1));
160 }
161
275 @Override
276 public InputStream newInputStream(Path path, OpenOption... options)
277 throws IOException
278 {
279 return toZipPath(path).newInputStream(options);
280 }
281
282 @Override
283 public OutputStream newOutputStream(Path path, OpenOption... options)
284 throws IOException
285 {
286 return toZipPath(path).newOutputStream(options);
287 }
288
289 @Override
290 @SuppressWarnings("unchecked") // Cast to A
291 public <A extends BasicFileAttributes> A
292 readAttributes(Path path, Class<A> type, LinkOption... options)
293 throws IOException
294 {
295 // unconditionally support BasicFileAttributes and ZipFileAttributes
296 if (type == BasicFileAttributes.class ||
297 type == ZipFileAttributes.class)
298 {
299 return (A)toZipPath(path).getAttributes();
300 }
301
302 // support PosixFileAttributes when activated
303 if (type == PosixFileAttributes.class) {
304 ZipPath zpath = toZipPath(path);
305 if (zpath.zfs.supportPosix) {
306 return (A)zpath.getAttributes();
307 }
308 }
309
310 throw new UnsupportedOperationException("Attributes of type " +
311 type.getName() + " not supported");
312 }
313
314 @Override
315 public Map<String, Object>
316 readAttributes(Path path, String attributes, LinkOption... options)
317 throws IOException
318 {
319 return toZipPath(path).readAttributes(attributes, options);
320 }
321
322 @Override
323 public Path readSymbolicLink(Path link) throws IOException {
324 throw new UnsupportedOperationException("Not supported.");
325 }
326
327 @Override
328 public void setAttribute(Path path, String attribute,
329 Object value, LinkOption... options)
330 throws IOException
331 {
332 toZipPath(path).setAttribute(attribute, value, options);
333 }
334
335 //////////////////////////////////////////////////////////////
336 void removeFileSystem(Path zfpath, ZipFileSystem zfs) throws IOException {
337 synchronized (filesystems) {
338 Path tempPath = zfpath;
339 PrivilegedExceptionAction<Path> action = tempPath::toRealPath;
|