Java Zip File System Provider: ReadOnly on remote drive [Windows]

萝らか妹 提交于 2019-12-24 17:09:27

问题


I have a problem with Zip File System Provider: If the zip file is on a remote drive (mapped or not seems to be irrelevant), the virtual file system is readonly, although the file itself is not. I wrote a minimal sample code:

public static void main(String[] args) throws IOException {
    File workingDir = new File(args[0]);
    File source = new File(workingDir, "in.zip");
    File target = new File(workingDir, "out.zip");
    Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);

    try (FileSystem zipfs = FileSystems.newFileSystem(target.toPath(), null)) {
        Path pathInZipfile = zipfs.getPath("test.xml");
        System.out.println("zipfile writable: " + target.canWrite());
        System.out.println("zipFS writable: " + !zipfs.isReadOnly());
        Files.delete(pathInZipfile);
        System.out.println("File successfully deleted");   
    } catch (IOException e) {
        e.printStackTrace();
    }
}

If workingDir is a local directory, everything works fine. However, if it is a (mapped) remote drive, i get:

zipfile writable: true
zipFS writable: false
Exception in thread "main" java.nio.file.ReadOnlyFileSystemException
    at com.sun.nio.zipfs.ZipFileSystem.checkWritable(ZipFileSystem.java:155)
    at com.sun.nio.zipfs.ZipFileSystem.deleteFile(ZipFileSystem.java:1335)
    at com.sun.nio.zipfs.ZipPath.delete(ZipPath.java:655)
    at com.sun.nio.zipfs.ZipFileSystemProvider.delete(ZipFileSystemProvider.java:206)
    at java.nio.file.Files.delete(Unknown Source)
    at zipfs.ZipFS.main(ZipFS.java:23)

Am I doing something wrong? Is it impossible? Is there a workaround?


回答1:


I ran into the same thing and I looked at the JDK code.

Findings

In ZipFileSystem.java there are three relevant lines:

zfpath.getFileSystem().provider().checkAccess(zfpath, AccessMode.READ);
if (!Files.isWritable(zfpath))
    this.readOnly = true;

zfPath is a Path object. Something in the Windows FileSystem provider blocks write access to a zip archive path. Doesn't seem like there is much to be done about that.

Workaround

What I used as a workaround was to:

  1. Create the zip archive in a temp folder
  2. Populate the zip archive
  3. Copy the temp file to the original mapped drive location

As long as the mapped drive is writable in contexts outside of the zip FileSystem, this method works.




回答2:


Try setting the private field readOnly in ZipFileSystem to false with reflection.



来源:https://stackoverflow.com/questions/24863465/java-zip-file-system-provider-readonly-on-remote-drive-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!