Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 416
Sue Avatar asked Dec 02 '16 08:12

Sue


1 Answers

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

like image 77
Maurice Perry Avatar answered Oct 04 '22 18:10

Maurice Perry