2012-07-02 22 views
6

के अंदर एक ज़िप फ़ाइल पढ़ें मेरे पास ज़िप फ़ाइल है जो ज़िप फ़ाइल में किसी फ़ोल्डर के अंदर है कृपया मुझे सुझाव दें कि ज़िप इनपुट स्ट्रीम का उपयोग करके इसे कैसे पढ़ा जाए।ज़िप फ़ाइल

उदा .:

abc.zip 
    | 
     documents/bcd.zip 

कैसे ज़िप फ़ाइल के अंदर एक ज़िप फ़ाइल को पढ़ने के लिए?

+1

क्या आप पढ़ने के द्वारा मतलब है? क्या आप bcd.zip निकालना चाहते हैं? – Sujay

उत्तर

5

निम्न कोड स्निपेट एक ज़िप फ़ाइल की प्रविष्टियों को एक और ज़िप फ़ाइल के अंदर सूचीबद्ध करता है। इसे अपनी जरूरतों के अनुसार अनुकूलित करें। ZipFile हुड के नीचे ZipInputStreams का उपयोग करता है।

कोड स्निपेट Apache Commons IO का उपयोग करता है, विशेष रूप से IOUtils.copy

public static void readInnerZipFile(File zipFile, String innerZipFileEntryName) { 
    ZipFile outerZipFile = null; 
    File tempFile = null; 
    FileOutputStream tempOut = null; 
    ZipFile innerZipFile = null; 
    try { 
     outerZipFile = new ZipFile(zipFile); 
     tempFile = File.createTempFile("tempFile", "zip"); 
     tempOut = new FileOutputStream(tempFile); 
     IOUtils.copy(// 
       outerZipFile.getInputStream(new ZipEntry(innerZipFileEntryName)), // 
       tempOut); 
     innerZipFile = new ZipFile(tempFile); 
     Enumeration<? extends ZipEntry> entries = innerZipFile.entries(); 
     while (entries.hasMoreElements()) { 
      ZipEntry entry = entries.nextElement(); 
      System.out.println(entry); 
      // InputStream entryIn = innerZipFile.getInputStream(entry); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     // Make sure to clean up your I/O streams 
     try { 
      if (outerZipFile != null) 
       outerZipFile.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     IOUtils.closeQuietly(tempOut); 
     if (tempFile != null && !tempFile.delete()) { 
      System.out.println("Could not delete " + tempFile); 
     } 
     try { 
      if (innerZipFile != null) 
       innerZipFile.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public static void main(String[] args) { 
    readInnerZipFile(new File("abc.zip"), "documents/bcd.zip"); 
} 
+0

ग्रेट यही वह है जिसे मैं ढूंढ रहा हूं। बहुत बहुत धन्यवाद –

+0

बहुत बढ़िया दोस्त .... धन्यवाद ... !!!!! – Milesh

+1

बिल्कुल सही समाधान, जो मैं चाहता था और घंटों के लिए मेरे सिर तोड़ रहा था। आपने मेरा दिन बना दिया। बहुत बहुत धन्यवाद। –

2

आप ज़िप फ़ाइलों रिकर्सिवली भीतर ज़िप फ़ाइलों के माध्यम से देखना चाहते हैं,

public void lookupSomethingInZip(InputStream fileInputStream) throws IOException { 
     ZipInputStream zipInputStream = new ZipInputStream(fileInputStream); 
     String entryName = ""; 
     ZipEntry entry = zipInputStream.getNextEntry(); 
     while (entry!=null) { 
      entryName = entry.getName(); 
      if (entryName.endsWith("zip")) { 
       //recur if the entry is a zip file 
       lookupSomethingInZip(zipInputStream); 
      } 
      //do other operation with the entries.. 

      entry=zipInputStream.getNextEntry(); 
     } 
    } 

कॉल फ़ाइल इनपुट फ़ाइल से ली गई धारा के साथ विधि -

File file = new File(name); 
lookupSomethingInZip(new FileInputStream(file)); 
+0

एक ही ज़िप पर पुनरावृत्ति? किसी काम का नहीं.. – harvish

0

मैं लिखा है एक कोड जो ज़िप फ़ाइल के अंदर सभी ज़िप फ़ाइलों को अनजिप कर सकता है। यह संपीड़न के एन स्तरों को भी अनजिप कर सकता है। उदाहरण के लिए यदि आपके पास ज़िप के अंदर एक ज़िप फ़ाइल थी, तो एक ज़िप (और इसी तरह) के अंदर यह उन सभी को निकालेगा। इस वर्ग के zipFileExtract विधि का प्रयोग करें और स्रोत ज़िप फ़ाइल और गंतव्य निर्देशिका को तर्क के रूप में पास करें।

import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.*; 
import java.util.concurrent.ConcurrentLinkedQueue; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

public class RecursiveFileExtract { 

private static final int BUFFER_SIZE = 4096; 
private static Queue<File> current; 
private static List<File> visited; 

public static void zipFileExtract(File sourceZipFile, File destinationDirectory) { 
    Path temp = null; 
    if(!destinationDirectory.exists()) 
    { 
     destinationDirectory.mkdirs(); 
    } 
    try { 
     temp = Files.move(Paths.get(sourceZipFile.getAbsolutePath()), Paths.get(destinationDirectory.getAbsolutePath()+File.separator+sourceZipFile.getName())); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    File zipFile = new File(temp.toAbsolutePath().toString()); 
    current = new ConcurrentLinkedQueue<>(); 
    visited = new ArrayList<>(); 
    current.add(zipFile); 
    do { 
     unzipCurrent(); 
     zipFinder(destinationDirectory); 
    } 
    while (!current.isEmpty()); 
} 

private static void zipFinder(File directory) { 
    try { 
     if (directory != null) { 
      File fileArray[] = directory.listFiles(); 
      if (fileArray != null) { 
       for (File file : fileArray) { 
        if (file != null) { 
         if (file.isDirectory()) { 
          zipFinder(file); 
         } else { 
          if (file.getName().endsWith(".zip")) { 
           if (!visited.contains(file)) { 
            current.add(file); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } catch (Exception e) { 
     System.out.println(e.getLocalizedMessage()); 
    } 
} 

private static void unzipCurrent() { 
    try { 
     while (!current.isEmpty()) { 
      File file = current.remove(); 
      visited.add(file); 
      File zipDirectory = new File(file.getParentFile().getAbsolutePath()); 
      unzip(file.getAbsolutePath(), zipDirectory.getAbsolutePath()); 
     } 
    } catch (Exception e) { 
     System.out.println(e.getLocalizedMessage()); 
    } 
} 

public static void unzip(String zipFilePath, String destDirectory) { 
    try { 
     ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); 
     ZipEntry entry = zipIn.getNextEntry(); 

     while (entry != null) { 
      String filePath = destDirectory + File.separator + entry.getName(); 
      if (!entry.isDirectory()) { 
       extractFile(zipIn, filePath); 
      } else { 

       File dir = new File(filePath); 
       Boolean result = dir.mkdir(); 
      } 
      zipIn.closeEntry(); 
      entry = zipIn.getNextEntry(); 
     } 
     zipIn.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private static void extractFile(ZipInputStream zipIn, String filePath) { 
    try { 
     File file = new File(filePath); 
     File parentFile = file.getParentFile(); 
     if (!parentFile.exists()) { 
      Boolean result = parentFile.mkdirs(); 
      if (!result) { 
       throw new Exception(); 
      } 
     } 
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); 
     byte[] bytesIn = new byte[BUFFER_SIZE]; 
     int read = 0; 
     while ((read = zipIn.read(bytesIn)) != -1) { 
      bos.write(bytesIn, 0, read); 
     } 
     bos.close(); 
    } catch (Exception e) { 
     System.out.println(e.getLocalizedMessage()); 
    } 
} 

}