java 压缩只读文件系统,Java Zip文件系统提供程序:ReadOnly在远程驱动器上[Windows]...

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?

解决方案

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:

Create the zip archive in a temp folder

Populate the zip archive

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值