6

我尝试将 Zip 从 a 复制Zipinputstream到 a Zipoutputstream

我将 Zip 存储byte[]在 Oracle 数据库中。我Zipinputstream用来解压缩 zip(稍后我想编辑 Zip),然后将其放入 aZipoutputstream中以获取新的byte[]并使用此数组稍后通过 a 下载文件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();
4

1 回答 1

8

我的猜测是 You should do zos.close()before byteArrayOutputStream.close()

更新:

并移动:

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

zos.close();

于 2016-12-02T08:55:45.427 回答