如何在Java中将文件放入zip中,仅打包而不压缩它们

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月12日 22:32:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定