Wednesday, August 16th, 2017

Download selected files as Zip Using JAVA

0

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* This servlet class is used to download the list files as a zip

*/

public class ZipDownload extends HttpServlet{

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

byte[] files = null;

try {

String random = this.generateRamdomCode(10, 15);

//Specify the files names seperated by ‘@!’

String reqFile = request.getParameter(“fileNames”);

if(reqFile != null && reqFile.trim().length()>0){

String filenames[] = reqFile.split(“@!”);

ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(random + “.zip”));

for (String filename : filenames) {

try{

filename = filename.trim();

if(filename.length()>0){

FileInputStream fin = new FileInputStream(filename);

File file = new File(filename);

zipFile.putNextEntry(new ZipEntry(filename.substring(filename

.lastIndexOf(“/”) + 1, filename.length())));

files = new byte[(int) file.length()];

fin.read(files);

zipFile.write(files);

fin.close();

zipFile.closeEntry();

}

}

catch (Exception e) {

e.printStackTrace();

}

}

zipFile.close();

FileInputStream zip = new FileInputStream(random + “.zip”);

File f = new File(random + “.zip”);

files = new byte[(int) f.length()];

zip.read(files);

zip.close();

f.delete();

//response

response.setContentType(“application/zip”);

response.setContentLength(files.length);

response.setHeader(“Content-Disposition”,

“attachment; filename=\”filename.zip\”");

try {

response.getOutputStream().write(files);

} catch (Exception e) {

e.printStackTrace();

}

}

}

catch (Exception e) {

e.printStackTrace();

}

finally {

}

}

private String generateRamdomCode(double min, double max){

String code = “”;

int totalChars = (int)((Math.random() * min)+max);

char startChar = ‘A’;

for(int i=0;i<totalChars;i++){

int selNum = (int)(Math.random() * 13.0);

if(selNum <= 4){

startChar = ‘A’;

code += (char)((Math.random()*26.0)+startChar);

}else if(selNum >=5 && selNum<= 9){

code += (int)(Math.random() * 10.0);

}else if(selNum >=10){

startChar = ‘a’;

code += (char)((Math.random()*26.0)+startChar);

}

}

System.out.println(code);

return code;

}

}

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • RSS
  • Technorati
  • Twitter

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Secure Linux Web Hosting