Reputation: 153
I'm writing some data to an entry on a zip file using ZipArchiveOutputStream. I need to write to a file1, create another file2 entry, add some data to that entry, and somehow write again to the file1 I was writing before.
I do have the following:
File zipFile = new File("zipTest.zip");
ZipArchiveOutputStream zipOut;
try {
zipOut = new ZipArchiveOutputStream(zipFile);
ArchiveEntry file1 = new ZipArchiveEntry("file1.txt");
ArchiveEntry file2 = new ZipArchiveEntry("file2.txt");
zipOut.putArchiveEntry(file1);
zipOut.write("SOME TEXT FILE 1, LINE 1\n".getBytes());
zipOut.closeArchiveEntry();
zipOut.putArchiveEntry(file2);
zipOut.write("SOME TEXT FILE 2, LINE1\n".getBytes());
zipOut.closeArchiveEntry();
zipOut.putArchiveEntry(file1);
zipOut.write("SOME TEXT FILE 1, LINE 2\n".getBytes());
zipOut.closeArchiveEntry();
zipOut.finish();
zipOut.close();
} catch(...)
However only "SOME TEXT FILE 1, LINE 1" is being written to file1 entry, and I would like to have the 2 lines, of course.
I realize that a solution would be writing a full entry and then another, etc, but in my case I would like to avoid that because I would need to create some large temporary files.
Upvotes: 0
Views: 962
Reputation: 122364
Once you've closed an entry you can't go back and add more data to that same entry, so what you are trying is not going to work.
However it's not an error to have two entries in the same zip file with the same name. You'll only be able to access one of the entries from the central directory by name but the output stream will allow you to add them both, and a ZipArchiveInputStream reading from the resulting file would give you both (the input stream just reads the local entry headers, not the central directory).
Upvotes: 1