2011-09-20 10 views
25

फ़ाइलों को ज़िप और अनजिप करने के लिए जो पहले से ही डीडीएमएस में हैं: data/data/mypackage/files/ मुझे इसके लिए एक साधारण उदाहरण चाहिए। मैं पहले से ही ज़िप और अनजिप से संबंधित खोज कर चुका हूं। लेकिन, मेरे लिए कोई भी उदाहरण उपलब्ध नहीं है। क्या कोई कुछ उदाहरण बता सकता है। अग्रिम धन्यवाद।फ़ाइलों को ज़िप और अनजिप कैसे करें?

+1

यहाँ एक नजर डालें। – nhaarman

उत्तर

55

ज़िप कार्यक्षमता के लिए java.util.zip। * कक्षाओं पर एक नज़र डालें। मैंने कुछ बुनियादी ज़िप/अनजिप कोड किया है, जिसे मैंने नीचे चिपकाया है। आशा करता हूँ की ये काम करेगा।

public static void zip(String[] files, String zipFile) throws IOException { 
    BufferedInputStream origin = null; 
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); 
    try { 
     byte data[] = new byte[BUFFER_SIZE]; 

     for (int i = 0; i < files.length; i++) { 
      FileInputStream fi = new FileInputStream(files[i]);  
      origin = new BufferedInputStream(fi, BUFFER_SIZE); 
      try { 
       ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); 
       out.putNextEntry(entry); 
       int count; 
       while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { 
        out.write(data, 0, count); 
       } 
      } 
      finally { 
       origin.close(); 
      } 
     } 
    } 
    finally { 
     out.close(); 
    } 
} 

public static void unzip(String zipFile, String location) throws IOException { 
    try { 
     File f = new File(location); 
     if(!f.isDirectory()) { 
      f.mkdirs(); 
     } 
     ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); 
     try { 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
       String path = location + ze.getName(); 

       if (ze.isDirectory()) { 
        File unzipFile = new File(path); 
        if(!unzipFile.isDirectory()) { 
         unzipFile.mkdirs(); 
        } 
       } 
       else { 
        FileOutputStream fout = new FileOutputStream(path, false); 
        try { 
         for (int c = zin.read(); c != -1; c = zin.read()) { 
          fout.write(c); 
         } 
         zin.closeEntry(); 
        } 
        finally { 
         fout.close(); 
        } 
       } 
      } 
     } 
     finally { 
      zin.close(); 
     } 
    } 
    catch (Exception e) { 
     Log.e(TAG, "Unzip exception", e); 
    } 
} 
+0

मेरे लिए काम नहीं करता है। अनंत पाश में छड़ें। – usman

+0

यह गलत है: 'स्ट्रिंग पथ = स्थान + ze.getName(); यदि (ze.isDirectory()) { फ़ाइल unzipFile = नई फ़ाइल (पथ); अगर (! UnzipFile.isDirectory()) { unzipFile.mkdirs(); } } ' – maohieng

+0

बफर आकार परिवर्तक का मूल्य क्या होना चाहिए? –

45

ज़िप समारोह brianestey काम करता है अच्छी तरह से प्रदान की जाती है, लेकिन unzip समारोह की वजह से एक समय में एक बाइट पढ़ने के लिए बहुत धीमी है। यहां उनके अनजिप फ़ंक्शन का एक संशोधित संस्करण है जो एक बफर का उपयोग करता है और बहुत तेज़ है। जब के लिए खोज गूगल पर "एंड्रॉयड फ़ाइलें ज़िप करने के लिए कैसे" पहले पृष्ठ पर, http://www.jondev.net/articles/Zipping_Files_with_Android_(Programmatically):

/** 
* Unzip a zip file. Will overwrite existing files. 
* 
* @param zipFile Full path of the zip file you'd like to unzip. 
* @param location Full path of the directory you'd like to unzip to (will be created if it doesn't exist). 
* @throws IOException 
*/ 
public static void unzip(String zipFile, String location) throws IOException { 
    int size; 
    byte[] buffer = new byte[BUFFER_SIZE]; 

    try { 
     if (!location.endsWith(File.separator)) { 
      location += File.separator; 
     } 
     File f = new File(location); 
     if(!f.isDirectory()) { 
      f.mkdirs(); 
     } 
     ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE)); 
     try { 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
       String path = location + ze.getName(); 
       File unzipFile = new File(path); 

       if (ze.isDirectory()) { 
        if(!unzipFile.isDirectory()) { 
         unzipFile.mkdirs(); 
        } 
       } else { 
        // check for and create parent directories if they don't exist 
        File parentDir = unzipFile.getParentFile(); 
        if (null != parentDir) { 
         if (!parentDir.isDirectory()) { 
          parentDir.mkdirs(); 
         } 
        } 

        // unzip the file 
        FileOutputStream out = new FileOutputStream(unzipFile, false); 
        BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE); 
        try { 
         while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) { 
          fout.write(buffer, 0, size); 
         } 

         zin.closeEntry(); 
        } 
        finally { 
         fout.flush(); 
         fout.close(); 
        } 
       } 
      } 
     } 
     finally { 
      zin.close(); 
     } 
    } 
    catch (Exception e) { 
     Log.e(TAG, "Unzip exception", e); 
    } 
} 
+2

यह अनजिप मेरे लिए काम नहीं करता था। मुझे त्रुटियां मिल रही थीं क्योंकि यह अपनी मूल निर्देशिका से पहले फ़ाइल को अनजिप करने का प्रयास कर रही थी। अन्य कथन में मैंने मूल निर्देशिका बनाने के लिए एक शर्त जोड़ा है यदि यह अस्तित्व में नहीं है। अब वह काम करता है। –

+0

@ जे-एल इसे इंगित करने के लिए धन्यवाद। मैंने तदनुसार कोड संपादित किया है। – Ben

+0

तेज़ और काम gr8, मेरा BUFFER_SIZE = 8192 –