[英]Generated zip file is always corrupted after download on Mac
假设我有一个对象集合,我想用 zip 包装它们。我的 zip 转换 class 看起来像:
public class ZipFile {
public byte[] createZipByteArray(String fileName, byte[] content) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (byteArrayOutputStream; ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
zipOutputStream.setLevel(Deflater.NO_COMPRESSION);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(content);
zipOutputStream.closeEntry();
zipOutputStream.flush();
}
return byteArrayOutputStream.toByteArray();
}
}
我的 controller 看起来像:
@GetMapping(value = "objectWithDetails/zip", produces = "application/zip")
@RolesAllowed("QUERY_DATA")
public ResponseEntity<byte[]> getObjectWithDetails(String limit, HttpServletResponse response) throws IOException {
response.setContentType("application/zip");
List<ObjectSpecification> objectRepresentation = recipeService.getObjectRepresentation(limit);
byte[] objectsBytes = objectMapper.writeValueAsBytes(objectRepresentation);
ZipFile zipFile = new ZipFile();
return ResponseEntity
.ok()
.contentType(MediaType.valueOf("application/zip"))
.header("Content-Encoding", "UTF-8")
.header("Content-Disposition", String.format("attachment; filename=\"details_%s.zip\"", dateTimeProvider.nowDate()))
.body(zipFile.createZipByteArray(String.format("details_%s.zip", dateTimeProvider.nowDate()), objectsBytes));
}
在 swagger 中,我有生成的 zip 文件的下载选项。 毕竟,当我尝试打开下载的文件时,我收到以下信息:
Unable to expand filename.zip. It is in an unsupported format.
我尝试在不同的配置中关闭ZipOutputStream
和ByteArrayOutputStream
。 为 controller 中的内容类型应用了不同的选项,但我仍然无法找出为什么所有 zip 文件都已损坏。 对于我想念的内容,我将不胜感激。 干杯!
我设法通过 @k314159 找到了根本原因,即我有一个 zip 文件被 zip 文件包裹。 新版本按预期工作。
public static byte[] createZipByteArray(String fileName, byte[] content) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (byteArrayOutputStream; ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
zipOutputStream.setLevel(Deflater.DEFLATED);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(content);
zipOutputStream.closeEntry();
zipOutputStream.flush();
}
return byteArrayOutputStream.toByteArray();
}
和 controller
@GetMapping(value = "objectWithDetails", produces = "application/zip")
@RolesAllowed("QUERY_DATA")
public ResponseEntity<byte[]> getObjectWithDetails(String limit, HttpServletResponse response) throws IOException {
response.setContentType("application/zip");
List<ObjectSpecification > objectRepresentation = objectService.getObjectRepresentation(limit);
byte[] objectsBytes = objectMapper.writeValueAsBytes(objectRepresentation);
return ResponseEntity
.ok()
.contentType(MediaType.valueOf("application/zip"))
.header("Content-Encoding", "UTF-8")
.header("Content-Disposition", getObjectFileName("attachment; filename=objects_"))
.body(createZipByteArray(getObjectFileName("objects_"), objectsBytes));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.