Can I open a zip filesystem in read-only mode?

200 Views Asked by At

I'm trying to unify code to list files whether they're in a directory or on the classpath.

I can get a URL to the location of the resource:

URI uri = FontManagerTestUtils.class.getResource("/fonts").toURI();
FileSystem fileSystem =
    FileSystems.newFileSystem(uri, Collections.emptyMap());

But the security manager doesn't like it:

java.security.AccessControlException: access denied ("java.io.FilePermission" "/Users/tester/.gradle/caches/modules-2/files-2.1/com.acme.utilities/acme-utils/2.0.4/2c3dd60154b9f63c52ee0defe82422c2987f69b5/acme-utils-2.0.4.jar" "write")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
    at sun.nio.fs.UnixPath.checkWrite(UnixPath.java:801)
    at sun.nio.fs.UnixFileSystemProvider.checkAccess(UnixFileSystemProvider.java:294)
    at java.nio.file.Files.isAccessible(Files.java:2455)
    at java.nio.file.Files.isWritable(Files.java:2521)
    at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:125)
    at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)

I don't particularly intend to write back into the jar, though, and my security policy does permit reading from jar files on the classpath, so is there a way to just tell it not to open it with write access?

0

There are 0 best solutions below