자바를 이용한 zip 압축과 압축해제 예제 입니다.
[한글.zip ]에 약간의 문제가 발생 할 수 있을 것 같습니다.

================================================================================================
import java.io.*;
import java.util.zip.*;

class FillHand {
    static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in)); //입력
    static PrintWriter screen=new PrintWriter(System.out, true);  //표시

    public static void main(String[] args) throws IOException //결정부분. 예외는 vm으로.
    {
        int choice_main;
        int choice_zip;
        String from;
        String to;
        String fromhuge;       
        String tozip;        
        String fromzipp;        
        String tohuuge;
        screen.println(“입력선택을하시오”);        
        screen.println(“1번은 파일을 압축합니다.”);        
        screen.println(“2번은 파일을 복사합니다.”);        
        choice_main=new Integer(keyboard.readLine()).intValue(); //작업선택

        switch(choice_main) //1번시의 작업선택
        {
            case 1:                    
                screen.println(“3번을 선택하면 압축합니다.4번은 압축을 풀어줍니다.”);                    
                choice_zip = new Integer(keyboard.readLine()).intValue();                    
                /*zipping()압축 파일의 처러, 키보드로부터 원본 파일과 압축된 결과 파일의 이름을 입력                 
                받고, ZipRusty 클래스 객체를 선언, 이 클래스의 매써드zipping()을 이용해서 압축 수행                   
                */                    
                switch (choice_zip)                    
                {                    
                    case 3:                        
                        screen.println(“++++파일명을 써주시고 엔터를 눌러주시오++++”);                            
                        fromhuge=keyboard.readLine();                            
                        screen.println(“새로 압축해서 만들 파일명을 써주십시오. 확장자포함. –상태표시**”);
                        tozip=keyboard.readLine();
                        ZipRusty zipprusty=new ZipRusty();
                        zipprusty.zipping(fromhuge, tozip);
                        break;
                        
                    case 4:
                        screen.println(“++압축 풀 파일 ++++”);
                        fromzipp=keyboard.readLine();
                        screen.println(“**압축 풀 파일***”);
                        tohuuge=keyboard.readLine();
                        UnZipRusty unzipprusty=new UnZipRusty();
                        unzipprusty.unzipping(fromzipp, tohuuge);
                        screen.println();
                        break;
                    
                } //switch (choice_zip)의 끝나는 부분
            case 2: //복사
                 screen.println(“+++복사할 파일+++”);
                 from= keyboard.readLine();
                 screen.println(“파일 명”);        
                 to= keyboard.readLine();        
                 Duplicate duplo=new Duplicate();        
                 duplo.duplicate(from,to);        
                 break;
        }// 메인 swithc 끝
    }//main 끝
}//class 닫음

class Duplicate //두개의 파일을 복사하느 기능
{
    public static void duplicate(String from, String to) throws IOException
    {
        FileInputStream in = new FileInputStream(from);
        FileOutputStream out= new FileOutputStream(to);
        byte[] buffer=new byte[6453];
        int read_bytes;
       
        while ((read_bytes=in.read(buffer))!=-1)
            out.write(buffer, 0, read_bytes);
        //원본 파일에서 읽어서 대상을 파일로 기록한다.
       
        in.close();
        out.close();
        FileInputStream sizeIn=new FileInputStream(from);
        int total=0;
                
        while (sizeIn.read() != -1)
            total++;
        //원본 파일의 용량을 계산한다.
        
        in.close();
        System.out.println(“복사 “+from+”—–“+to+”성공했습니다.”);
        System.out.println(“파일의 크기”+total+”bytes”);
    }
}

class ZipRusty
{
    public static void zipping(String fromhuge, String tozip) throws IOException
    {
        FileInputStream in=new FileInputStream(fromhuge);        
        GZIPOutputStream out =new GZIPOutputStream(new FileOutputStream(tozip));
        byte[] buffer =new byte[4096];        
        int bytes_read;        
        int totalhuge=0;        
        int totalgzip=0;      
       
        while ((bytes_read=in.read(buffer)) !=-1)        
             out.write(buffer,0,bytes_read);
       
        in.close();        
        out.close();        
        in=new FileInputStream(fromhuge);        
        FileInputStream sizeIn =new FileInputStream(tozip);

        while (in.read() !=-1)
             totalhuge++;
        
        while (sizeIn.read() !=-1)
             totalgzip++;
        
        in.close();        
        out.close();        
        System.out.println(“파일”+fromhuge+”성공했습니다.^^”+tozip);        
        System.out.println(totalhuge+”bytes=원래사이즈——“+”         “+totalgzip+”bytes=새 사이즈”);
    }// zipping() 닫음.
}// class 닫음

class UnZipRusty
{
    public static void unzipping(String fromzipp, String tohuuge)throws IOException
    {
        GZIPInputStream in =new GZIPInputStream(new FileInputStream(fromzipp));
        FileOutputStream out= new FileOutputStream(tohuuge);
        byte[]  buffer =new byte[4096];
        int bytes_read;

        while((bytes_read=in.read(buffer)) !=-1)
                out.write(buffer, 0,bytes_read);

        in.close();
        out.close();
        System.out.println(“파일”+fromzipp+”성공된 파일”+tohuuge);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *