EOFException:ZLIB 输入流意外结束

问题描述 投票:0回答:2

我在解压缩文件并将其写入另一个文件时遇到这个问题。这是代码。任何人都可以让我知道需要进行哪些更改吗?

我在线路中遇到此异常

while ((len = zis.read(buffer)) > 0)

private FileItem readZippedFileRequest(HttpServletRequest request,Part part, String fileName) {

    FileItem fileItem = null;
    byte[] buffer = new byte[1024];

    InputStream inputStream = part.getInputStream();
    ZipInputStream zis = new ZipInputStream(inputStream);

    ZipEntry entry;    
    while ((entry = zis.getNextEntry()) != null) {

        ByteArrayOutputStream fos = new ByteArrayOutputStream();

        int len;
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        InputStream myByteArray = new ByteArrayInputStream(fos.toByteArray());
        fileItem = createCSVFile(myByteArray, fileName,ImportExportConstant.FILE_TYPE_EXCEL);
    }       

    return fileItem;
}
java zip
2个回答
0
投票

您的代码没有任何问题。正如消息所述,该文件有问题。您确定它是压缩的,而不是 GZipped 吗?对零件进行 GZipped 更为常见。尝试一下

GZIPInputStream.

注意,没有必要

ByteArrayInputStream.
这完全是浪费时间和空间。只需将 zip/gzip 输入流直接传递到您的
createCSVFile()
方法即可。


0
投票

我也有这个错误,我搜索了一下...我读到在

zis.closeEntry();
之前必须有
len = zis.read(buffer)
,但我尝试了,然后错误出现在
zis.closeEntry();
我问了谷歌,这是答案:

!回答!

我尝试写了一点,然后我把

throws IOException
换成
try/catch-block
,现在就可以了。 该异常是一个众所周知的错误。您必须将所有内容放入
try/catch-block
中,并且在 catch 中不执行任何操作。

private FileItem readZippedFileRequest(HttpServletRequest request,Part part, String fileName) {

    FileItem fileItem = null;
    byte[] buffer = new byte[1024];
    try{
        InputStream inputStream = part.getInputStream();
        ZipInputStream zis = new ZipInputStream(inputStream);

        ZipEntry entry;    
        while ((entry = zis.getNextEntry()) != null) {

            ByteArrayOutputStream fos = new ByteArrayOutputStream();

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            InputStream myByteArray = new   ByteArrayInputStream(fos.toByteArray());
            fileItem = createCSVFile(myByteArray, fileName,ImportExportConstant.FILE_TYPE_EXCEL);
        }       
    }catch(IOException ex){
        //Do nothing here
    }
    return fileItem;
}
© www.soinside.com 2019 - 2024. All rights reserved.