0

ZipInputStream から xml ファイルとその他のコンテンツを抽出し、zipinputstream を解析する xml からオブジェクトを作成しようとしています。ただし、inputStream を読み取る while ループがない場合、次のコードまたは Stream Closed に対して、早すぎるファイル終了例外が発生します。私が理解していることから、 ZipInputStream.getNextEntry は次のエントリの入力ストリームを取得します。

また、実際の一時ファイルを作成して実行し、入力ストリームを渡すと(コメントされたコードのように)、正常に処理されますが、私の場合はディスクに書き込むことができません-したがって、これはすべて-メモリー。mycode のどこが間違っているのか、それを修正するにはどうすればよいか教えてもらえますか?

ZipEntry entry; 
Map<String, byte[]> otherElements = new HashMap<String, byte[]>();
        entry =((ZipInputStream)inputStream).getNextEntry();
        while (entry != null) {
            logger.debug("entry: " + entry.getName() + ", " + entry.getSize());
            System.out.println(entry.getName() + " - " + entry.getSize());

            if (entry.getName().equalsIgnoreCase("Document.xml")) {
                /*File file = new File("C:\\tmp.xml");
                FileOutputStream fos = new FileOutputStream();
                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = inputStream.read(bytes)) != -1) {
                    fos.write(bytes, 0, read);
                }
                InputStream fis = new FileInputStream();*/

                while(inputStream.available()>0){
                    inputStream.read();
                }

                myOutput = buildMyOutput((ZipInputStream)inputStream);
                //fos.close();
                //fis.close();

// method that takes the input and creates the java object
private MyObject buildMyOutput(InputStream xmlStream) throws Exception {

    // build my objects
    XStream xstream = ConvertUtil.getXStream();
    xstream.processAnnotations(MyObject.class);
    MyObject myOutput = (MyObject) xstream.fromXML(xmlStream);
    return myOutput;
}

4

1 に答える 1

0

問題が何であるかを発見しました: XStream はエンコーディング情報を失い、null に設定していました [codehaus の JIRA リンク][1]https://jira.codehaus.org/browse/XSTR-441

この入力に基づいて、入力ストリームを UTF-8 としてコピーし、UTF-8 エンコード形式で buildMyOutput メソッドを呼び出しました。次のスニペットを削除しました -

 while(inputStream.available()>0){
         inputStream.read();
 }

buildMyOutput 呼び出しを次のように変更しました

String xml = IOUtils.toString(zipInputStream, "UTF-8");
    InputStream documentXmlIS = IOUtils.toInputStream(xml);
    map = buildMyOutput(documentXmlIS);
于 2012-06-26T23:10:21.913 に答える