如何在Java中创建未压缩的Zip存档 [英] How to create Uncompressed Zip archive in Java

查看:256
本文介绍了如何在Java中创建未压缩的Zip存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java的Zip实用程序包,想知道如何创建一个完全不压缩的zip文件.将级别设置为0并没有帮助.是这样吗?

I am using the Zip utility package of Java and wanted to know how to create a zip file with no compression at all. Setting the level to 0 doesn't help. Is this right?

此外,当我使用 STORED 方法时,它会引发以下异常:

Also, when I used the STORED method, it throws following exception:

java.util.zip.ZipException: STORED entry missing size, compressed size, or crc-32

我可以设置大小,但是现在引发以下异常:

I can set the size but now following exception is thrown:

java.util.zip.ZipException: invalid entry crc-32 

我只是在跟踪通过网络进行搜索的所有示例,我想我并不能真正正确地理解它.如果有人可以在此方面帮助我,并提供建议以纠正我可能正在做的问题,那将是很好的.

I am just following all the examples available by searching on the web and I am not able to really understand it properly I guess. It would be great if someone can help me on this and provide me suggestion to correct the problem I might be doing.

推荐答案

我对 aperkins 解决方案(已删除)持谨慎态度,但我知道它为什么有效.该行(此后已在他的答案中得到纠正)

I'm leery of aperkins solution (since deleted), but I know why it worked. The line (which has since been corrected in his answer)

zipOut.setLevel(ZipOutputStream.STORED); // accidentally right

使用的静态值 ZipOutputStream.STORED ,恰好等于 0 .因此,该行正在执行的操作是将默认DEFLATED方法使用的 level 设置为零压缩(这显然是您想要做的,但碰巧只能靠运气).因此,要明确,安全地获取您想要的东西,请改用此:

was using the static value ZipOutputStream.STORED, which coincidentally equals 0. So what that line is doing is setting the level used by the default DEFLATED method to zero compression (this is obviously what you want to do, but happened to only work by luck). So to get what you want explicitly and safely, use this instead:

zipOut.setMethod(ZipOutputStream.DEFLATED); // this line optional
zipOut.setLevel(0);

zipOut.setLevel(Deflater.NO_COMPRESSION);

如果您使用

zipOut.setMethod(ZipOutputStream.STORED);
zipOut.setLevel(Deflater.NO_COMPRESSION);

您可能会得到Keya在原始问题中提到的异常.我相信 Christian Schlichtherle 是对的;因为没有在条目中设置CRC,所以您将获得Exceptions.其结果是要使用STORED方法,您必须先读取整个条目文件,或者在调用 zipOut之前找到其他设置大小,压缩大小(必须相等)和CRC的方法.putNextEntry().否则,如果通过向输出流写入太多字节而超出了size属性,则会遇到更多的异常.ZIP规范似乎表明,如果您要写入STORED数据,则必须在数据本身之前在前面"写头文件(其中包括CRC-32和长度),因此需要在其之前设置Java API可以启动,因为它基本上仅支持流式传输到最终的zip文件.

you'll probably get the Exception that Keya noted in the original question. I believe Christian Schlichtherle is right; you are getting the Exceptions because you are not setting the CRC in the entry. The repercussions of that is that to use the STORED method, you have to read the entire entry file first, or find some other way to set the size, compressed size (must be equal) and the CRC before you call zipOut.putNextEntry(). Otherwise, you'll run into more exceptions if you overrun the size attribute by writing too many bytes to the output stream. It appears that the ZIP specs say that if you are writing STORED data then it has to write the header [which includes the CRC-32 and length] "up front" before the data itself, hence the java API requiring these be set before it can start, since it basically only supports streaming out to the final zip file.

这篇关于如何在Java中创建未压缩的Zip存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆