Posted
Filed under JSP, JAVA
[출처] : http://suya55.tistory.com/230

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class ZipFileUtil {

    public static int unZip(String orgFileName, String orgDirName) {
        return ZipFileUtil.unZip(new File(orgDirName + File.separator + orgFileName));
    }


    @SuppressWarnings("unchecked")
    public static int unZip(File fileName) {
        Enumeration entries;
        ZipFile zipFile = null;

        try {
            zipFile = new ZipFile(fileName,"EUC-KR");   // 요거 한방이면 끝인데.ㅠㅠ;

            entries = zipFile.getEntries();

            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String orgDirName = fileName.getParent() + File.separator;
                String entryFileName = entry.getName();
                if (entry.isDirectory()) {
                    System.err.println("Extracting directory: " + entryFileName);
                    (new File(orgDirName + entryFileName)).mkdir();
                    continue;
                } else {
                    String[] tmpSplit = entryFileName.split(File.separator);
                    if (tmpSplit.length > 1) {
                        String tmpDir = "";
                        for (int i = 0; i < tmpSplit.length - 1; i++)
                            tmpDir += (tmpSplit[i] + File.separator);
                        tmpDir = orgDirName + tmpDir;
                        File tmpFile = new File(tmpDir);
                        if (!tmpFile.exists())
                            tmpFile.mkdir();
                    }
                }
                System.out.println("Extracting File: " + entryFileName);

                FileUtil.copyInputStream(zipFile.getInputStream(entry),
                        new BufferedOutputStream(new FileOutputStream(
                                orgDirName + entryFileName)));
            }

        } catch (ZipException ze) {
            ze.printStackTrace();
            return 0;
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return 0;
        } finally {
            try {
                if (zipFile != null)
                    zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }

}


import java.io.*;
public class FileUtil {
   
     /**
     * 파일을 stream으로 읽어들여 대상 outputStream에 복사하는 메소드
     * @param in
     * @param out
     * @throws IOException
     */
    public static final void copyInputStream(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }
}
2009/12/08 15:32 2009/12/08 15:32