From abce3b3936d23e1ba524a59a1feede89b8e2e175 Mon Sep 17 00:00:00 2001 From: leebj Date: Tue, 19 Nov 2024 15:37:07 +0900 Subject: [PATCH] no message --- pom.xml | 6 - src/main/java/cokr/xit/ZIP.java | 120 ++++++++++++++++++ .../service/bean/WindowsServiceBean.java | 2 +- .../nuri2/web/nuri2Controller.java | 14 +- 4 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cokr/xit/ZIP.java diff --git a/pom.xml b/pom.xml index c29c8b11..dd2126b6 100644 --- a/pom.xml +++ b/pom.xml @@ -55,12 +55,6 @@ - - cokr.xit.base - xit-file - 23.04.01-SNAPSHOT - - org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/cokr/xit/ZIP.java b/src/main/java/cokr/xit/ZIP.java new file mode 100644 index 00000000..49e0a0ae --- /dev/null +++ b/src/main/java/cokr/xit/ZIP.java @@ -0,0 +1,120 @@ +package cokr.xit; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.Arrays; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import cokr.xit.foundation.Assert; + +public class ZIP { + /**지정한 파일들을 지정한 경로의 zip 파일로 압축한다. + * @param zipPath zip 파일 경로 + * @param targets 압축할 파일들의 경로 + * @return 압축한 zip 파일 + */ + public File compress(String zipPath, String... targets) { + return compress(zipPath, Arrays.asList(targets)); + } + + /**지정한 파일들을 지정한 경로의 zip 파일로 압축한다. + * @param zipPath zip 파일 경로 + * @param targets 압축할 파일들의 경로 + * @return 압축한 zip 파일 + */ + public File compress(String zipPath, List targets) { + try ( + FileOutputStream fout = new FileOutputStream(zipPath); + ZipOutputStream zout = new ZipOutputStream(fout); + ) { + for (String path: targets) { + File file = new File(path); + compress(zout, file, file.getName()); + } + + return new File(zipPath); + } catch(Exception e) { + throw Assert.runtimeException(e); + } + } + + private void compress(ZipOutputStream zout, File file, String filename) throws Exception { + if (file.isHidden()) return; + + if (file.isDirectory()) { + zout.putNextEntry(new ZipEntry(filename.endsWith("/") ? filename : filename + "/")); + zout.closeEntry(); + + File[] children = file.listFiles(); + for (File child: children) { + compress(zout, child, filename + "/" + child.getName()); + } + } else + try (FileInputStream fis = new FileInputStream(file);) { + zout.putNextEntry(new ZipEntry(filename)); + byte[] bytes = new byte[1024]; + int length; + while((length = fis.read(bytes)) >= 0) { + zout.write(bytes, 0, length); + } + } + } + + /**지정한 zip 파일을 지정한 경로의 디렉토리에 압축을 해제한다. + * @param zipPath zip 파일 경로 + * @param destDir 압축을 해제한 파일들을 저장할 디렉토리 경로 + */ + public void decompress(String zipPath, String destDir) { + try ( + FileInputStream fis = new FileInputStream(zipPath); + ZipInputStream zis = new ZipInputStream(fis); + ) { + decompress(zis, destDir); + } catch (Exception e) { + throw Assert.runtimeException(e); + } + } + + private void decompress(ZipInputStream zis, String destDir) throws Exception { + byte[] buff = new byte[1024]; + File dir = new File(destDir); + + ZipEntry zipEntry = zis.getNextEntry(); + while (zipEntry != null) { + File newFile = newFile(dir, zipEntry); + + if (zipEntry.isDirectory()) { + if (!newFile.isDirectory() && !newFile.mkdirs()) + throw new Exception("Failed to create directory " + newFile); + } else { + File parent = newFile.getParentFile(); + if (!parent.isDirectory() && !parent.mkdirs()) + throw new Exception("Failed to create directory " + parent); + + try ( + FileOutputStream fos = new FileOutputStream(newFile); + ) { + int len; + while ((len = zis.read(buff)) > 0) { + fos.write(buff, 0, len); + } + } + } + zipEntry = zis.getNextEntry(); + } + } + + private File newFile(File destDir, ZipEntry zipEntry) throws Exception { + File destFile = new File(destDir, zipEntry.getName()); + + String destDirPath = destDir.getCanonicalPath(); + if (!destFile.getCanonicalPath().startsWith(destDirPath + File.separator)) + throw new Exception(String.format("%s is outside of the %s", zipEntry.getName(), destDirPath)); + + return destFile; + } +} \ No newline at end of file diff --git a/src/main/java/externalsystem/nuri2/service/bean/WindowsServiceBean.java b/src/main/java/externalsystem/nuri2/service/bean/WindowsServiceBean.java index 6176b74e..4f8b83e1 100644 --- a/src/main/java/externalsystem/nuri2/service/bean/WindowsServiceBean.java +++ b/src/main/java/externalsystem/nuri2/service/bean/WindowsServiceBean.java @@ -5,7 +5,7 @@ import java.io.File; import javax.servlet.http.HttpServletRequest; import cokr.xit.TextFileMaker; -import cokr.xit.base.file.ZIP; +import cokr.xit.ZIP; public class WindowsServiceBean extends Nuri2ServiceBean { diff --git a/src/main/java/externalsystem/nuri2/web/nuri2Controller.java b/src/main/java/externalsystem/nuri2/web/nuri2Controller.java index 072beb29..7f307543 100644 --- a/src/main/java/externalsystem/nuri2/web/nuri2Controller.java +++ b/src/main/java/externalsystem/nuri2/web/nuri2Controller.java @@ -6,7 +6,6 @@ import java.io.OutputStream; import java.nio.file.Files; import java.util.function.Consumer; -import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; @@ -22,8 +21,6 @@ import externalsystem.nuri2.service.bean.WindowsServiceBean; @RequestMapping(name="모바일 메시지 컨트롤러", value="/mms") public class nuri2Controller extends AbstractController { - @Resource(name="nuri2ServiceBean") - Nuri2ServiceBean nuri2ServiceBean; @RequestMapping(name="모듈 실행 스크립트 파일 생성", value="/createScript.do") @@ -83,7 +80,14 @@ public class nuri2Controller extends AbstractController { public ModelAndView createConf(HttpServletRequest req) { ModelAndView mav = new ModelAndView("downloadView"); - String filePath = nuri2ServiceBean.makeConfFile(req); + Nuri2ServiceBean nuri2Service = new Nuri2ServiceBean() { + @Override + public String makeScriptFile(HttpServletRequest req) { + return null; + } + }; + + String filePath = nuri2Service.makeConfFile(req); //다운로드 File file = new File(filePath); @@ -106,7 +110,7 @@ public class nuri2Controller extends AbstractController { new Downloadable() .setContentType("text/plain") .setWriter(writer) - .setFilename(nuri2ServiceBean.nuri2confName) + .setFilename(nuri2Service.nuri2confName) ); } catch (IOException e) { e.printStackTrace();