2011-12-03 15 views
5

में कैसे परिवर्तित कर सकता हूं मैं बाइट्स की एक सरणी को ज़िप फ़ाइल में परिवर्तित करने की कोशिश कर रहा हूं। यह कैसे किया जा सकता है -मैं बाइट सरणी को ज़िप फ़ाइल

byte[] originalContentBytes= new Verification().readBytesFromAFile(new File("E://file.zip")); 

private byte[] readBytesFromAFile(File file) { 
    int start = 0; 
    int length = 1024; 
    int offset = -1; 
    byte[] buffer = new byte[length]; 
    try { 
     //convert the file content into a byte array 
     FileInputStream fileInuptStream = new FileInputStream(file); 
     BufferedInputStream bufferedInputStream = new BufferedInputStream(
       fileInuptStream); 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

     while ((offset = bufferedInputStream.read(buffer, start, length)) != -1) { 
      byteArrayOutputStream.write(buffer, start, offset); 
     } 

     bufferedInputStream.close(); 
     byteArrayOutputStream.flush(); 
     buffer = byteArrayOutputStream.toByteArray(); 
     byteArrayOutputStream.close(); 
    } catch (FileNotFoundException fileNotFoundException) { 
     fileNotFoundException.printStackTrace(); 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 

    return buffer; 
} 

लेकिन मेरी समस्या अब बाइट सरणी ज़िप फ़ाइल में वापस परिवर्तित करने के साथ है: मैं निम्नलिखित कोड का उपयोग कर बाइट्स मिल गया?

नोट: निर्दिष्ट ज़िप में दो फाइलें हैं।

+0

आप वास्तव में क्या चाहते हैं? क्या आप बाइट्स को डिस्क पर वापस ज़िप फ़ाइल में लिखना चाहते हैं? या आप सामग्री को पढ़ना चाहते हैं? बाइट्स आप उन्हें कैसे पढ़ते हैं अभी तक डीकोड नहीं किए गए हैं। – morja

+0

@ morja -> हाँ मैं बाइट्स को ज़िप फ़ाइल के रूप में डिस्क पर वापस लिखना चाहता हूं। – Mohan

+0

खैर, लेकिन फिर बस एक फ़ाइलऑटपुटस्ट्रीम के साथ डिस्क पर बाइट्स लिखें और फ़ाइल .zip नाम दें। क्या आप निकाली गई फाइलें लिखना नहीं चाहते हैं? – morja

उत्तर

17

प्राप्त करने के लिए बाइट्स से सामग्री का उपयोग कर सकते

ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes)); 
ZipEntry entry = null; 
while ((entry = zipStream.getNextEntry()) != null) { 

    String entryName = entry.getName(); 

    FileOutputStream out = new FileOutputStream(entryName); 

    byte[] byteBuff = new byte[4096]; 
    int bytesRead = 0; 
    while ((bytesRead = zipStream.read(byteBuff)) != -1) 
    { 
     out.write(byteBuff, 0, bytesRead); 
    } 

    out.close(); 
    zipStream.closeEntry(); 
} 
zipStream.close(); 
+0

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

+0

आप ज़िपस्ट्रीम से बाइट्स पढ़ सकते हैं और फिर इसे फ़ाइलऑटपुटस्ट्रीम के साथ लिख सकते हैं। या सीधे इसे फिर से लिखो। मेरा अपडेट देखें। – morja

+0

बहुत बहुत धन्यवाद, यह सही काम करता है। – Mohan

5

आप शायद इस तरह कोड के लिए देख रहे हैं:

ZipInputStream z = new ZipInputStream(new ByteArrayInputStream(buffer)) 

अब आप getNextEntry()

के माध्यम से ज़िप फ़ाइल सामग्री प्राप्त कर सकते हैं
संबंधित मुद्दे