Here's a problem I've been struggling with for a while now, and googling gave me nothing useful. Here goes.
I have an archive (archive.zip), and I want to create a file in the archive (newfile.txt) and write some text to it. Here is the (boiled-down) code:
Path archive = Paths.get("/home/user/archive.zip");
FileSystem fs = FileSystems.newFileSystem(archive, null);
Path fileInArchive = fs.getPath("/newfile.txt");
OutputStream output = Files.newOutputStream(fileInArchive, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
output.write("Testing!".getBytes());
output.flush();
// I've also tried putting:
// ((DeflaterOutputStream) output).finish();
output.close();
As you can see it is fairly simple. The stream returned by Files.newOutputStream is a ZipFileSystem.EntryOutputStream (which extends DeflaterOutputStream). Flushing, finishing and closing the stream does not seem to have any effect.
The weird thing is that I've used similar code to read text from files in archives, which works fine.
Any suggestions or fixes would be greatly appreciated! Thanks.
Update: apparently this is marked as a duplicate of: Appending files to a zip file with Java, but honestly I fail to see why, as that is a generic question about appending to zip files, and mine is way more specific with a code example. If I missed anything useful from there it'd be great if someone could put a comment.