所以我从一个数据库(各种mimetypes)中抓取一组blob,然后尝试将它们压缩起来,供用户通过http响应下载。我可以进行下载,但是当我尝试打开下载的zip文件时,它说“存档格式未知或已损坏”。我已经尝试了以下代码与application / zip,application / octet-stream和application / x-zip-compressed,但我开始假设问题在于我如何添加文件。我也使用Java 7和Grails 2.2.4。
对此的任何帮助将不胜感激。谢谢!
final ZipOutputStream out = new ZipOutputStream(new FileOutputStream("test.zip"));
for (Long id : ids){
Object[] stream = inlineSamplesDataProvider.getAttachmentStream(id);
if (stream) {
String fileName = stream[0]
String mimeType = (String) stream[1];
InputStream inputStream = stream[2]
byte[] byteStream = inputStream.getBytes();
ZipEntry zipEntry = new ZipEntry(fileName)
out.putNextEntry(zipEntry);
out.write(byteStream, 0, byteStream.length);
out.closeEntry();
}
}
out.close();
response.setHeader("Content-Disposition", "attachment; filename=\"" + "test.zip" + "\"");
response.setHeader("Content-Type", "application/zip");
response.outputStream << out;
response.outputstream.flush();
答案 0 :(得分:5)
我在这里找到答案:Returning ZipOutputStream to browser
好吧,最终为我工作的是将ZipOutputStream转换为ByteArrayOutputStream并将其作为byte []写入响应:
/blog/2015/10/20/sample-post/
感谢所有帮助过的人!