为什么以下代码在通过 servlet 输出流输出时会生成损坏的 zip 文件?使用 FileOutputStream 将 ZIP 写入本地磁盘时,输出流似乎没有损坏。
// Create zip stream
ZipOutputStream zos = new ZipOutputStream(this.servletOutputStream);
// prepare a new entry
ZipEntry zipEntry = new ZipEntry(forZip.getName());
zipEntry.setSize(forZip.getTotalSpace());
zipEntry.setTime(System.currentTimeMillis());
// write entry
zos.putNextEntry(zipEntry);
// write the file to the entry
FileInputStream toBeZippedInputStream = new FileInputStream(forZip);
IOUtils.copy(toBeZippedInputStream, zos);
zos.flush();
// close entry
zos.closeEntry();
// close the zip
zos.finish();
zos.close();
this.servletOutputStream.flush();
this.servletOutputStream.close();
// close output stream
IOUtils.closeQuietly(toBeZippedInputStream);
这可能是刷新/关闭流的顺序问题吗?