Java 7中的Zip文件可以类似于文件系统:
http://fahdshariff.blogspot.cz/2011/08/java-7-working-with-zip-files.html
在我的项目中,我想处理存储在资源中的zip文件。但是,如果不首先将该文件存储到磁盘上,我就无法找到将资源流(getResourceAsStream)直接转换为新的FileSystems对象的有效方法:
Map<String, String> nameMap = new HashMap<>();
Path path = Files.createTempFile(null, ".zip");
Files.copy(MyClass.class.getResourceAsStream("/data.zip"), path, StandardCopyOption.REPLACE_EXISTING);
try (FileSystem zipFileSystem = FileSystems.newFileSystem(path, null)) {
Files.walkFileTree(zipFileSystem.getPath("/"), new NameMapParser(nameMap));
}
Files.delete(path);我是不是遗漏了什么?
发布于 2013-09-02 15:46:09
不,这不可能。原因是:( a)流通常不能读取两次,b) ZIP档案需要随机读取。
文件列表附在末尾,因此您需要跳过数据,找到文件条目,定位数据输入的位置,然后向后查找。
这就是像Java WebStart这样的代码下载和缓存文件的原因。
但请注意,您不必将ZIP存档写入磁盘。您可以使用ShrinkWrap创建内存中的文件系统。
https://stackoverflow.com/questions/18576756
复制相似问题