2013-03-13 18 views
6

Dizin arşivini java kullanarak sıkıştırmaya çalışıyorum. Her şey yolunda, ama sadece bazı gerçekleri açıklığa kavuşturmak istiyorum. Şimdi sorular vardırSıkıştırılmış arşiv sıkıştırması

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(); 
    } 
    } 
}

: İşte dosyaları sıkıştırmak için kullandığınız koddur

  1. doğru böyle mi varsayılan olarak (ve de benim durumumda) Zaten sıkıştırılmış alacak arşiv (Deflate yöntemini kullanarak)? sıkıştırılmamış arşiv almak nasıl

  2. : Ben boyutu, sıkıştırılmış boyutu sağlamak zorunda yöntemi zipOutputStream.setMethod(ZipOutputStream.STORED) ayarlarsanız

    • aksi takdirde istisnaları
    • alacak ve crc (? o boyuta eşit olacaktır)
    • Boyut ve crc'yi kendim hesaplamak istemezsem DEFLATE yöntemini sıfır düzeyiyle kullanabilirim:
      zipOutputStream.setMethod(ZipOutputStream.DEFLATED); 
      zipOutputStream.setLevel(ZipOutputStream.STORED);
      Yani, bu durumda sıkıştırılmış bir arşiv almam doğru mu?
    • Sıkıştırılmamış arşivler oluşturmanın daha kullanışlı ve açık yöntemi var mı?
+3

Bu [cevap] (http://stackoverflow.com/a/1207041/354831) STORED için size yardımcı olabilir. –

+1

Ayrıca, kullanım durumunuza bağlı olarak, sıkıştırılmamış arşiv için '.zip' yerine .tar' biçimini de dikkate alabilirsiniz. Bunun için bir lib: https://code.google.com/p/jtar/ – hyde

cevap

-2

yerine yeniden icat Ciddi böyle Apache Ant gibi, bunun için mevcut bir kitaplığı kullanarak düşünün tekerleği. Bir zip dosyası oluşturmayla ilgili temel deyim:

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(); 

Varsayılan olarak sıkıştırılmış bir arşiv alırsınız. sıkıştırılmamış zip için eklemek için gereken tüm (her yerde perform önce aslında) setDestFile sonra

zip.setCompress(false); 

olduğunu.

İlgili konular