gpt4 book ai didi

Java 7 zip 文件系统提供程序似乎不接受 URI 中的空格

转载 作者:IT老高 更新时间:2023-10-28 20:53:49 25 4
gpt4 key购买 nike

我一直在测试所有可能的变化和排列,但我似乎无法为包含空格的路径 (URI) 使用 zip/jar 方案构建 FileSystemProvider。 Oracle Docs 上有一个非常简单的测试用例。 .我冒昧地修改了这个例子,只是在 URI 中添加了空格,它就停止了工作。以下片段:

import java.util.*;
import java.net.URI;
import java.nio.file.*;

public class Test {
public static void main(String [] args) throws Throwable {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = new URI("jar:file:/c:/dir%20with%20spaces/zipfstest.zip");
Path dir = Paths.get("C:\\dir with spaces");
if(Files.exists(dir) && Files.isDirectory(dir)) {
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {}
}
}
}

当我执行此代码(Windows、JDK7u2、x32 和 x64)时,我得到以下异常:

java.lang.IllegalArgumentException: Illegal character in path at index 12: file:/c:/dir with spaces/zipfstest.zip
at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:87)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:107)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:322)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:272)

如果我使用 + 而不是 %20 作为空格转义字符,则会引发不同的异常:

java.nio.file.NoSuchFileException: c:\dir+with+spaces\zipfstest.zip
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:229)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430)
at java.nio.file.Files.newOutputStream(Files.java:170)
at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:116)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:322)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:272)

我可能遗漏了一些非常明显的东西,但这是否表明提供的 ZIP/JAR 文件系统提供程序存在问题?

编辑:

另一个基于 File 对象的用例,根据评论中的要求:

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
try {
File zip = new File("C:\\dir with spaces\\file.zip");
URI uri = URI.create("jar:" + zip.toURI().toURL());
Map<String, String> env = new HashMap<>();
env.put("create", "true");
if(zip.getParentFile().exists() && zip.getParentFile().isDirectory()) {
FileSystems.newFileSystem(uri, env);
}
} catch (Exception ex) {
Logger.getAnonymousLogger().log(Level.SEVERE, null, ex);
System.out.println();
}
}
}

再次抛出异常为:

java.lang.IllegalArgumentException: Illegal character in path at index 12: file:/C:/dir with spaces/file.zip
at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:87)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:107)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:322)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:272)

最佳答案

实际上,进一步的分析似乎表明 ZipFileSystemProvider 存在问题。类中包含的 uriToPath(URI uri) 方法执行以下代码段:

String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep != -1)
spec = spec.substring(0, sep);
return Paths.get(new URI(spec)).toAbsolutePath();

从 URI.getSchemeSpecificPart() 的 JavaDocs 我们可以看到以下内容:

The string returned by this method is equal to that returned by the getRawSchemeSpecificPart method except that all sequences of escaped octets are decoded.

这个相同的字符串然后作为参数传递回新的 URI() 构造函数。由于任何转义的八位字节都由 getSchemeSpecificPart() 反转义,因此如果原始 URI 包含任何转义字符,它们将不会传播到新的 URI - 因此出现异常。

一种潜在的解决方法 - 遍历所有可用的文件系统提供程序并获取对规范等于“jar”的人的引用。然后使用它来创建一个仅基于路径的新文件系统。

关于Java 7 zip 文件系统提供程序似乎不接受 URI 中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9873845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com