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<Element> 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();
}
}
/**
* 读取指定的xml文件
* @param contentPath xml文件路径
* @throws DocumentException
*/
private static void getContent(String contentPath) throws DocumentException {
SAXReader reader = new SAXReader();
Document read = reader.read(new File(contentPath));
Element element = read.getRootElement();
Iterator<Element> elementIterator = element.elementIterator("Story");
while (elementIterator.hasNext()) {
Iterator<Element> ParagraphStyleRange = elementIterator.next().elementIterator("ParagraphStyleRange");
while (ParagraphStyleRange.hasNext()){
Iterator<Element> characterStyleRange = ParagraphStyleRange.next().elementIterator("CharacterStyleRange");
while (characterStyleRange.hasNext()){
Iterator<Element> content = characterStyleRange.next().elementIterator("Content");
while (content.hasNext()){
System.out.println(content.next().getText());
}
}
}
}
}
/**
* 将解压后的文件放到指定的文件夹中
* @param destinationDir 文件解压后所放的位置
* @param zipEntry 解压后的文件
* @return
* @throws IOException
*/
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;
}
Java 读取 idml 文件中的内容
最新推荐文章于 2024-09-27 00:45:01 发布
关键词由CSDN通过智能技术生成