Click to See Complete Forum and Search --> : Download zip file from server to local machine


arpitak
11-10-2008, 12:49 AM
I have written the following code to download zip file.


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

response.setContentType("application/zip");

response.setContentType("application/x-zip-compressed");

String fileName = request.getParameter("fileName") != null ? request
.getParameter("fileName") : "";// It will return the zip filename

response.setHeader("content-disposition","attachment; filename=\""+fileName+"\"");

File f = new File(getXMLLocation
String[] filesToZip = f.list(new XMLFileNameFilter());());//it will return all the files' name to zip it.

ZipOutputStream out=null;
ServletOutputStream sos = null;
byte[] buffer = new byte[4096];
try{

sos = response.getOutputStream();

out=new ZipOutputStream(sos);
out.setLevel(Deflater.DEFAULT_COMPRESSION);
String xmlFileName = null;
FileInputStream in = null;
int len;

for (int i = 0; i < filesToZip.length; i ++)
{


xmlFileName = filesToZip[i];

ZipEntry zipEntry = new ZipEntry(xmlFileName);
out.putNextEntry(zipEntry);
System.out.println("zipEntry name:"+zipEntry.getName()+"size:"+zipEntry.getSize());
File file=new File(getXMLLocation()+xmlFileName);//this 'getXMLLocation()+xmlFileName' will return the actual file position.

in = new FileInputStream(file);

while ((len = in.read(buffer)) > 0){
System.out.println("len:"+len);
out.write(buffer, 0, len);

}

out.closeEntry();

in.close() ;

}


out.flush();



} catch(IOException ioe){
ioe.printStackTrace();

} finally {


if(out != null){
try {

out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return null;

}


When I click on a button to zip, it executed this method but the progress bar is still in process.I am not able to get the zip file.
Can anyone please help me?