2010-02-16 2 views
6

내 응용 프로그램이 Java를 동적 방법으로 사용하여 결과 파일 (파일 그룹)을 압축하려면 Java에서 사용할 수있는 옵션은 무엇입니까? 내가 브라우징 할 때 java.util.zip 패키지를 사용하고 있지만 구현에 사용할 수있는 다른 방법이 있습니까?Java를 사용하여 폴더와 파일 압축 및 압축 해제

+0

같은 것을 사용할 수 있습니다 http://devharbor.blogspot.com/2009/01/create-zip -archive-in-java.html 및 google – ant

+0

코드에 감사드립니다. – gmhk

답변

0

JDK 과 함께 제공되는 ZIP 파일 처리 라이브러리를 사용할 수도 있습니다. this 자습서가 도움이 될 수 있습니다.

15
public class FolderZiper { 
    public static void main(String[] a) throws Exception { 
    zipFolder("c:\\a", "c:\\a.zip"); 
    } 

    static public void zipFolder(String srcFolder, String destZipFile) throws Exception { 
    ZipOutputStream zip = null; 
    FileOutputStream fileWriter = null; 

    fileWriter = new FileOutputStream(destZipFile); 
    zip = new ZipOutputStream(fileWriter); 

    addFolderToZip("", srcFolder, zip); 
    zip.flush(); 
    zip.close(); 
    } 

    static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) 
     throws Exception { 

    File folder = new File(srcFile); 
    if (folder.isDirectory()) { 
     addFolderToZip(path, srcFile, zip); 
    } else { 
     byte[] buf = new byte[1024]; 
     int len; 
     FileInputStream in = new FileInputStream(srcFile); 
     zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); 
     while ((len = in.read(buf)) > 0) { 
     zip.write(buf, 0, len); 
     } 
    } 
    } 

    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) 
     throws Exception { 
    File folder = new File(srcFolder); 

    for (String fileName : folder.list()) { 
     if (path.equals("")) { 
     addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); 
     } else { 
     addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); 
     } 
    } 
    } 
} 
3

원본 Java 구현에는 파일 인코딩과 관련된 몇 가지 버그가있는 것으로 알려져 있습니다. 예를 들어 움라우트가있는 파일 이름을 제대로 처리 할 수 ​​없습니다.

TrueZIP는 프로젝트에서 사용한 대체 방법입니다. https://truezip.dev.java.net/ 사이트의 문서를 확인하십시오.

0

Java는 java.util.zip.ZipInputStream 클래스가이와 함께 당신이 ZipEntry를 ...

public static void unZipIt(String zipFile, String outputFolder){ 
File folder = new File(zipFile); 
    List<String> files = listFilesForFolder(folder); 
    System.out.println("Size " + files.size()); 
    byte[] buffer = new byte[1024]; 
    try{ 
    Iterator<String> iter = files.iterator(); 
    while(iter.hasNext()){ 
     String file = iter.next(); 
    System.out.println("file name " + file);  
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file)); 
    ZipEntry ze = zis.getNextEntry(); 
    while(ze!=null){ 
      String fileName = ze.getName(); 
      File newFile = new File(outputFolder + File.separator + fileName); 
      System.out.println("file unzip : "+ newFile.getAbsoluteFile()); 
      new File(newFile.getParent()).mkdirs(); 
      FileOutputStream fos = new FileOutputStream(newFile);    
      int len; 
      while ((len = zis.read(buffer)) > 0) { 
      fos.write(buffer, 0, len); 
      } 
      fos.close(); 
      ze = zis.getNextEntry(); 
    } 
    zis.closeEntry(); 
    zis.close(); 
    System.out.println("Done"); 
    } 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    }