'Java: Zipinputstream to Zipoutputstream leads to "end-of-central-directory signature not found" Error

I try to copy a Zip from a Zipinputstream to a Zipoutputstream.

I store the Zip as byte[] in a Oracle database. I use Zipinputstream to decompress the zip (later I want to edit the Zip) and then put it into a Zipoutputstream to get a new byte[] and use this array to download the file later via a ServletOutputStream. When I create a new file - without the Zipinputstream - it works. But when I use the Zipinputstream I get the error.

Here is my code:

        ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(fileFromDataBase),
                Charset.forName("UTF-8"));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream, Charset.forName("UTF-8"));
        ZipEntry currentEntry;
        byte[] buffer = new byte[8192];
        while ((currentEntry = zipInputStream.getNextEntry()) != null) {
            ZipEntry newEntry = new ZipEntry(currentEntry.getName());
            zos.putNextEntry(newEntry);
            int length;
            while ((length = zipInputStream.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();                   
        }

        //TO Object to download later the Zipfile from html page
        paketDownloadTO = new PaketDownloadTO();
        paketDownloadTO.setData(byteArrayOutputStream.toByteArray());
        paketDownloadTO.setFileName(fileName);

        zos.finish();
        zipInputStream.close();
        zos.close();


Solution 1:[1]

My guess is that You should do zos.close() before byteArrayOutputStream.close().

UPDATE:

and move:

paketDownloadTO = new PaketDownloadTO();
paketDownloadTO.setData(byteArrayOutputStream.toByteArray());
paketDownloadTO.setFileName(fileName);

after zos.close();

Solution 2:[2]

The problem is, calling byteArrayOutputStream.toByteArray() before calling zipInputStream.close()

The ZipInputStream only write the end-of-central-directory when close is called, because before that it does not know if you're going to add new entries or not.

But since byteArrayOutputStream.toByteArray() returns a copy of the byte array at the time you're calling it. It does not contains the end-of-central-directory, which is written to the stream at a later time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Flore