如果路径包含空格,则 Zip 文件的 URI 不正确 [英] URI to file in Zip incorrect if path contains spaces

查看:26
本文介绍了如果路径包含空格,则 Zip 文件的 URI 不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 zip 文件条目的 URI,以便保留对其内容的引用,而不必保持 zip 文件打开.

I want to get the URIs to the entries of a zip file in order to keep references to it's contents without having to keep the zip file open.

因此,我使用 zip 文件系统打开 zip 文件并将条目的路径导出为 URI.

Therefore I open the zip file using the zip filesystem and export the Path of the entries as URI.

Path zipfile = ...
URI uriOfFileInZip;
try(FileSystem fs = FileSystems.newFileSystem(zipfile, null)){
   Path fileInZip = fs.getPath("fileInZip.txt");
   uriOfFileInZip = fileInZip.toUri();
}

现在我想再次读取文件,所以我尝试打开文件流.

Now I want to read the file again, so I try to open a stream to the file.

InputStream is = uriOfFileInZip.toURL().openStream();

只要 zip 文件的路径不包含任何空格,就可以使用.只要它包含空格,我就会收到这样的错误

This works as long as the path of the zip file does not contain any spaces. As soon as it contains spaces, I get an error like this

java.io.FileNotFoundException: D:\example\name%20of%20zipfile.zip (The system cannot find the file specified)

zip 中文件的 URI 是

the URI to the file in the zip is

jar:file:///D:/example/name%2520of%2520zipfile.zip!/fileInZip.txt

压缩包的名称是

D:\example\name of zipfile.zip

我想知道 %2520 这似乎是 URL 编码的问题,但这不应该透明处理吗?或者这是一个错误?

I wonder about the %2520 this seems like an issue with the URL encoding, but shouldn't this be handled transparently? Or is it a bug?

有什么想法可以解决这个问题吗?

Any ideas to solve this problem?

推荐答案

看起来像一个错误.

似乎 com.sun.nio.zipfs.ZipPath.toUri() 要么搞砸了,要么我还没有阅读相应的 RFC ;-).玩弄其他一些文件名.zip 文件路径似乎有双重编码,但 zip 中的文件条目没有.除了不使用 URI 方法之外,您还可以自己从头开始构建 URI,但是这样您就不再那么灵活了.或者你只是撤消不必要的编码:

Seems as if com.sun.nio.zipfs.ZipPath.toUri() is either messed up, or I didn't read the corresponding RFC yet ;-). Played around with some other file names. There seems to be a double encoding going on for the zip file path, but not for the file entry in the zip. Besides not using the URI-approach you could also build the URI yourself from scratch, but then you are not that flexible anymore. Or you just undo the unnecessary encoding:

String uriParts[] = uriOfFileInZip.toString().split("!");
uriParts[0] = URLDecoder.decode(uriParts[0], "UTF-8");
uriOfFileInZip = URI.create(String.join("!", uriParts));

但老实说,我宁愿尝试省略 zip 文件的 URI,或者如果您真的需要,事先重命名文件 ;-) 更好的是:如果它的行为与相应的 RFC 中所述不符,则打开一个错误.

But to be honest, I would rather try to omit the URI for zip files or if you really have to, rename the files beforehand ;-) Better yet: open a bug if it does not behave as stated in the corresponding RFCs.

您可能还想从以下有关错误等的问题中获取一些其他信息:Java 7 zip 文件系统提供程序似乎不接受 URI 中的空格

You may also want to get some additional information from the following question regarding bug, etc.: Java 7 zip file system provider doesn't seem to accept spaces in URI

EDIT(添加没有 URI 的提案):

您还可以尝试完全使用 Path 实例 (fileInZip) 而不是 URI,因为路径实例知道"其文件系统.只要您需要访问 zip 中的文件,您就可以根据 Path 实例的信息 (fileInZip.getFileSystem()) 创建一个新的 FileSystem.我没有详细说明,但至少文件存储应该包含再次访问 zip 文件的所有必要信息.有了这些信息,你可以调用类似 FileSystems.newFileSystem(Paths.get(fileStoreName), null) 的东西.然后您还可以使用 Files.newInputStream(fileInZip) 来创建您的 InputStream.此处无需使用 URI.

You can also try to completely work with your Path instance (fileInZip) instead of the URI, as the path instance "knows" its filesystem. As soon as you need access to the file inside the zip, you create a new FileSystem based on the information of the Path instance (fileInZip.getFileSystem()). I did not elaborate that completely, but at least the file store should contain all the necessary information to access the zip file again. With that information you could call something like FileSystems.newFileSystem(Paths.get(fileStoreName), null). Then you can also use Files.newInputStream(fileInZip) to create your InputStream. No need to use URI here.

这篇关于如果路径包含空格,则 Zip 文件的 URI 不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆