Java:Zipinputstream到Zipoutputstream会导致“找不到中心目录签名”错误

时间:2016-12-02 08:39:05

标签: java zipinputstream zipoutputstream

我尝试将Zip从Zipinputstream复制到Zipoutputstream

我将Zip存储为byte[]在Oracle数据库中。我使用Zipinputstream来解压缩zip(稍后我想编辑Zip),然后将其放入Zipoutputstream以获取新的byte[]并使用此数组稍后通过一个ServletOutputStream。当我创建一个新文件时 - 没有Zipinputstream - 它可以工作。但是当我使用Zipinputstream时,我得到了错误。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

我的猜测是你应该在zos.close()之前byteArrayOutputStream.close()

更新:

并移动:

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

在zos.close();

之后