2013-03-13 9 views
6

का संपीड़न मैं जावा का उपयोग करके ज़िप संग्रह में निर्देशिका सामग्री को संपीड़ित करने का प्रयास कर रहा हूं। सब कुछ ठीक है, लेकिन मैं बस कुछ तथ्यों को स्पष्ट करना चाहता हूं।ज़िप संग्रह

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) सेट मैं आकार, संकुचित आकार प्रदान करने के लिए है, तो और सीआरसी (? यह आकार के बराबर हो जाएगा है) अन्यथा मैं अपवाद
    • मिल जाएगा
    • यदि मैं अपने आप से आकार और सीआरसी की गणना नहीं करना चाहता हूं तो मैं शून्य स्तर के साथ 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
बजाय

फिर से आविष्कार पहिया मैं गंभीरता से इस तरह के अपाचे चींटी के रूप में इस के लिए एक मौजूदा पुस्तकालय उपयोग करने पर विचार होगा। ज़िप फ़ाइल बनाने के लिए मूल मुहावरे है:

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

डिफ़ॉल्ट रूप से आपको एक संपीड़ित संग्रह प्राप्त होगा। एक असम्पीडित जिप के लिए आप सभी को जोड़ने की आवश्यकता है

zip.setCompress(false); 
setDestFile के बाद ( perform से पहले कहीं भी वास्तव में)

संबंधित मुद्दे