2009-11-10 6 views
1

나는 압축 파일이 있습니다. 이 파일에는 다양한 디렉토리와 파일도 들어 있습니다. 모든 것을 추출하여 지정된 경로에 저장하고 싶습니다.자바를 사용하여 zip 파일을 추출하는 가장 좋은 방법은 무엇입니까

그래서 압축 된 파일을 추출하는 Java 프로그램을 작성하는 방법.

감사 선일 쿠마르 Sahoo

+0

가능한 중복 [반복적으로 자바에서 파일을 압축 해제하는 방법?] (http://stackoverflow.com/questions/981578/how-to-unzip-files-recursively-in -java) – Kevin

답변

1
public static void unzip(){ 
     try{ 
      BufferedOutputStream out = null; 
      ZipInputStream in = new ZipInputStream(ZipFileExtracter.class.getClassLoader().getResourceAsStream("com/artificialmachines/chitme/stamps/ChitMeData.zip")); 
      ZipEntry entry; 
      boolean isDirectory=false; 
      while((entry = in.getNextEntry()) != null){ 
       int count; 
       byte data[] = new byte[BUFFER]; 
       // write the files to the disk 
       String entryName = entry.getName(); 
       File newFile = new File(new StringBuffer().append(System.getProperty("user.dir")).append(File.separator).append(entryName).toString()); 
       if(entryName.endsWith("/")){ 
        isDirectory=true; 
        newFile.mkdir(); 
        //System.out.println("This is directory "+newFile.exists()+" IS DIr "+newFile.isDirectory()+" path "+newFile.getPath()); 
       }else{ 
        newFile.createNewFile(); 
       } 
       if(!isDirectory){ 
        out = new BufferedOutputStream(new FileOutputStream(newFile),BUFFER); 
        while ((count = in.read(data,0,BUFFER)) != -1){ 
         out.write(data,0,count); 
        } 
        cleanUp(out); 
       } 
       isDirectory=false; 
      } 
      cleanUp(in); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 
1

java.util.zip. *은 필수 항목입니다. refernec 용 API를 살펴보십시오. 당신은 예를 찾고 있다면 LookThis

관련 문제