Mohit Verma
Mohit Verma

Reputation: 2089

Compress file using Java Zip class or bash zip command

I have a java app which generates an output file, and that needs to be compressed. Currently, I am zipping it after the file has been generated using script

$ zip <abc.zip> abc

Other option is to let Java code compress it using below classes.

java.util.zip.ZipEntry;
java.util.zip.ZipOutputStream;

Now, want to know which is better in terms of compression ratio and time taken ?

Java has advantage of being system independent, but that is ok for me.

Upvotes: 2

Views: 2024

Answers (4)

beny23
beny23

Reputation: 35068

I would think that you may get a few advantages by wrapping the output stream that writes your file with a ZipOutputStream.

Using the script there are several things that you would need to do that you can avoid by writing the zipped stream directly:

  • Write out the uncompressed file
  • Start an external process (or integrate your Java app with a script)
  • Read the uncompressed file in again
  • Compress (common to both approaches)
  • Write the compressed file (common to both approaches)

Whereas if you use the ZipOutputStream

  • Write out the result to the stream, which compresses it and writes it to the compressed file
  • No headaches with integrating your app with the zip script.
  • Do you know whether zip is available on your deployment platform, here I've got it on Linux but not on AIX)

As always, the solution is to prototype it and carry out testing and then decide which performance/compression is better/acceptable.

Upvotes: 0

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12064

I think performance will be same but using

zip -9 <abc.zip> abc

you can get more compression and you can avoid recursing directory in java by giving -r switch in zip command

Upvotes: 1

simaremare
simaremare

Reputation: 417

i think of course Java is slower, because it is running on the JVM, and not directly to the OS,. if i were you, i will keep using that script, as long as i have the time to execute it myself,. i think that is better if you automate it, if you may have no time to execute them everytime,.

Upvotes: -1

Konstantin Solomatov
Konstantin Solomatov

Reputation: 10352

Java's ZipOutputStream uses the same zlib which is used by zip command as a native library. So in terms of performance they should be equivalent.

Upvotes: 3

Related Questions