[zipDirDownLoad]
๋์ ํด๋๋ฅผ ์์ถํด zip ํ์ผ๋ก ์์ฑ(compressDir, compressFile)ํ, ์์ฑํ zip ํ์ผ์ response์ ๋ด์์ client์ ๋ณด๋ธ๋ค.
package com.study.controller;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.io.File;
/**
* @param targetDirPath ์์ถํ ํด๋ ๊ฒฝ๋ก
* @param outputPath ์ถ๋ ฅํ์ผ ๊ฒฝ๋ก
* @param outputFileName ์ถ๋ ฅํ์ผ๋ช
* @description ํด๋ ์์ถ ๋ฉ์๋
*/
@SuppressWarnings("resource")
protected void zipDirDownLoad(HttpServletResponse response, String targetDirPath, String resultPath, String resultFileName) throws Exception {
// ํ์ผ๋ช
์ .zip์ด ์๋ ๊ฒฝ์ฐ, .zip ์ ๋ถ์ฌ์ค๋ค.
int pos = resultFileName.lastIndexOf(".") == -1 ? resultFileName.length() : resultFileName.lastIndexOf(".");
if (!resultZipName.substring(pos).equalsIgnoreCase(".zip")) {
resultFileName += ".zip";
}
// ์์ถ ๋์ ํ์ผ ์กด์ฌ ์ฌ๋ถ์ฒดํฌ
File targetDir = new File(targetDirPath);
if (!targetDir.exists()) {
response.getWriter().println("<script>alert('File Not Found');history.back();</script>");
logger.error("TargetDir does not exist.");
}
// ํ์ผ์ถ๋ ฅ ์คํธ๋ฆผ
FileOutputStream fos = null;
// ์์ถํ์ผ์ถ๋ ฅ ์คํธ๋ฆผ
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(new File(resultPath + resultFileName)); // ํ์ผ ๊ฐ์ฒด(new File(resultPath + resultFileName))์ ์ธ ํ์ผ์ถ๋ ฅ ์คํธ๋ฆผ
zos = new ZipOutputStream(fos); // zip output stream
// ๋๋ ํ ๋ฆฌ ๊ฒ์๋ฅผ ํตํ ํ์ ํ์ผ๊ณผ ํด๋ ๊ฒ์ ๋ฐ ์์ถ
compressDir(targetDir, targetDir.getPath(), zos);
} finally {
if (zos != null) zos.close();
if (fos != null) fos.close();
}
// ์์ถ ํ์ผ์ response๋ก ๋ณด๋ด๊ธฐ
File resultZipFile = new File(resultPath + resultFileName);
FileInputStream fis = new FileInputStream(resultZipFile);
// ํ์ผ๋ช
์ธ์ฝ๋ฉ ์ค์
String userAgent = request.getHeader("User-Agent");
if(userAgent.contains("Edge") || userAgent.contains("MSIE") || userAgent.contains("Trident")) {
resultZipName = URLEncoder.encode(resultZipName, "UTF-8").replace("\\+", "%20");
} else if(userAgent.contains("Chrome") || userAgent.contains("Opera") || userAgent.contains("Firefox")) {
resultZipName = new String(resultZipName.getBytes("UTF-8"), "ISO-8859-1");
}
byte[] byteFile = new byte[(int) resultZipFile.length()];
fis.read(byteFile); // ์ฃผ์ด์ง ๋ฐฐ์ด byteFile๋งํผ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ byteFile์ ์ ์ฅํ๊ณ ์ฝ์ ๋ฐ์ดํธ ์๋ฅผ ๋ฐํ
response.setContentType("application/zip");
response.setContentLength(byteFile.length);
response.setHeader("Content-Disposition", "attachment; filename=\""+resultFileName+"\"");
response.getOutputStream().write(byteFile); // ์ฃผ์ด์ง ๋ฐฐ์ด byteFile์ ์ ์ฅ๋ ๋ชจ๋ ๋ด์ฉ์ ์ถ๋ ฅ์์ค์ ์ด๋ค.
if(fis != null) fis.close();
if(resultZipFile.exists()) resultZipFile.delete();
}
[ compressDir ]
๋๋ ํ ๋ฆฌ๋ฅผ ํ์ผ ์ธ์๋ก ๋ฐ์์ ๋ด์ฉ ํ์ผ๋ค์ ๋๋ ํ ๋ฆฌ์ฌ๋ถ์ ๋ฐ๋ผ ๋ถ๊ธฐ ์ฒ๋ฆฌ
/**
* @param file ํ์ฌ ํ์ผ
* @param resultRootPath ๋ฃจํธ ๊ฒฝ๋ก
* @param zos ์์ถ ์คํธ๋ฆผ
* @description ๋๋ ํ ๋ฆฌ ํ์ ๋ฐ ๋ถ๊ธฐ
*/
private void compressDir(File file, String resultRootPath, ZipOutputStream zos) throws Exception {
// ์ธ์๋ก ์ฃผ์ด์ง ํ์ผ์ด ๋๋ ํ ๋ฆฌ์ธ์ง ํ์ผ์ธ์ง์ ๋ฐ๋ผ ๋ถ๊ธฐ
if (file.isDirectory()) {
// ๋๋ ํ ๋ฆฌ์ผ ๊ฒฝ์ฐ ์ฌ๊ท
File[] files = file.listFiles();
for (File f : files) {
compressDir(f, resultRootPath, zos);
}
file.delete();
} else { // ํ์ผ์ผ ๊ฒฝ์ฐ ์์ถ์ ํ๋ค.
compressFile(file, resultRootPath, zos);
}
}
[ compressFile ]
๋ถ๊ธฐ์ฒ๋ฆฌ ์ค ๋๋ ํ ๋ฆฌ๊ฐ ์๋ ํ์ผ์ผ ๊ฒฝ์ฐ ์ค์ง์ ์ผ๋ก ์์ถ ์ํ
/**
* @param file ํ์ฌ ํ์ผ
* @param resultRootPath ๋ฃจํธ ๊ฒฝ๋ก
* @param zos ์์ถ ์คํธ๋ฆผ
* @description ํ์ผ ์์ถ ๋ฉ์๋
*/
private void compressFile(File file, String resultRootPath, ZipOutputStream zos) throws Exception {
FileInputStream fis = null;
try {
String zipFileName = file.getPath().replace(resultRootPath + "\\", "");
// ํ์ผ์ ์ฝ์ด๋ค์
fis = new FileInputStream(file);
// Zip์ํธ๋ฆฌ ์์ฑ
ZipEntry zipentry = new ZipEntry(zipFileName); // zipFileName์ ์ด๋ฆ์ผ๋ก ๊ฐ์ง๋ zipEntry ์์ฑ
// ์คํธ๋ฆผ์ ๋ฐ์ด๋ฃ๊ธฐ(์๋ ์คํ)
zos.putNextEntry(zipentry); // zip entry๋ฅผ ์ฐ๊ณ , ์ํธ๋ฆฌ ๋ฐ์ดํฐ ์์์ stream์ ์์น์ํด
int length = (int) file.length(); // ํ์ผ์ ๊ธธ์ด
byte[] buffer = new byte[length]; // ํ์ผ์ ๊ธธ์ด๋งํผ์ ๋ฒํผ
fis.read(buffer, 0, length); // ์ต๋ length๊ฐ์ byte๋ฅผ ์ฝ์ด์, ๋ฐฐ์ด buffer์ ์ง์ ๋ ์์น(0)๋ถํฐ ์ ์ฅํ๊ณ ์ฝ์ ๋ฐ์ดํธ ์ ๋ฐํ
zos.write(buffer, 0, length); // ์ฃผ์ด์ง ๋ฐฐ์ด buffer์ ์ ์ฅ๋ ๋ด์ฉ ์ค์์ 0๋ฒ์งธ ๋ถํฐ length๋งํผ๋ง์ ์ฝ์ด์ ์ถ๋ ฅ์์ค์ ์ด๋ค.
zos.closeEntry(); // ํ์ฌ zip entry๋ฅผ ๋ซ๊ณ ๋ค์ ์ํธ๋ฆฌ ์์ฑ์ ์ํด ์์นํจ
} finally {
if (fis != null) fis.close();
if (file != null && file.exists())file.delete();
}
}
'Programming Languages > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ๊ธฐ๋ณธ | ๋ฐฐ์ด (3) | 2023.08.04 |
---|---|
Java ๊ธฐ๋ณธ | ํ ๋ณํ ์ ๋ฆฌ (0) | 2023.08.04 |
Java ๊ธฐ๋ณธ | ๋ณ์(Variable) (0) | 2023.08.04 |
Java | ์ ๊ทผ ์ง์ ์์ ๋ฐ๋ฅธ ๋ฉค๋ฒ ์ ๊ทผ (0) | 2023.03.21 |
Java | (์ปดํ์ผ ์ค๋ฅ) class ํด๋์ค๋ช is public, should be declared in a file named ํ์ผ๋ช .java (0) | 2023.03.16 |