ํŒŒ์ผ๊ณผ ํด๋”๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํด๋” DIR ๊ตฌ์กฐ

[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();
                }
	}

๋ฐ˜์‘ํ˜•

+ Recent posts