多个文件存档的 ZIP 文件 servlet 响应已损坏
我正在尝试压缩一包文件并通过 servlet 返回 ZIP。如果我只有一个文件,则效果很好,但当我尝试添加更多文件时,它们似乎只是被附加到第一个 ZipEntry 中,因此 zip 存档会损坏。
private void writeZipResponse(HttpServletResponse response,
List<File> bundle) {
try {
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(bout);
ServletOutputStream out =response.getOutputStream();
for (File file : bundle) {
FileInputStream fi = new FileInputStream(file);
byte bytes[] = new byte[(int)file.length()];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=fi.read(bytes, offset, bytes.length-offset)) >= 0)
{ offset += numRead; }
// Ensure all the bytes have been read in
if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); }
fi.close();
ZipEntry zipEntry = new ZipEntry(file.getName());
//zipEntry.setCompressedSize(file.length());
zipEntry.setSize(offset);
CRC32 crc = new CRC32();
crc.update(bytes);
zipEntry.setCrc(crc.getValue());
zout.putNextEntry(zipEntry);
zout.write(bytes, 0, offset);
zout.closeEntry();
zout.finish();
fi.close();
}
zout.close();
response.setContentType("application/zip");
response.setHeader("Content-Disposition",
"attachment; filename=hivseqdb.zip;");
out.write(bout.toByteArray());
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm trying ZIP a bundle of File's and return ZIP through a servlet. This works fine if I just have one file but when I try to add more it seems they are just being appended to the first ZipEntry and thus the zip archive gets corrupted.
private void writeZipResponse(HttpServletResponse response,
List<File> bundle) {
try {
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(bout);
ServletOutputStream out =response.getOutputStream();
for (File file : bundle) {
FileInputStream fi = new FileInputStream(file);
byte bytes[] = new byte[(int)file.length()];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=fi.read(bytes, offset, bytes.length-offset)) >= 0)
{ offset += numRead; }
// Ensure all the bytes have been read in
if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); }
fi.close();
ZipEntry zipEntry = new ZipEntry(file.getName());
//zipEntry.setCompressedSize(file.length());
zipEntry.setSize(offset);
CRC32 crc = new CRC32();
crc.update(bytes);
zipEntry.setCrc(crc.getValue());
zout.putNextEntry(zipEntry);
zout.write(bytes, 0, offset);
zout.closeEntry();
zout.finish();
fi.close();
}
zout.close();
response.setContentType("application/zip");
response.setHeader("Content-Disposition",
"attachment; filename=hivseqdb.zip;");
out.write(bout.toByteArray());
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要移动 zout.finish();循环外的线。
You need to move the zout.finish(); line outside of the loop.