2014-04-27 3 views
4

저는 Java 프로그래밍에 초보자이며 현재 .zip 파일을 압축 및 압축 해제 할 수 있어야하는 응용 프로그램을 작성 중입니다. 내가 사용하여 Java에서 ZipFile에 압축을 해제하려면 다음 코드를 사용하여 내장 된 자바 우편 기능뿐만 아니라 아파치 코 몬즈 IO 라이브러리 :디렉토리를 Commons IO가있는 zip 파일로 압축하십시오.

public static void decompressZipfile(String file, String outputDir) throws IOException { 
    if (!new File(outputDir).exists()) { 
     new File(outputDir).mkdirs(); 
    } 
    ZipFile zipFile = new ZipFile(file); 
    Enumeration<? extends ZipEntry> entries = zipFile.entries(); 
    while (entries.hasMoreElements()) { 
     ZipEntry entry = entries.nextElement(); 
     File entryDestination = new File(outputDir, entry.getName()); 
     if (entry.isDirectory()) { 
      entryDestination.mkdirs(); 
     } else { 
      InputStream in = zipFile.getInputStream(entry); 
      OutputStream out = new FileOutputStream(entryDestination); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
      IOUtils.closeQuietly(out); 
     } 
    } 
} 

가 어떻게 외부를 사용하지 디렉토리에서 ZipFile를 만들기에 대한 갈 것이라고 이미 사용하고있는 라이브러리가 아닌 다른 라이브러리? (자바 표준 라이브러리 및 가공 IO)

+2

zip 파트가 java.util.Zip에 의해 수행되고있는 경우 commons-IO는 파일을 닫기위한 유틸리티를 제공하고 있습니다. 위와 같은 해결책을 찾고 있습니까? – AdityaKeyal

+0

예, 제공된 Java 라이브러리 및/또는 Commons-IO 만 사용하고 다른 외부 종속성은 사용하지 않는 솔루션을 찾고 있습니다. 나는 이것에 대해보다 명확하게하기 위해 질문 문안을 편집했다. 나는이 코드가 "zipfile을 압축 해제하는 Commons-IO 방법"이라고 표현한 또 다른 SE 질문에서 나온 것처럼 오히려 새롭다. 나는 그 기능이 Commons-IO에 의해 제공되었다고 잘못 생각했다. – StackUnderflow

답변

7

다음 방법 (들)을 성공적으로 재귀 적으로 디렉토리를 압축하는 것 :

내 압축 코드에서 볼 수 있듯이
public static void compressZipfile(String sourceDir, String outputFile) throws IOException, FileNotFoundException { 
    ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(outputFile)); 
    compressDirectoryToZipfile(sourceDir, sourceDir, zipFile); 
    IOUtils.closeQuietly(zipFile); 
} 

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException, FileNotFoundException { 
    for (File file : new File(sourceDir).listFiles()) { 
     if (file.isDirectory()) { 
      compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out); 
     } else { 
      ZipEntry entry = new ZipEntry(sourceDir.replace(rootDir, "") + file.getName()); 
      out.putNextEntry(entry); 

      FileInputStream in = new FileInputStream(sourceDir + file.getName()); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
     } 
    } 
} 

, 내가 스트림 데이터를 처리 할 수 ​​IOUtils.copy()을 사용하고 있습니다 이전.

+0

10 번째 줄에 오류가 있습니다. File.separator가 너무 늦게 하나의 concat에 삽입되었습니다. –

+0

이 줄에는 하나의 오류가 있습니다 ..이 줄에서 FileInputStream = new FileInputStream (sourceDir + file.getName()); 그것은 FileInputStream = new FileInputStream (sourceDir + File.separator + file.getName());에 있어야합니다. – shiva

2

위의 오류가 수정되어 완벽하게 작동합니다.

public static void compressZipfile(String sourceDir, String outputFile) throws IOException, FileNotFoundException { 
    ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(outputFile)); 
    Path srcPath = Paths.get(sourceDir); 
    compressDirectoryToZipfile(srcPath.getParent().toString(), srcPath.getFileName().toString(), zipFile); 
    IOUtils.closeQuietly(zipFile); 
} 

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException, FileNotFoundException { 
    String dir = Paths.get(rootDir, sourceDir).toString(); 
    for (File file : new File(dir).listFiles()) { 
     if (file.isDirectory()) { 
      compressDirectoryToZipfile(rootDir, Paths.get(sourceDir,file.getName()).toString(), out); 
     } else { 
      ZipEntry entry = new ZipEntry(Paths.get(sourceDir,file.getName()).toString()); 
      out.putNextEntry(entry); 

      FileInputStream in = new FileInputStream(Paths.get(rootDir, sourceDir, file.getName()).toString()); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
     } 
    } 
}