2013-03-13 5 views
6

java를 사용하여 디렉토리 내용을 zip 압축 파일로 압축하려고합니다. 모든 것이 정상이지만 몇 가지 사실을 분명히 밝힙니다. 이제 질문이 있습니다압축 파일 압축

public void pack(@Nonnull String archiveName, @Nonnull File outputDir, @Nonnull File targetDir) { 
    File zipFile = new File(outputDir, "out.zip"); 

    ZipOutputStream zipOutputStream = null; 
    OutputStream outputStream; 
    try { 
    // create stream for writing zip archive 
    outputStream = new FileOutputStream(zipFile); 
    zipOutputStream = new ZipOutputStream(outputStream); 
    // write files recursively 
    writeFiles(zipOutputStream, targetDir.listFiles(), ""); 
    } catch (IOException e) { 
    LOGGER.error("IO exception while packing files to archive", e); 
    } finally { 
    // close output streams 
    if (zipOutputStream != null) { 
     try { 
     zipOutputStream.close(); 
     } catch (IOException e) { 
     LOGGER.error("Unable to close zip output stream", e); 
     } 
    } 
    } 
} 

/** 
* Writes specified files and their children (in case of directories) to archive 
* 
* @param zipOutputStream archive output stream 
* @param files   which should be added to archive 
* @param path   path relative of root of archive where files should be placed 
*/ 
private void writeFiles(@Nonnull ZipOutputStream zipOutputStream, @Nullable File[] files, @Nonnull String path) throws IOException { 
    if (files == null || files.length == 0) { 
    return; 
    } 

    for (File file : files) { 
    if (file.isDirectory()) { 
     // recursively add files in this directory 
     String fullDirectoryName = path + file.getName() + "/"; 
     File[] childFiles = file.listFiles(); 
     if (childFiles != null && childFiles.length > 0) { 
     // write child files to archive. current directory will be created automatically 
     writeFiles(zipOutputStream, childFiles, fullDirectoryName); 
     } else { 
     // empty directory. write directory itself to archive 
     ZipEntry entry = new ZipEntry(fullDirectoryName); 
     zipOutputStream.putNextEntry(entry); 
     zipOutputStream.closeEntry(); 
     } 
    } else { 
     // put file in archive 
     BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); 
     zipOutputStream.putNextEntry(new ZipEntry(path + file.getName())); 
     ByteStreams.copy(bufferedInputStream, zipOutputStream); 
     zipOutputStream.closeEntry(); 
     bufferedInputStream.close(); 
    } 
    } 
}

: 는 여기에 내가 파일을 압축하는 데 사용하는 코드입니다

  1. 그것이 올바른인가요 기본적으로 (너무 내 경우) 이미 압축 얻을 것이다 아카이브 (Deflate 메소드 사용)? 압축 보관하는 방법을

  2. : 나는 크기, 압축 된 크기를 제공 할 방법 zipOutputStream.setMethod(ZipOutputStream.STORED)을 설정하면

    • 을, 그렇지 않으면 나는 예외
    • 을 얻을 것이다, 및 CRC (? 그것은 크기와 동일한 것입니다)
    • 크기와 crc를 계산하고 싶지 않다면, 0 레벨로 DEFLATE 메서드를 사용할 수 있습니다 :
      zipOutputStream.setMethod(ZipOutputStream.DEFLATED); 
      zipOutputStream.setLevel(ZipOutputStream.STORED);
      그래서이 경우 압축 된 아카이브가 전혀 없습니까?
    • 압축되지 않은 압축 파일을 만드는 데 더 편리한 방법이 있습니까?
+3

저장됨에 대한 답변 [http://stackoverflow.com/a/1207041/354831]이 도움이 될 수 있습니다. –

+1

유즈 케이스에 따라 압축되지 않은 아카이브의 경우'.zip' 대신'.tar' 형식을 사용할 수도 있습니다. 하나의 lib : https://code.google.com/p/jtar/ – hyde

답변

-2

보다는 다시 발명 나는 심각하게 아파치 개미로,이를 위해 기존 라이브러리를 사용하는 것을 고려하려는 휠을. zip 파일을 만드는 기본 관용구는 다음과 같습니다.

Project p = new Project(); 
p.init(); 
Zip zip = new Zip(); 
zip.setProject(p); 
zip.setDestFile(new File(outputDir, "out.zip")); 
FileSet fs = new FileSet(); 
fs.setProject(p); 
fs.setDirectory(targetDir); 
zip.addFileset(fs); 
zip.perform(); 

기본적으로 압축 된 아카이브가 제공됩니다. 압축되지 않은 지퍼를 들어 추가 할 필요가있는 (어느 곳에서나 perform 전에 사실)을 setDestFile

zip.setCompress(false); 

입니다.