Java读取zip文件

Java读取zip文件,java,filesystems,zip,Java,Filesystems,Zip,我正在尝试读取ZIP文件的内容 在创建文件系统时,由于受影响的zip文件已经存在,因此会出现FileSystemAlreadyExistsException。 即使我在创建文件系统时指定了{“create”,“false”} 代码(改编自): filesystemalreadyexistException并不意味着“zip文件已经存在”。它的意思是“zip文件已经作为文件系统加载”。重新使用创建的文件系统,例如,在单例类后面或作为共享资源,或者如果您确定此zip文件不会在多个线程中打开,请在使用

我正在尝试读取ZIP文件的内容
在创建文件系统时,由于受影响的zip文件已经存在,因此会出现FileSystemAlreadyExistsException。 即使我在创建文件系统时指定了{“create”,“false”}

代码(改编自):


filesystemalreadyexistException
并不意味着“zip文件已经存在”。它的意思是“zip文件已经作为文件系统加载”。重新使用创建的
文件系统
,例如,在单例类后面或作为共享资源,或者如果您确定此zip文件不会在多个线程中打开,请在使用完
文件系统
实例后关闭它们。太好了!。以下代码替换了上面的返回语句:if(FileSystems.getFileSystem(uri.isOpen()){return FileSystems.getFileSystem(uri);}return FileSystems.newFileSystem(uri,env);但是我还是不太明白为什么。调试应用程序我很清楚,错误是在我第一次创建文件系统对象时出现的……无论如何,非常感谢您的帮助
private static FileSystem createZipFileSystem(String zipFilename,
                                          boolean create)
                                          throws IOException {
    // convert the filename to a URI
    final Path path = Paths.get(zipFilename);
    final URI uri = URI.create("jar:file:" + path.toUri().getPath());

    final Map<String, String> env = new HashMap<>();
    String value="false";

    if (create) {
        value="true";            
    }        
    env.put("create", value);

    return FileSystems.newFileSystem(uri, env);
}
 FileSystem zipFileSystem = createZipFileSystem(zipFilename, false);