import java.util.zip.ZipOutputStream;
int bufferSize = 1024 * 2;
String ouputName = "test";
ZipOutputStream zos = null;
try {
if (request.getHeader("User-Agent").indexOf("MSIE 5.5") > -1) {
response.setHeader("Content-Disposition", "filename=" + ouputName + ".zip" + ";");
} else {
response.setHeader("Content-Disposition", "attachment; filename=" + ouputName + ".zip" + ";");
}
response.setHeader("Content-Transfer-Encoding", "binary");
OutputStream os = response.getOutputStream();
zos = new ZipOutputStream(os); // ZipOutputStream
zos.setLevel(8); // 압축 레벨 - 최대 압축률은 9, 디폴트 8
BufferedInputStream bis = null;
String[] filePaths = {"filePath1","filePath2","filePath3"};
String[] fileNames = {"fileName1","fileName2","fileName3"};
int i = 0;
for(String filePath : filePaths){
File sourceFile = new File(filePath);
bis = new BufferedInputStream(new FileInputStream(sourceFile));
ZipEntry zentry = new ZipEntry(fileNames[i]);
zentry.setTime(sourceFile.lastModified());
zos.putNextEntry(zentry);
byte[] buffer = new byte[bufferSize];
int cnt = 0;
while ((cnt = bis.read(buffer, 0, bufferSize)) != -1) {
zos.write(buffer, 0, cnt);
}
zos.closeEntry();
i++;
}
zos.close();
bis.close();
} catch(Exception e){
e.printStackTrace();
}