如何在junrar中将xml文件作为InputStream?

时间:2017-07-16 19:41:03

标签: java dom4j

我想用junrar库提取rar文件。当我将xml文件解压缩为InputStream时,dom4j会抛出异常:

  

org.dom4j.DocumentException:文档第1行出错:内容为   不允许在prolog中。嵌套异常:不允许使用内容   序言

我该如何解决这个问题?

 String filename = importFile.getAbsolutePath();
        File f = new File(filename);
        Archive a = null;
        try {
            a = new Archive(f);
        } catch (RarException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (a != null) {
            a.getMainHeader().print();
            FileHeader fh = a.nextFileHeader();
            while (fh != null) {
                try {
                                    if(fh.isDirectory()){
                                        continue;
                                    }
                                    else if(fh.getFileNameString().endsWith(".jml")||fh.getFileNameString().endsWith(".xml")){

                                        InputStream in=a.getInputStream(fh);
;
                                        List<Card> xml = convertXml(in);
                                        if(xml!=null){
                                         xmlPath = createNewXml(xml,fh.getFileNameString());
                                        }
                                        ZipEntry newZe=new ZipEntry(fh.getFileNameString().substring(0,fh.getFileNameString().lastIndexOf("."))+"xml");            
                                        zos.putNextEntry(newZe);
                                        InputStream inXml = new FileInputStream(xmlPath);
                                           int len;
                                           while ((len = inXml.read(buffer))>0 ) {
                                               zos.write(buffer, 0, len);
                                           }
                                           in.close();
                                           zos.closeEntry();
                                        }
                                    else if(fh.getFileNameString().startsWith("images")){
                                         ZipEntry newZe=new ZipEntry(fh.getFileNameString());
                                         zos.putNextEntry(newZe);
                                           InputStream inFile=a.getInputStream(fh);
                                            int len;
                                              while ((len = inFile.read(buffer))>0 ) {
                                                zos.write(buffer, 0, len);
                                            }

                                            inFile.close();
                                            zos.closeEntry();

                                    }
                                }catch(RarException | IOException | DocumentException e){
                                    e.printStackTrace();
                                }
                fh = a.nextFileHeader();
            }

        }

                 zos.close();

0 个答案:

没有答案