I am trying to modify a file in a zip file using java.util.net Since it is not possible to directly modify the file, and I want to change only one file, I simply create a new zip file containing mainly the contents of the template and replace the specific file with at least this plan.
Here are the most important lines of my attempt:
Enumeration<? extends ZipEntry> entries = zif.entries();
while (entries.hasMoreElements()) {
ZipEntry currentEntry = entries.nextElement();
if (!currentEntry.isDirectory() && currentEntry.getSize() >0 && currentEntry.getCompressedSize() > 0)
{
System.out.println(currentEntry.getName() + ": " + currentEntry.getSize() + "-" + currentEntry.getMethod());
if (currentEntry.getName() != "file_i_want_to_change")
{
try {
this.zos.putNextEntry(currentEntry);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Information: zif = ZipFile, correctly initialized and opened from an existing file; zos = ZipOutputStream correctly initialized for the new file.
This is an exception:
java.util.zip.ZipException: invalid entry size (expected 39 but got 0 bytes)
at java.util.zip.ZipOutputStream.closeEntry(ZipOutputStream.java:228)
at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:144)
at TestClass.replace(TestClass.java:117)
at TestClass.main(TestClass.java:10)
whereas TestClass: 117 is a comment line in which it does not work.
The funny thing is that System.out.println in a piece of code works fine and without messages about files of size 0.
Does anyone see a mistake I could make?
Any idea is welcome.
Thanks in advance and best wishes.