2012-05-17 17 views
16

क्या कोई नमूना कोड है, आंशिक रूप से ज़िप से मेरी वांछित निर्देशिका में फ़ोल्डर को अनजिप कैसे करें? मैंने बाइट सरणी में फ़ोल्डर "फ़ोल्डर" से सभी फाइलें पढ़ी हैं, मैं अपनी फाइल संरचना से कैसे पुन: निर्माण करूं?जावा ज़िप - फ़ोल्डर को अनजिप कैसे करें?

उत्तर

0

आप अपने ज़िप फ़ाइल से सभी प्रविष्टियों मिलना चाहिए:

Enumeration entries = zipFile.getEntries(); 

तो itareting इस गणना खत्म होने ZipEntry इसे से, जाँच करें कि क्या यह एक निर्देशिका है या नहीं, और dirctrory बना सकते हैं या सिर्फ एक फ़ाइल respectivly निकालने ।

+0

यह हिस्सा मैं वास्तव में जरूरत है ... मैं में मेरे फ़ोल्डर के लिए उपयोग किया ज़िप और ज़िप से इसकी सामग्री के साथ sdcard/foldername में इसे स्टोर करना चाहते हैं। उसको कैसे करे? – Waypoint

+1

अच्छी तरह से, मुझे लगता है कि आपको कुछ कोड लिखने का प्रयास करना चाहिए, कुछ उदाहरणों पर नज़र डालें और यदि आप असफल हो जाते हैं या अटक जाते हैं - तो अपने कोड के साथ यहां वापस आएं। –

21

यहां कोड है जिसका मैं उपयोग कर रहा हूं। अपनी जरूरतों के लिए BUFFER_SIZE बदलें।

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

public class ZipUtils 
{ 
    private static final int BUFFER_SIZE = 4096; 

    private static void extractFile(ZipInputStream in, File outdir, String name) throws IOException 
    { 
    byte[] buffer = new byte[BUFFER_SIZE]; 
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir,name))); 
    int count = -1; 
    while ((count = in.read(buffer)) != -1) 
     out.write(buffer, 0, count); 
    out.close(); 
    } 

    private static void mkdirs(File outdir,String path) 
    { 
    File d = new File(outdir, path); 
    if(!d.exists()) 
     d.mkdirs(); 
    } 

    private static String dirpart(String name) 
    { 
    int s = name.lastIndexOf(File.separatorChar); 
    return s == -1 ? null : name.substring(0, s); 
    } 

    /*** 
    * Extract zipfile to outdir with complete directory structure 
    * @param zipfile Input .zip file 
    * @param outdir Output directory 
    */ 
    public static void extract(File zipfile, File outdir) 
    { 
    try 
    { 
     ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile)); 
     ZipEntry entry; 
     String name, dir; 
     while ((entry = zin.getNextEntry()) != null) 
     { 
     name = entry.getName(); 
     if(entry.isDirectory()) 
     { 
      mkdirs(outdir,name); 
      continue; 
     } 
     /* this part is necessary because file entry can come before 
     * directory entry where is file located 
     * i.e.: 
     * /foo/foo.txt 
     * /foo/ 
     */ 
     dir = dirpart(name); 
     if(dir != null) 
      mkdirs(outdir,dir); 

     extractFile(zin, outdir, name); 
     } 
     zin.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    } 
} 
+4

आपको IOException निगलना नहीं चाहिए। –

+0

यह मेरे लिए काम करता है। धन्यवाद। –

10

चींटी कंप्रेस लाइब्रेरी का उपयोग करके भी हासिल किया जा सकता है। यह फ़ोल्डर संरचना को संरक्षित रखेगा।

Maven निर्भरता: -

<dependency> 
    <groupId>org.apache.ant</groupId> 
    <artifactId>ant-compress</artifactId> 
    <version>1.2</version> 
</dependency> 

नमूना कोड: -

Unzip unzipper = new Unzip(); 
unzipper.setSrc(theZIPFile); 
unzipper.setDest(theTargetFolder); 
unzipper.execute(); 
24

मुझे यकीन है कि आप particaly से क्या मतलब है नहीं कर रहा हूँ? क्या आपका मतलब एपीआई सहायता के बिना स्वयं ही करें?

मामले आप कुछ खुला स्रोत पुस्तकालय का उपयोग कर कोई आपत्ति नहीं है में, वहाँ उस के लिए एक शांत एपीआई बाहर वहाँ zip4J

यह उपयोग करने के लिए आसान है कहा जाता है और मुझे लगता है कि इसके बारे में अच्छा प्रतिक्रिया होती है। फ़ाइलें आप अनज़िप करने के लिए पासवर्ड है चाहते हैं, तो आप इस कोशिश कर सकते हैं, तो

String source = "folder/source.zip"; 
String destination = "folder/source/"; 

try { 
    ZipFile zipFile = new ZipFile(source); 
    zipFile.extractAll(destination); 
} catch (ZipException e) { 
    e.printStackTrace(); 
} 

: इस उदाहरण देखें

String source = "folder/source.zip"; 
String destination = "folder/source/"; 
String password = "password"; 

try { 
    ZipFile zipFile = new ZipFile(source); 
    if (zipFile.isEncrypted()) { 
     zipFile.setPassword(password); 
    } 
    zipFile.extractAll(destination); 
} catch (ZipException e) { 
    e.printStackTrace(); 
} 

मुझे आशा है कि यह उपयोगी है।

2

यहां एक आसान समाधान है जो अधिक आधुनिक सम्मेलनों का पालन करता है। यदि आप बड़ी फ़ाइलों को अनजिप कर रहे हैं तो आप बफर आकार को छोटा होने के लिए बदल सकते हैं। ऐसा इसलिए है कि आप सभी फ़ाइलों की जानकारी स्मृति में नहीं रखते हैं।

public static void unzip(File source, String out) throws IOException { 
    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) { 

     ZipEntry entry = zis.getNextEntry(); 

     while (entry != null) { 

      File file = new File(out, entry.getName()); 

      if (entry.isDirectory()) { 
       file.mkdirs(); 
      } else { 
       File parent = file.getParentFile(); 

       if (!parent.exists()) { 
        parent.mkdirs(); 
       } 

       try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { 

        byte[] buffer = new byte[Math.toIntExact(entry.getSize())]; 

        int location; 

        while ((location = zis.read(buffer)) != -1) { 
         bos.write(buffer, 0, location); 
        } 
       } 
      } 
      entry = zis.getNextEntry(); 
     } 
    } 
} 
0

यहाँ this पद पर आधारित है और अधिक "आधुनिक" पूरा कोड लेकिन पुनर्संशोधित (और का उपयोग कर Lombok):

import lombok.experimental.var; 
import lombok.val; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipInputStream; 

import static java.nio.file.Files.createDirectories; 

public class UnZip 
{ 
    public static void unZip(String sourceZipFile, String outputDirectory) throws IOException 
    { 
     val folder = new File(outputDirectory); 
     createDirectories(folder.toPath()); 

     try (val zipInputStream = new ZipInputStream(new FileInputStream(sourceZipFile))) 
     { 
      var nextEntry = zipInputStream.getNextEntry(); 

      while (nextEntry != null) 
      { 
       val fileName = nextEntry.getName(); 
       val newFile = new File(outputDirectory + File.separator + fileName); 

       createDirectories(newFile.getParentFile().toPath()); 
       writeFile(zipInputStream, newFile); 

       nextEntry = zipInputStream.getNextEntry(); 
      } 

      zipInputStream.closeEntry(); 
     } 
    } 

    private static void writeFile(ZipInputStream inputStream, File file) throws IOException 
    { 
     val buffer = new byte[1024]; 
     try (val fileOutputStream = new FileOutputStream(file)) 
     { 
      int length; 
      while ((length = inputStream.read(buffer)) > 0) 
      { 
       fileOutputStream.write(buffer, 0, length); 
      } 
     } 
    } 
} 
संबंधित मुद्दे