Search code examples
javaziprarwinrarzipoutputstream

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


Solution

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