2016-08-08 35 views
0

Ich habe einen Eingangsstrom einer Zip-Datei, die ich von einer Web-Service-Antwort erhalten werde. Diese ZIP-Datei enthält eine XML-Datei. Ich muss den Eingabestrom dieser XML-Datei aus dem Eingabedatenstrom der ZIP-Datei extrahieren. Bitte helfen. Ich habe den folgenden Code ausprobiert. Aber kein Erfolg.Wie erhält man den Inhalt des Inhalts einer Zip-Datei?

DataHandler dataHandler = odDocClient.getUniqueDoc(null, null); 
inputStream = dataHandler.getInputStream(); 
ZipInputStream zipInputStream = new ZipInputStream(inputStream); 
ZipEntry zipEntry=zipInputStream.getNextEntry();    
File tempZipFile= new File("D:\\WorkSpace\\Invoicing\\ZIP\\tempZip1.zip"); 
FileOutputStream fileOutputStream= new FileOutputStream(tempZipFile); 
IOUtils.copy(inputStream, fileOutputStream); 
inputStream.close(); 
inputStream.close(); 
fileOutputStream.close(); 
ZipFile zipFile = new ZipFile(tempZipFile); 
inputStream=zipFile.getInputStream(zipEntry); 

Dieser Eingangsstrom wird weiter verwendet.

Mit dem obigen Code bekomme ich die folgende Ausnahme.

java.util.zip.ZipException: invalid END header (bad central directory offset) 
    at java.util.zip.ZipFile.open(Native Method) 
    at java.util.zip.ZipFile.<init>(ZipFile.java:215) 
    at java.util.zip.ZipFile.<init>(ZipFile.java:145) 
    at java.util.zip.ZipFile.<init>(ZipFile.java:159) 

Antwort

0

Können Sie dies versuchen, hier Variable „ist“ ist Ihr INPUTSTREAM- Für Zeit Bohne warf ich lange in Array-Größe in int aber in Ihrem Code haben Sie Strom richtig zu lesen.

ZipInputStream zis = new ZipInputStream(is);   
ZipEntry ze; 
    while((ze=zis.getNextEntry()) != null){ 
     if(!ze.isDirectory() && ze.getName().endsWith(".xml")) {     
      byte[] arr = new byte[(int)ze.getSize()]; // an array of size in zip entry 
      zis.read(arr); 
      // Your contains is here 
      System.out.println(new String(arr)); 
     } 
}