Java ZipOutputStream 仅压缩某些文件/压缩过滤器
这确实是一个由两部分组成的问题。
前言:我使用WinRAR来压缩文件。它使您可以选择仅压缩某些文件。我可以按文件扩展名进行过滤,这样 JPEG 文件就不会被压缩,而其他文件则会被压缩。
这可以用一般的 ZIP 文件来完成吗?还是只是 WinRAR/RAR 格式的功能?
如果可以使用 ZIP 格式,有没有办法使用 Java 的 ZipOutputStream 类?或者,也许使用其他一些 ZIP Java 实现?
为了澄清,我想告诉我的 ZipOutputStream 仅压缩具有特定扩展名的文件。这可能吗?
谢谢
This is a two part question, really.
Preface: I use WinRAR to compress files. It gives you the option of only compressing certain files. I can filter by file extension so that, say, JPEG files are not compressed, while other files are.
Can this be done with ZIP files in general, or is it only a WinRAR/RAR format capability?
If it is possible do to with the ZIP format, is there a way I can do that using Java's ZipOutputStream class? Or, perhaps using some other ZIP Java implementation?
To clarify, I would like to tell my ZipOutputStream to only compress files with a particular extension. Is this possible?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以使用 ZipOutputStream 来实现。在每个文件之前(或者当您想要更改它时)调用
setLevel
带有来自Deflater
,在您的情况下Deflater.NO_COMPRESSION
。这方面的文档应该更清楚。您可以轻松创建 ZipOutputStream 的子类来重写 putNextEntry 来处理此逻辑。
Yes, you can do it with
ZipOutputStream
. Before each file (or when you want to change it) callsetLevel
with a constant fromDeflater
, in your caseDeflater.NO_COMPRESSION
. The documentation on this should be clearer.You could easily make a subclass of
ZipOutputStream
that overridesputNextEntry
to handle this logic.ZipOutputStream 有方法控制后续条目是否 要放气,并且使用哪个压缩级别。
也可以指定在 ZipEntry 级别(优先)。
ZipOutputStream has methods to control if subsequent entries are to be deflated, and with which compression level.
This can also be specified on the ZipEntry level (where it takes precedence).