英文:
How put files into zip just for pack them and no compress them in Java
问题
在Java中,可以使用ZipOutputStream来将文件打包成ZIP文件而不进行压缩。只需将多个文件打包到一个ZIP文件中,而且需要快速完成,而压缩会花费很长时间。您可以尝试使用ZipOutputStream的Stored方法,但结果的ZIP文件可能会损坏。
编辑后的代码如下:
response.setContentType("application/zip"); // zip archive format
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
.filename("download.zip", StandardCharsets.UTF_8)
.build()
.toString());
response.setContentLengthLong(totalSize);
try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
zipOutputStream.setLevel(Deflater.NO_COMPRESSION);
for (FileSystemResource file : resources) {
try (InputStream inputStream = file.getInputStream()) {
zipOutputStream.putNextEntry(new ZipEntry(file.getFilename().toString()));
StreamUtils.copy(inputStream, zipOutputStream);
zipOutputStream.flush();
}
}
}
这段代码正常工作,但是当您添加了以下行:
zipOutputStream.setLevel(Deflater.NO_COMPRESSION);
当尝试打开最终的ZIP文件时,您会收到以下错误消息:
"无法打开,错误1,进程不存在"
没有设置压缩级别的ZIP文件通常是正常的。
英文:
Is there way in Java to pack files into zip without compression? I just need to pack many files into one, but i need it fast and compression take long time. I tried something like Stored method of ZipOutputStream but the result zip is always corrupted.
Does anybody know some solution?
EDIT:
There is my code:
response.setContentType("application/zip"); // zip archive format
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
.filename("download.zip", StandardCharsets.UTF_8)
.build()
.toString());
response.setContentLengthLong(totalSize);
try(ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
zipOutputStream.setLevel(Deflater.NO_COMPRESSION);
for (FileSystemResource file : resources) {
try (InputStream inputStream = file.getInputStream()) {
zipOutputStream.putNextEntry(new ZipEntry(file.getFilename().toString()));
StreamUtils.copy(inputStream, zipOutputStream);
zipOutputStream.flush();
}
}
}
it works normal but when i added:
zipOutputStream.setLevel(Deflater.NO_COMPRESSION);
i get this error when i try to open final zip:
Could not open, ERROR 1 process doesnt exist
Zip wihtou level is always good
答案1
得分: 3
你可以使用 zipOutputStream.setLevel(Deflater.NO_COMPRESSION)
。
英文:
You can use zipOutputStream.setLevel(Deflater.NO_COMPRESSION)
.
答案2
得分: 1
问题是您正在将响应的内容长度设置为(可能是)您正在输出的文件的长度。然而,即使没有压缩,zip文件仍然包含头部(或尾部,视情况而定)。您要么必须计算完整zip文件的长度,要么使用分块编码。
英文:
The problem is that you are setting the content length of the response to (presumably) the length of the file you are outputting. however, a zip file, even with no compression, still includes a header (or footer as the case may be). you either have to compute the length of the complete zip file, or use chunked encoding.
答案3
得分: 0
ZipOutputStream的setLevel方法是你想要的。值为'0'表示没有压缩。
链接:https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipOutputStream.html#setLevel-int-
编辑:最好使用压缩器常量NO_COMPRESSION,而不是0。
链接:https://docs.oracle.com/javase/8/docs/api/java/util/zip/Deflater.html#NO_COMPRESSION
英文:
The ZipOutputStream setLevel method is what you want. A value of '0' specifies no compression.
https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipOutputStream.html#setLevel-int-
EDIT: The best form is to use the deflator constant NO_COMPRESSION instead of 0.
https://docs.oracle.com/javase/8/docs/api/java/util/zip/Deflater.html#NO_COMPRESSION
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论