0

Zip files in Java 7 can be treated similarly like file system:
http://fahdshariff.blogspot.cz/2011/08/java-7-working-with-zip-files.html

In my project I'd like to process the zip file stored in resources. But I cannot find any efficient way for converting the resource stream (getResourceAsStream) into a new FileSystems object directly without storing that file to the disk first:

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);

Am I missing something?

4

1 回答 1

2

No, this isn't possible. The reason for this is that a) streams often can't be read twice and b) ZIP archives need random reading.

The list of files is attached at the end, so you need to skip the data, find the file entry, locate the position of the data entry and then seek backwards.

This is why code like Java WebStart downloads and caches the files.

Note that you don't have to write the ZIP archive to disk, though. You can use ShrinkWrap to create an in-memory filesystem.

于 2013-09-02T15:46:09.403 回答