I want to extract a rar file with junrar library. When I extract an xml file as InputStream
, dom4j throws an exception:
org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
How do I solve this?
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();