2010-05-06 1 views
3

docx 파일의 inputStream이 있으며 docx 내부에있는 document.xml을 가져와야합니다. 당신이 zipEntry.getName의 출력이 어떤 점에서 "단어/document.xml"로 오는 볼 수 있듯이 내 스트림 내 코드를 읽을 ZipInputStream를 사용하고ZipInputStream을 사용하여 docx 파일에서 document.xml 가져 오기

ZipInputStream docXFile = new ZipInputStream(fileName); 
    ZipEntry zipEntry; 
    while ((zipEntry = docXFile.getNextEntry()) != null) { 
     if(zipEntry.getName().equals("word/document.xml")) 
     { 
      System.out.println(" --> zip Entry is "+zipEntry.getName()); 
     } 
    } 

같은 것입니다. 이 document.xml을 스트림으로 전달해야하며 .getInputStream을 호출 할 때 쉽게 전달할 수있는 ZipFile 메서드와 달리이 docXFile을 어떻게 수행 할 수 있을지 궁금합니다. 사전에

감사합니다, Meenakshi

@Update : 입력 스트림이 출력 스트림을 변환하는 몇 가지 기본 API가 있는지 궁금하고

 ZipInputStream docXFile = new ZipInputStream(fileName); 
    ZipEntry zipEntry; 
    OutputStream out; 

    while ((zipEntry = docXFile.getNextEntry()) != null) { 
     if(zipEntry.toString().equals("word/document.xml")) 
     { 
      System.out.println(" --> zip Entry is "+zipEntry.getName()); 
      byte[] buffer = new byte[1024 * 4]; 
      long count = 0; 
      int n = 0; 
      long size = zipEntry.getSize(); 
      out = System.out; 

      while (-1 != (n = docXFile.read(buffer)) && count < size) { 
       out.write(buffer, 0, n); 
       count += n; 
      } 
     } 
    } 

:이 솔루션의 출력을 발견 ? 이 같은

답변

2

뭔가 (테스트되지 않음) 작동합니다 :

ZipFile zip = new ZipFile(filename) 
Enumeration entries = zip.entries(); 
while (entries.hasMoreElements()) { 
    ZipEntry entry = (ZipEntry)entries.nextElement(); 

    if (!entry.getName().equals("word/document.xml")) continue; 

    InputStream in = zip.getInputStream(entry); 
    handleWordDocument(in); 
} 

은 또한 당신이 하나의 내장 외에 다른 압축 라이브러리를 한 번 봐 걸릴 수 있습니다. AFAIK 내장 된 모든 압축 수준/암호화 및 기타 물건을 지원하지 않습니다.