2010-05-06 93 views
3

我有一個docx文件的inputStream,我需要獲取位於docx內的document.xml。使用ZipInputStream從docx文件獲取document.xml

我使用ZipInputStream看我流,我的代碼是一樣的東西

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()); 
     } 
    } 

正如你可以看到zipEntry.getName輸出當屬「字/ document.xml中」在某些時候。我需要將這個document.xml作爲一個流傳遞,而不像ZipFile方法那樣,你可以很容易地通過調用.getInputStream來傳遞這個方法,我想知道我該怎麼做這個docXFile?

由於提前, 米納克什

@Update: 我發現這個解決方案輸出:

 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; 
      } 
     } 
    } 

我想知道是否有一些基本的API輸出流轉換爲輸入流?

回答

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內置的不支持所有的現代壓縮級別/加密和其他的東西。

相關問題