3

docx ファイルの inputStream があり、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 の出力は、ある時点で「word/document.xml」になります。この document.xml をストリームとして渡す必要があります。.getInputStream の呼び出しでこれを簡単に渡すことができる ZipFile メソッドとは異なり、この 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 があるかどうか疑問に思っていますか?

4

1 に答える 1

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

さらに、組み込みの zip ライブラリに加えて、他の zip ライブラリを検討することもできます。私の知る限り、組み込みのものは、すべての最新の圧縮レベル/暗号化およびその他のものをサポートしているわけではありません。

于 2010-05-06T09:26:41.090 に答える