2017년 4월 18일 화요일

Java 압축파일 zip 다운로드


압축파일(zip)을 만든 후 다운로드하는 소스이다.

  • zip 파일을 생성한다.
ZipOutputStream zout = null; 
String zipName = "C:/test.zip"; 
try{ 
  zout = new ZipOutputStream(new FileOutputStream(zipName)); 
  byte[] buf = new byte[1024]; 
  FileInputStream in = new FileInputStream("C:/test.txt");//압축대상 파일 
  zout.putNextEntry( new ZipEntry("test.txt") ); 
  int len; 
  while( (len = in.read(buf)) > 0 ){ 
    zout.write(buf, 0, len); 
  } 
  zout.closeEntry(); 
}catch(Exception e){ 
e.printStackTrace(); 
}finally{ 
if( null != zout ){ 
try { 
      zout.close(); 
}catch(Exception e){ 
zout = null; 
}


  • 생성 된 zip파일을 읽어 다운로드 받을 수 있게 처리한다.
File zipFile = new File(zipName); 
res.setContentType("application/x-zip-compressed;"); 
res.setHeader("Content-Disposition", "attachment; filename=\"" + zipFile.getName() + "\";"); 
OutputStream out = null; 
InputStream in = null; 
try{ 
  out = res.getOutputStream(); 
  in = new FileInputStream(zipFile); 
byte[] buf = new byte[1024]; 
  int len; 
  while( (len = in.read(buf)) > 0 ){ 
  out.write(buf, 0, len); 
  }
}catch(Exception e){ 
  e.printStackTrace(); 
}finally{ 
  if (out != null) { 
    try { out.close(); } catch (Exception e) {} out = null; 
  } 
  if (in != null) { 
    try { in.close(); } catch (Exception e) {} in = null; 
  } 
}

댓글 없음:

댓글 쓰기

🧠💥 이탈리안 브레인롯(Italian Brainrot): 인터넷 밈의 신세계 🇮🇹

요즘 SNS에서 유행하는 이상한 말투, 이탈리아 억양, 그리고 피자 이모지 🤌🍕. 이게 다 뭔지 궁금하셨다면, 바로 이 ‘이탈리안 브레인롯(Italian Brainrot)’ 때문입니다! 중독성 있는 이 밈, 지금부터 쉽고 재밌게 알아볼게요. ...