Q:

java: Flush stream while creating zip file

I have code for create zip file in servlet like this :
ByteArrayOutputStream baos =null;
baos = new ByteArrayOutputStream(); 
ZipOutputStream zos = new ZipOutputStream(baos);
for(list  of file){
    bis = new BufferedInputStream(new FileInputStream(("somefile.extn"));

    other code for add entry in zip file

    bis.close();
}
baos.flush();
zos.flush();            
zos.close(); 
baos.close();

// Return bytes

baos.toByteArray();

// Write bytes to ServletOutputStream

Is there any problem if I flush & close ByteArrayOutputStream object (baos).
Thanks for looking here :)

A:

It is redundant. All you need is
zos.close();

Closing the ZipOutputStream flushes and closes all the other streams it is wrapped around. See the Javadoc.
You don't even need the ByteArrayOutputStream. You should connect the ZipOutputStream directly to the servlet output stream.

