2010-05-06 140 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内置的不支持所有的现代压缩级别/加密和其他的东西。

相关问题