public static void main(String[] args) throws IOException {
String fileZip = "idml文件路径";
File destDir = new File("idml解压后文件位置");
// 字节流
byte[] buffer = new byte[1024];
// 解压idml文件
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
// 将解压后的文件放入到指定的文件夹下
File newFile = newFile(destDir, zipEntry);
if (!newFile.getParentFile().exists()){
newFile.getParentFile().mkdirs();
}
// 写入数据到文件中
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
// 文件夹名称
String fileDir = destDir.getAbsolutePath();
// 读取xml文件
try {
SAXReader reader = new SAXReader();
Document read = reader.read(new File(fileDir + "\designmap.xml"));
Element element = read.getRootElement();
Iterator it = element.elementIterator("Story");
while (it.hasNext()) {
Element next = it.next();
for (Attribute attribute : next.attributes()) {
getContent(fileDir + "\" + attribute.getValue());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
private static void getContent(String contentPath) throws DocumentException {
SAXReader reader = new SAXReader();
Document read = reader.read(new File(contentPath));
Element element = read.getRootElement();
Iterator elementIterator = element.elementIterator("Story");
while (elementIterator.hasNext()) {
Iterator ParagraphStyleRange = elementIterator.next().elementIterator("ParagraphStyleRange");
while (ParagraphStyleRange.hasNext()){
Iterator characterStyleRange = ParagraphStyleRange.next().elementIterator("CharacterStyleRange");
while (characterStyleRange.hasNext()){
Iterator content = characterStyleRange.next().elementIterator("Content");
while (content.hasNext()){
System.out.println(content.next().getText());
}
}
}
}
}
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}
return destFile;
}