I ran into an unexpected case while creating a zip file using Java's FileSystem
API, where the disk space ran out in the process of creating the file.
Because of a coding error, the source files where deleted even though the zip file failed to be created. I have salvaged a tmp-file from the server, and am wondering if I can use the FileSystem
API to salvage the data by loading the temporary file and finish the write.
The code used to write a zip file looks like this:
final Map<String, String> env = new HashMap<>();
env.put("create", "true");
final String zipArchiveFilename = System.currentTimeMillis() + ".zip";
final URI uri = URI.create("jar:file:" + zipDir.getAbsolutePath() + "/" + zipArchiveFilename);
final List<Path> files = ...;
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
for (Path file : files) {
Files.copy(file, zipfs.getPath(file.toFile().getAbsolutePath().replace(dir.getAbsolutePath(), "")), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
Logger.error("Failed to create zip file:", e);
}