6

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();
Sue
  • 73
  • 1
  • 4
  • Why? Just copy the bytes. You don't need all this complication. – user207421 Dec 02 '16 at 08:57
  • Because I later need to edit the zip (put new files into, delete some etc) But if plain copy is not working, then I cant edit it. – Sue Dec 02 '16 at 09:17

2 Answers2

8

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();

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

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.

Flore
  • 151
  • 2
  • 5