java. util. zip:putNextEntry

I'm trying to use Java util. Net to modify the file in the zip file

Here are the most important parts I tried:

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);  // HERE the exception is thrown
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}

Information: ZIF = zipfile, instantiate correctly and open from the existing file; Zos = zipoutputstream, instantiate the new file correctly

This is the exception thrown:

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)

Testclass: 117 is the comment line, which is where it failed

Interestingly, the system out. Println works normally, and no files are reported as 0 size

Did anyone see the mistakes I might make?

Any idea is appreciated

Thank you for your greetings

Solution

It's not enough just to write zipentry to the stream. You still need to write content

this.zos.putNextEntry(new ZipEntry(currentEntry));
        int len;
        while ((len = zis.read(buf)) >= 0)
        {
           zos.write(buf,len);
        }

Complete example here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>