98 lines
3.7 KiB
Java
98 lines
3.7 KiB
Java
package com.interplug.qcast.util;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.nio.file.Path;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import com.interplug.qcast.config.message.Messages;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class ZipFileManager {
|
|
|
|
@Autowired
|
|
Messages message;
|
|
|
|
/**
|
|
* zip file을 생성하여 ZipOutputStream를 반환하는 method
|
|
*
|
|
* @param response HttpServletResponse
|
|
* @param strCreateFileName 생성할 file 이름
|
|
* @param listFilePath 파일 정보 목록
|
|
*/
|
|
public void createZipFile(HttpServletResponse response, String strCreateFileName,
|
|
List<Map<String, String>> listFile) throws Exception {
|
|
// 압축될 파일명이 존재하지 않을 경우
|
|
if (strCreateFileName == null || "".equals(strCreateFileName))
|
|
throw new IllegalArgumentException(
|
|
message.getMessage("common.message.data.no.exists", "file name"));
|
|
|
|
// 파일이 존재하지 않을 경우
|
|
if (listFile == null || listFile.size() == 0)
|
|
throw new IllegalArgumentException(message.getMessage("common.message.no.dataDown"));
|
|
|
|
// zip 파일명
|
|
String strZipName = strCreateFileName + ".zip";
|
|
|
|
// response 설정
|
|
response.setContentType("application/zip");
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + strZipName + ";");
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
|
|
for (Map<String, String> mapFile : listFile) {
|
|
String strDirectory = mapFile.get("directory");
|
|
String strSourceFile = mapFile.get("filename");
|
|
Path path = Path.of(strSourceFile);
|
|
|
|
try (FileInputStream fis = new FileInputStream(path.toFile())) {
|
|
// 압축될 파일명을 ZipEntry에 담아준다
|
|
ZipEntry zipEntry = new ZipEntry(strDirectory + "\\" + path.getFileName().toString());
|
|
|
|
// 압축될 파일명을 ZipOutputStream 에 담아준다
|
|
zos.putNextEntry(zipEntry);
|
|
byte[] buffer = new byte[1024];
|
|
int length;
|
|
|
|
while ((length = fis.read(buffer)) >= 0) {
|
|
zos.write(buffer, 0, length);
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
// response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
// throw new IllegalArgumentException("파일 변환 작업중, [ " + strSourceFile + " ] 파일을 찾을 수
|
|
// 없습니다.");
|
|
log.debug("파일 변환 작업중, [ " + strSourceFile + " ] 파일을 찾을 수 없습니다.");
|
|
} catch (IOException e) {
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
throw new IllegalArgumentException(
|
|
"파일 변환 작업중, [ " + strSourceFile + " ] 파일을 다운로드 할 수 없습니다.");
|
|
} finally {
|
|
// ZipOutputStream 에 담아둔 압축될 파일명을 flush 시켜준다
|
|
zos.flush();
|
|
// ZipOutputStream 에 담아둔 압축될 파일명을 close 시켜준다
|
|
zos.closeEntry();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
// response 에 담아둔 파일을 flush 시켜준다
|
|
response.flushBuffer();
|
|
} catch (IOException e) {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|