1

I want to use ZipOutputStream for writing big chunks of bytes what is preferred ?

FileOutputStream fos = new FileOutputStream(fileName); 

...

ZipOutputStream zos =  new ZipOutputStream(new BufferedOutputStream(fos));

Or

ZipOutputStream zos =  new ZipOutputStream(new PrintStream(fos));
Saar peer
  • 817
  • 6
  • 21

1 Answers1

1
ZipOutputStream zos =  new ZipOutputStream(new BufferedOutputStream(fos));

seems better for at least two reasons :

  • PrintStream doesn't throw IOException even if it has a error during the writting in the stream. In case of error, you could have a error in the zip content without knowing it and therefore getting a corrupted zip.

  • The writing should be more expensive for PrintStream since all characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The Javadoc advises to use the PrintWriter class in situations that require writing characters rather than bytes.

You could benchmark it to have a confirmation.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 10x !, Well i am trying to improve Jarsigner performance. I looked at the code. Any idea why they use PrintStream instaed of BufferedOutputStream ?. When I am switching to BufferedOutputStream the sign operation accelerates. – Saar peer Aug 11 '16 at 20:02
  • Excellent :) No idea too. The Java classes and tools are sometimes improvable... The proof here. Anyway, I never use Jarsigner but from we are told on the web, it is not known for being fast :) – davidxxx Aug 11 '16 at 20:11