2011-01-23 7 views
31

में ज़िप संग्रह बनाना मेरे पास 7zip प्रोग्राम द्वारा बनाई गई एक फ़ाइल है। मैंने का उपयोग को संपीड़ित करने के लिए डिफ्लेट किया था। अब मैं java में एक ही संग्रह (उसी MD5sum के साथ) बनाना चाहता हूं। जब मैं ज़िप फ़ाइल बनाता हूं, तो मैंने इंटरनेट पर पाया एल्गोरिदम का उपयोग किया उदाहरण के लिए http://www.kodejava.org/examples/119.html लेकिन जब मैंने इस विधि के साथ ज़िप फ़ाइल बनाई तो संपीड़ित आकार असंपीड़ित फ़ाइल के आकार से अधिक है तो क्या चल रहा है? यह एक बहुत ही उपयोगी संपीड़न नहीं है। तो मैं ज़िप फ़ाइल कैसे बना सकता हूं जो कि ज़िप फ़ाइल के समान है जिसे मैंने 7zip प्रोग्राम के साथ बनाया है? अगर यह मदद करता है कि मेरे पास ज़िप फ़ाइल के बारे में सारी जानकारी है जो मैंने 7zip प्रोग्राम में बनाई है।जावा

उत्तर

57
// simplified code for zip creation in java 

import java.io.*; 
import java.util.zip.*; 

public class ZipCreateExample { 

    public static void main(String[] args) throws Exception { 

     // input file 
     FileInputStream in = new FileInputStream("F:/sometxt.txt"); 

     // out put file 
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip")); 

     // name the file inside the zip file 
     out.putNextEntry(new ZipEntry("zippedjava.txt")); 

     // buffer size 
     byte[] b = new byte[1024]; 
     int count; 

     while ((count = in.read(b)) > 0) { 
      out.write(b, 0, count); 
     } 
     out.close(); 
     in.close(); 
    } 
} 
+0

मैं ज़िप फ़ोल्डर खोलने में असमर्थ हूं, यह कहता है कि संपीड़ित फ़ोल्डर तक पहुंच अस्वीकार कर दी गई है –

4

बस स्पष्ट करने के लिए, आपने अपने मूल के लिए 7zip में ज़िप एल्गोरिदम का उपयोग किया है? इसके अलावा 7zip अन्य विक्रेताओं की तुलना में 2-10% बेहतर संपीड़न अनुपात होने का दावा करता है। मैं अनुमान लगाता हूं कि जावा में निर्मित ज़िप एल्गोरिदम लगभग 7zip में से एक के रूप में अनुकूलित नहीं है। यदि आप एक समान संपीड़ित फ़ाइल चाहते हैं तो कमांड लाइन से 7zip को आमंत्रित करना सबसे अच्छा सबसे अच्छा है।

क्या आप ज़िप फ़ाइल को अनपैक करने का प्रयास कर रहे हैं, इसके अंदर एक फ़ाइल बदल सकते हैं, फिर इसे फिर से संपीड़ित करें ताकि उसके पास एक ही MD5 हैश हो? हैश आपको ऐसा करने से रोकने के लिए हैं।

+0

इसके अलावा, 7zip के साथ संपीड़न करते समय बहुत सारे विकल्प उपलब्ध हैं। यहां तक ​​कि सीधे 7zip का आह्वान करते समय भी, आपको अभी भी कॉन्फ़िगरेशन का अनुमान लगाना होगा। – Cephalopod

+0

मैं बहुत सारी कॉन्फ़िगरेशन आज़माता हूं लेकिन अभी भी संपीड़ित आकार बिल्कुल असम्पीडित आकार या उससे कम के समान था लेकिन कभी भी – hudi

+0

एचएम और कमांड लाइन से 7zip कैसे आ सकता है? क्या आप मुझे एक उदाहरण – hudi

5

ZipOutputStream धुन संपीड़न के कुछ तरीके हैं:

public void setMethod(int method)

इसके बाद की प्रविष्टियों के लिए डिफ़ॉल्ट संपीड़न विधि सेट करता है। यह डिफ़ॉल्ट तब भी उपयोग किया जाएगा जब संपीड़न विधि व्यक्तिगत ज़िप फ़ाइल प्रविष्टि के लिए निर्दिष्ट नहीं है, और प्रारंभ में डिफलेटेड पर सेट है।

public void setLevel(int level)

इसके बाद की प्रविष्टियों जो हवा निकाल रहे हैं के लिए संपीड़न स्तर सेट करता है। डिफ़ॉल्ट सेटिंग DEFAULT_COMPRESSION है। स्तर - संपीड़न स्तर (0-9)

आप की तरह कुछ के बाद जोड़ते हैं:

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target)); 
zos.setMethod(ZipOutputStream.DEFLATED); 
zos.setLevel(5); 
... 

यह अपने संपीड़न में सुधार नहीं करता है?

+0

जैसा कि मैंने डिफ्लेटेड विधि का उपयोग करने से पहले कहा था और मैं संपीड़न के सभी स्तर (-1 .. 9) को भी आजमा सकता हूं लेकिन अभी भी कुछ भी नहीं बदला है। – hudi

1

एक ही स्रोत फ़ाइल से (समान md5sum सहित) दो समान ज़िप फ़ाइलें बनाने के लिए, मैं एक ही जिप सुविधा का उपयोग की सिफारिश करेंगे - या तो हमेशा एक ही जावा प्रोग्राम का उपयोग करें, या हमेशा 7zip का उपयोग। जिनमें से कई बस चूक अनुकूलित किया जा सकता है कि (? या विज्ञप्ति के बीच मतभेद हैं) कर रहे हैं - -

उदाहरण के लिए 7zip उपयोगिता विकल्पों में से एक बहुत है और किसी भी जावा ज़िप कार्यान्वयन भी स्पष्ट रूप से इन विकल्पों को सेट करने के लिए होगा । यदि आपका जावा ऐप बस एक बाहरी "7z" प्रोग्राम का आह्वान कर सकता है, तो संभवतः आपको एक कस्टम जावा ज़िप कार्यान्वयन के बावजूद बेहतर प्रदर्शन मिलेगा। (यह मानचित्र-कम समस्या का एक अच्छा उदाहरण भी है जहां आप आसानी से कार्यान्वयन को स्केल कर सकते हैं।)

लेकिन यदि आपके पास सर्वर-साइड जेनरेट की गई ज़िप फ़ाइल और क्लाइंट-साइड है तो मुख्य समस्या जेनरेट की गई ज़िप फ़ाइल यह है कि ज़िप फ़ाइल केवल मूल फ़ाइल के अतिरिक्त दो चीजें संग्रहीत करती है: (1) फ़ाइल का नाम, और (2) फ़ाइल टाइमस्टैम्प।यदि इनमें से किसी को बदल दिया है, तो जिसके परिणामस्वरूप ज़िप फ़ाइल एक अलग md5sum होगा:

$ ls tst1/ 
foo.tar 

$ cp -r tst1 tst2 

$ (cd tst1; zip foo.zip foo.tar) ; (cd tst2; zip foo.zip foo.tar) ; md5sum tst?/foo.zip 
updating: foo.tar (deflated 20%) 
updating: foo.tar (deflated 20%) 
359b82678a2e17c1ddbc795ceeae7b60 tst1/foo.zip 
b55c33c0414ff987597d3ef9ad8d1d08 tst2/foo.zip 

लेकिन, का उपयोग करते हुए "cp -p" (टाइमस्टैम्प की रक्षा):

$ cp -p -r tst1 tst2 

$ (cd tst1; zip foo.zip foo.tar) ; (cd tst2; zip foo.zip foo.tar) ; md5sum tst?/foo.zip 
updating: foo.tar (deflated 20%) 
updating: foo.tar (deflated 20%) 
359b82678a2e17c1ddbc795ceeae7b60 tst1/foo.zip 
359b82678a2e17c1ddbc795ceeae7b60 tst2/foo.zip 

आप पाएंगे अलग-अलग फ़ाइल नामों और पथों के साथ एक ही समस्या, भले ही ज़िप के अंदर की फाइलें समान हों।

4

यहां एक ऐसा फ़ंक्शन है जिसे आप पूर्ण पथ पारित करते हैं, यह निर्देशिका के समान नाम के साथ एक ज़िप फ़ाइल बनाएगा (जिसके अंतर्गत आप सभी उप फ़ोल्डर और फ़ाइलों का सब कुछ चाहते हैं !!) और सफलता पर सच और यदि कोई हो तो अपवाद पर झूठा।

public class FileUtil { 
final static int BUFFER = 2048; 
private static Logger log = Logger.getLogger(FileUtil.class); 

     public static boolean createZipArchive(String srcFolder) { 

    try { 
     BufferedInputStream origin = null; 



     FileOutputStream dest = new FileOutputStream(new File(srcFolder+ ".zip")); 

     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
     byte data[] = new byte[BUFFER]; 

     File subDir = new File(srcFolder); 
     String subdirList[] = subDir.list(); 
     for(String sd:subdirList) 
     { 
       // get a list of files from current directory 
       File f = new File(srcFolder+"/"+sd); 
       if(f.isDirectory()) 
       { 
        String files[] = f.list(); 

        for (int i = 0; i < files.length; i++) { 
         System.out.println("Adding: " + files[i]); 
         FileInputStream fi = new FileInputStream(srcFolder + "/"+sd+"/" + files[i]); 
         origin = new BufferedInputStream(fi, BUFFER); 
         ZipEntry entry = new ZipEntry(sd +"/"+files[i]); 
         out.putNextEntry(entry); 
         int count; 
         while ((count = origin.read(data, 0, BUFFER)) != -1) { 
          out.write(data, 0, count); 
          out.flush(); 
         } 

        } 
       } 
       else //it is just a file 
       { 
        FileInputStream fi = new FileInputStream(f); 
        origin = new BufferedInputStream(fi, BUFFER); 
        ZipEntry entry = new ZipEntry(sd); 
        out.putNextEntry(entry); 
        int count; 
        while ((count = origin.read(data, 0, BUFFER)) != -1) { 
         out.write(data, 0, count); 
         out.flush(); 
        } 

       } 
     } 
     origin.close(); 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
     log.info("createZipArchive threw exception: " + e.getMessage());   
     return false; 

    } 


    return true; 
} 
    } 
0
package comm; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream;*emphasized text* 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

public class Zip1 { 
     public static void main(String[] args) 
     { 
      byte[] buffer = new byte[1024]; 

      try{ 

       File f= new File("E:\\"); 
       f.mkdirs(); 
       File origFile= new File(f,"MyZipFile2.zip"); 
       FileOutputStream fos = new FileOutputStream(origFile); 

       ZipOutputStream zos = new ZipOutputStream(fos); 
       ZipEntry ze= new ZipEntry("test.pdf"); 
       zos.putNextEntry(ze); 
       FileInputStream in = new FileInputStream("D:\\Test.pdf"); 

       int len; 
       while ((len = in.read(buffer)) > 0) { 
        zos.write(buffer, 0, len); 
       } 

       in.close(); 
       zos.closeEntry(); 

       //remember close it 
       zos.close(); 

       System.out.println("Done"); 

      }catch(IOException ex){ 
       ex.printStackTrace(); 
      } 
     } 
} 
0

कृपया करने के लिए ज़िप और अनज़िप नीचे कोड कार्यक्षमताओं होने में पाते हैं। उम्मीद है कि यह किसी की मदद कर सकता है।

package com.util; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 
import java.util.zip.ZipOutputStream; 


/** 
* @author dinesh.lomte 
* 
*/ 
public class ZipUtil { 

    /** 
    * 
    * @param source 
    * @param destination 
    */ 
    public static void unZip(String source, String destination) { 

     String method = "unZip(String source, String destination)"; 
     ZipInputStream zipInputStream = null; 
     try { 
      // Creating the ZipInputStream instance from the source file 
      zipInputStream = new ZipInputStream(new FileInputStream(source)); 
      // Getting the zipped file list entry 
      ZipEntry zipEntry = zipInputStream.getNextEntry(); 
      // Iterating through the file list entry 
      while (zipEntry != null) { 
       String fileName = zipEntry.getName(); 
       File file = new File(new StringBuilder(destination) 
        .append(File.separator) 
        .append(AppUtil.getFileNameWithoutExtension(
          AppUtil.getNameFromPath(source))) 
        .append(File.separator).append(fileName).toString());     
       // Creating non existing folders to avoid any FileNotFoundException 
       // for compressed folder 
       new File(file.getParent()).mkdirs(); 
       FileOutputStream fileOutputStream = new FileOutputStream(file); 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = zipInputStream.read(buffer)) > 0) { 
        fileOutputStream.write(buffer, 0, length); 
       } 
       fileOutputStream.close(); 
       zipEntry = zipInputStream.getNextEntry(); 
      } 
     } catch (IOException iOException) { 
      System.out.println("Failed to unzip the ''{0}'' file located in ''{1}'' folder. Due to, {2}"); 

     } finally { 
      // Validating if zipInputStream instance in not null 
      if (zipInputStream != null) { 
       try { 
        zipInputStream.closeEntry(); 
        zipInputStream.close(); 
       } catch (IOException iOException) {     
       } 
      } 
     } 
    } 

    /** 
    * Traverse a directory from the source folder location and get all files, 
    * and add the file into files list. 
    * 
    * @param node 
    */ 
    public static void generateFileList(
      String source, File node, List<String> files) {  
     // Validating if the node is a file 
     if (node.isFile()) { 
      files.add(generateZipEntry(
        source, node.getPath().toString())); 
     } 
     // Validating if the node is a directory 
     if (node.isDirectory()) { 
      String[] subNote = node.list(); 
      for (String filename : subNote) { 
       generateFileList(source, new File(node, filename), files); 
      } 
     } 
    } 

    /** 
    * Format the file path to zip 
    * @param source 
    * @param file 
    * @return 
    */ 
    private static String generateZipEntry(String source, String file) { 
     return file.substring(source.length(), file.length()); 
    } 

    /** 
    * 
    * @param source 
    * @param destination 
    */ 
    public static void zip(String source, String destination) { 

     String method = "zip(String source, String destination)"; 
     ZipOutputStream zipOutputStream = null;   
     try {    
      // Creating the zipOutputStream instance 
      zipOutputStream = new ZipOutputStream(
        new FileOutputStream(destination)); 
      List<String> files = new ArrayList<>(); 
      generateFileList(source, new File(source), files); 
      // Iterating the list of file(s) to zip/compress 
      for (String file : files) { 
       // Adding the file(s) to the zip 
       ZipEntry zipEntry = new ZipEntry(file); 
       zipOutputStream.putNextEntry(zipEntry); 
       FileInputStream fileInputStream = new FileInputStream(
         new StringBuilder(source).append(File.separator) 
         .append(file).toString()); 
       int length; 
       byte[] buffer = new byte[1024]; 
       while ((length = fileInputStream.read(buffer)) > 0) { 
        zipOutputStream.write(buffer, 0, length); 
       }     
       // Closing the fileInputStream instance 
       fileInputStream.close(); 
       // De-allocating the memory by assigning the null value 
       fileInputStream = null; 
      } 
     } catch (IOException iOException) { 
      System.out.println("Failed to zip the file(s) located in ''{0}'' folder. Due to, {1}"); 
     } finally { 
      // Validating if zipOutputStream instance in not null 
      if (zipOutputStream != null) { 
       try { 
        zipOutputStream.closeEntry(); 
        zipOutputStream.close(); 
       } catch (IOException iOException) { 
       } 
      } 
     } 
    } 
}