Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ZipOutputStream only deflate certain files / deflate filter

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.

  1. Can this be done with ZIP files in general, or is it only a WinRAR/RAR format capability?

  2. 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

like image 782
Scott Avatar asked Jun 06 '26 10:06

Scott


2 Answers

Yes, you can do it with ZipOutputStream. Before each file (or when you want to change it) call setLevel with a constant from Deflater, in your case Deflater.NO_COMPRESSION. The documentation on this should be clearer.

You could easily make a subclass of ZipOutputStream that overrides putNextEntry to handle this logic.

like image 75
Matthew Flaschen Avatar answered Jun 08 '26 22:06

Matthew Flaschen


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).

like image 30
Thilo Avatar answered Jun 09 '26 00:06

Thilo