Problem statement:
Replace a file inside a zip file which is inside yet another zip file. Think the following hierarchy:
root.zip
child.zip
- file_to_replace.txt
What I've tried:
Filesystems.newFileSystem
Pros: Can update in place
Cons: I have not been able to figure out how to make a new filesystem using the
child.zipfile.
What I haven't tried:
ZipFile
Pros: Should "just work"
Cons: Need to extract and recreate the files
- This is what I did in a similar python script
Constraints:
No external libraries
Must be Java 8 compatible
I'm probably going to switch to ZipFile since it will "just work", but I'd like to have a better method that does not involve recreating the zip files.
Similar but unanswered questions:
* Java 8 FileSystem access file in zip package zipped in another zip
Sample code:
try (FileSystem zf = FileSystems.newFileSystem("root.zip")) {
for (Path path : Files.walk(zf.getRootDirectories().iterator().next()).filter(p -> p.toString().endsWith(".zip")).collect(Collectors.toList())) {
try (FileSystem zfchild = Files.newFileSystem(path)) { // This fails
// Do stuff to file_to_replace.txt here. Example:
Files.copy(Paths.get("test.txt"), zfchild.getPath("file_to_replace.txt"), StandardCopyOption.REPLACE_EXISTING);
}
}
}
Exception from second Files.newFileSystem:
Exception in thread "main" java.nio.file.ProviderNotFoundException: Provider not found
at java.nio.file.FileSystem.snewFileSystem(FileSystems.java:407)