2016-04-11 8 views
4

Ich habe byte [] zipFileAsByteArraylesen Zip-Datei Inhalt ohne in Java zu extrahieren

This zip file has rootDir --| 
          | --- Folder1 - first.txt 
          | --- Folder2 - second.txt 
          | --- PictureFolder - image.png 

Was ich brauche zwei txt-Dateien zu erhalten und lesen sie, ohne Dateien auf der Festplatte zu speichern. Mach es einfach in Erinnerung.

habe ich versucht, so etwas wie dieses:

ByteArrayInputStream bis = new ByteArrayInputStream(processZip); 
ZipInputStream zis = new ZipInputStream(bis); 

Auch separate Methode gehe ich muss haben Bild. Etwas wie folgt aus:

public byte[]image getImage(byte[] zipContent); 

Kann mir jemand helfen mit Idee oder gutes Beispiel dafür, wie das zu tun? Hier

+0

Ich denke, dass das, was Sie suchen finden Sie unter: http://stackoverflow.com/questions/15667125/read-content-from-files-which -are-inside-zip-Datei. Was das Bild betrifft, sollten Sie in der Lage sein, Folgendes zu tun: http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/. Um festzustellen, ob die getImage-Methode aufgerufen werden soll, überprüfen Sie die Erweiterung der Datei. – LoreV

Antwort

1

ist ein Beispiel:

public static void main(String[] args) throws IOException { 
    ZipFile zip = new ZipFile("C:\\Users\\mofh\\Desktop\\test.zip"); 


    for (Enumeration e = zip.entries(); e.hasMoreElements();) { 
     ZipEntry entry = (ZipEntry) e.nextElement(); 
     if (!entry.isDirectory()) { 
      if (FilenameUtils.getExtension(entry.getName()).equals("png")) { 
       byte[] image = getImage(zip.getInputStream(entry)); 
       //do your thing 
      } else if (FilenameUtils.getExtension(entry.getName()).equals("txt")) { 
       StringBuilder out = getTxtFiles(zip.getInputStream(entry)); 
       //do your thing 
      } 
     } 
    } 


} 

private static StringBuilder getTxtFiles(InputStream in) { 
    StringBuilder out = new StringBuilder(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
    String line; 
    try { 
     while ((line = reader.readLine()) != null) { 
      out.append(line); 
     } 
    } catch (IOException e) { 
     // do something, probably not a text file 
     e.printStackTrace(); 
    } 
    return out; 
} 

private static byte[] getImage(InputStream in) { 
    try { 
     BufferedImage image = ImageIO.read(in); //just checking if the InputStream belongs in fact to an image 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ImageIO.write(image, "png", baos); 
     return baos.toByteArray(); 
    } catch (IOException e) { 
     // do something, it is not a image 
     e.printStackTrace(); 
    } 
    return null; 
} 

Denken Sie daran, wenn ich einen String bin Überprüfung der möglichen Typen diferentiate und dies ist fehleranfällig. Nichts hält mich davon ab, einen anderen Dateityp mit einer erwarteten Erweiterung zu senden.

+0

Ich habe keine ZipFile zip = neue ZipFile ("C: \\ Benutzer \\ mofh \\ Desktop \\ test.zip"); mein Eintrag ist ZipInputStream zis = neuer ZipInputStream (bis); – TNN

+0

Einfach verwenden Sie es dann ... Wenn Sie das Byte [] für die ZIP haben, nur 'ZipFile Zip = neue ZipInputStream (neue ByteArrayInputStream (ProzessZip))'. – dambros

1

Sie können wie etwas tun:

public static void main(String args[]) throws Exception 
{ 
    //bis, zis as you have 
    try{ 
     ZipEntry file; 
     while((file = zis.getNextEntry())!=null) // get next file and continue only if file is not null 
     { 
      byte b[] = new byte[(int)file.getSize()]; // create array to read. 
      zis.read(b); // read bytes in b 
      if(file.getName().endsWith(".txt")){ 
       // read files. You have data in `b` 
      }else if(file.getName().endsWith(".png")){ 
       // process image 
      } 
     } 
    } 
    finally{ 
     zis.close(); 
    } 
} 
+0

file.getSize() gibt -1 zurück; – TNN