How to check if a generated zip file is corrupted?

后端 未结 8 1693
梦毁少年i
梦毁少年i 2020-12-05 10:33

we have a piece of code which generates a zip file on our system. Everything is ok, but sometimes this zip file while opened by FilZip or WinZip is considered to be corrupte

8条回答
  •  既然无缘
    2020-12-05 10:39

    in my implementation it looks like that. maybe it helps you:

    //[...]
    
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
    
        zos.putNextEntry(new ZipEntry(file.getName()));
    
        try {
            final byte[] buf = new byte[BUFFER_SIZE];
            while (true) {
                final int len = bis.read(buf);
                if (len == -1) {
                    break;
                }
                zos.write(buf, 0, len);
            }
            zos.flush();
            zos.closeEntry();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                LOG.debug("Buffered Stream closing failed");
            } finally {
                fis.close();
            }
        }
    } catch (IOException e) {
        throw new Exception(e);
    }
    
    //[...]
    zos.close
    

提交回复
热议问题