所以我正在从数据库中获取一系列blob(各种mimetypes),并尝试将它们压缩成zip文件,通过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();