반응형
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import com.google.common.collect.Lists; public class ZipUtil { public static void zip(String zipFileName, List<File> files) throws IOException { byte[] buf = new byte[1024]; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); try { for (int i = 0; i < files.size(); i++) { FileInputStream in = new FileInputStream(files.get(i)); try { String fileName = files.get(i).getAbsolutePath(); ZipEntry ze = new ZipEntry(fileName); out.putNextEntry(ze); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); } finally { in.close(); } } } finally { if (out != null) { out.close(); } } } public static List<String> unzip(String zipFilePath, String destDir) throws IOException { File dir = new File(destDir); if (!dir.exists()) { dir.mkdirs(); } byte[] buffer = new byte[1024]; FileInputStream fis = null; ZipInputStream zis = null; ZipEntry ze = null; List<String> fileList = Lists.newArrayList(); try { fis = new FileInputStream(zipFilePath); zis = new ZipInputStream(fis); ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(destDir + File.separator + fileName); fileList.add(fileName); if (ze.isDirectory()) { newFile.mkdirs(); } else { FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } zis.closeEntry(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } finally { if (fis != null) { fis.close(); } } return fileList; } } |
반응형
'java' 카테고리의 다른 글
spring boot favicon.ico (0) | 2019.03.09 |
---|---|
java.lang.IllegalStateException: No Java compiler available for configuration options compilerClassName: [null] and compiler: [null] (0) | 2019.03.09 |
springboot mybatis-config.xml to Java Config (0) | 2019.03.08 |
java - HttpServletRequest method 정리 (0) | 2019.02.26 |
java - spring boot - multipart max file size (0) | 2019.02.18 |
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field (0) | 2019.02.11 |
Apache POI 3.14 Exception - The maximum column width for an individual cell is 255 characters. (0) | 2019.02.08 |
java memory check - 자바 메모리 확인 (0) | 2019.01.23 |