[JAVA] Compress and download only the files selected in the view group with ALZip



@RequestMapping(value = "/fileAllDownload")

protected void finalfileAllDownload(HttpServletRequest request, HttpServletResponse response, HttpSession session, FileVO fileVO) throws IOException{

List<FileVO> getList = fileService.finalReportAll(fileVO);

String[] files = new String[getList.size()];

String[] filesOrg = new String[getList.size()];

Long filesActSum = new Long(0);

for(int i = 0; i<getList.size(); i++) {

files[i] = getList.get(i).getFileName1()+"."+getList.get(i).getExt();

files[i] = getList.get(i).getFileName2();

filesOrg[i] = getList.get(i).getFileName3();

filesActSum += getList.get(i).getFileSize4();

}

int actSize=Math.toIntExact(filesActSum); 

ZipOutputStream zout = null; 

String zipName = "finalReportAll.zip"; //ZIP file name

String tempPath = ""; 

if (files.length > 0) { 

try{

tempPath = "/temp/"; //ZIP file save route

tempPath = request.getSession().getServletContext().getRealPath("/") + "uploadFile/경로/";

//ZIP파일 압축 START 

//zout = new ZipOutputStream(new FileOutputStream(tempPath + zipName)); 

zout = new ZipOutputStream(new FileOutputStream(tempPath + zipName)); 

byte[] buffer = new byte[actSize]; 

FileInputStream in = null; 

for ( int k=0; k<files.length; k++){ 

in = new FileInputStream(request.getSession().getServletContext().getRealPath("/") + "uploadFile/경로/" + files[k]); //압축 대상 파일 

/*zout.putNextEntry(new ZipEntry(files[k])); //압축파일에 저장될 파일명 

*/

zout.putNextEntry(new ZipEntry(filesOrg[k])); //압축파일에 저장될 파일명

int len; while((len = in.read(buffer)) > 0){ 

zout.write(buffer, 0, len); //읽은 파일을 ZipOutputStream에 Write 

zout.closeEntry(); 

in.close(); 

zout.close(); //ZIP파일 압축 END 

//파일다운로드 START

response.setContentType("application/zip"); 

response.addHeader("Content-Disposition", "attachment;filename=" + zipName); 

FileInputStream fis = new FileInputStream(tempPath + zipName); 

BufferedInputStream bis = new BufferedInputStream(fis); 

ServletOutputStream so = response.getOutputStream(); 

BufferedOutputStream bos = new BufferedOutputStream(so); 

int n = 0; 

while((n = bis.read(buffer)) > 0){ 

bos.write(buffer, 0, n); bos.flush(); 

if(bos != null) bos.close(); 

if(bis != null) bis.close(); 

if(so != null) so.close(); 

if(fis != null) fis.close(); //파일다운로드 END 

}catch(IOException e){ //Exception 

System.out.println("fileDownload Error : " + new Date());

}finally{ 

if (zout != null){ 

zout = null; 

}

}


table data

Find the file_size of the db data and create an ALZIP file by summing it

😊
Thank you!!







Comments