一、ofd格式介绍

国家发布过一份关于ofd编码格式的资料,本来我想传上去的发现资源重复了,你们可以找找看,没有的话留个邮箱,我看到会发给你们的

ofd本质上其实是一个压缩文件,咱们把他当做一个压缩包来处理就好了,思路是先解压,对解压后的文件进行解析处理,解压后是xml文件,java有很多处理xml的类,这里我推荐dom4j,原因是相对来说功能全、速度快,处理完后再进行压缩,保存为ofd格式即可

ofd的阅读器我也有,只是是公司的,不方便共享了,大家可以找网上在线阅读器

二、xml处理工具类

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;/**  DOM4J类DOM4J定义了几个Java类。以下是最常见的类:Document - 表示整个XML文档。文档Document对象是通常被称为DOM树。Element - 表示一个XML元素。 Element对象有方法来操作其子元素,它的文本,属性和名称空间。Attribute - 表示元素的属性。属性有方法来获取和设置属性的值。它有父节点和属性类型。Node - 代表元素,属性或处理指令常见DOM4J的方法当使用DOM4J,还有经常用到的几种方法:SAXReader.read(xmlSource)() - 构建XML源的DOM4J文档。Document.getRootElement() - 得到的XML的根元素。Element.node(index) - 获得在元素特定索引XML节点。Element.attributes() - 获取一个元素的所有属性。Node.valueOf(@Name) - 得到元件的给定名称的属性的值。* * */
public class OfdXmlUtil {public static String ids = "";public static String getAttributeIdByPath(String path,String attribute) throws Exception {List<XmlEntity> xmlList = readXmlByPath(path);int mediaId = 0;for (XmlEntity xml : xmlList) {if (xml.getNode().equals(attribute) && xml.getAttributes().get("ID") != null) {mediaId = mediaId>Integer.parseInt(xml.getAttributes().get("ID"))?mediaId:Integer.parseInt(xml.getAttributes().get("ID"));}}String id=String.valueOf(mediaId+1);return id;}public static String getId(Element node, String element) {if(node.getName().equals(element)) {ids = node.valueOf("id");}// 当前节点下面子节点迭代器Iterator<Element> it = node.elementIterator();// 递归遍历while (it.hasNext()) {// 获取某个子节点对象Element e = it.next();// 对子节点进行遍历getId(e, element);}return ids;}public static String getLastIdByElement(String path, String element) throws Exception {File file = new File(path);// 创建saxReader对象SAXReader reader = new SAXReader();Document document = reader.read(file);// 获取根节点元素对象Element node = document.getRootElement();String str = getId(node, element);return str;}/*** * @param path* @return* @throws Exception*/public static List<XmlEntity> readXmlByPath(String path) throws Exception {File file = new File(path);List<XmlEntity> xmlEntities = readXmlByFile(file);return xmlEntities;}/*** @param file* @return* @throws Exception*/public static List<XmlEntity> readXmlByFile(File file) throws Exception {// 创建saxReader对象SAXReader reader = new SAXReader();Document document = reader.read(file);// 获取根节点元素对象Element node = document.getRootElement();List<XmlEntity> xmlEntities = new ArrayList<XmlEntity>();listNodes(node, xmlEntities);return xmlEntities;}/*** 遍历当前节点元素下面的所有(元素的)子节点* * @param node*/public static void listNodes(Element node, List<XmlEntity> xmlEntities) {XmlEntity xmlEntity = new XmlEntity();xmlEntity.setNode(node.getName());// 获取当前节点的所有属性节点List<Attribute> list = node.attributes();// 遍历属性节点Map<String, String> attributeMap = list.stream().collect(Collectors.toMap(Attribute::getName, Attribute::getValue));xmlEntity.setAttributes(attributeMap);if (!(node.getTextTrim().equals(""))) {xmlEntity.setContent(node.getText());}xmlEntities.add(xmlEntity);// 当前节点下面子节点迭代器Iterator<Element> it = node.elementIterator();// 递归遍历while (it.hasNext()) {// 获取某个子节点对象Element e = it.next();// 对子节点进行遍历listNodes(e, xmlEntities);}}// 递归获取当前节点下最后一个节点的元素public static Element getLastElement(Element node) throws Exception {Element node2 = getNextElement(node, 0);if (node2.elements().size() > 0) {node2 = getNextElement(node2, 0);}return node2;}// 获取当前节点下第i个节点的元素public static Element getNextElement(Element e, int i) {e = e.elements().get(i);return e;}/*** 根据节点名获取节点* * @param node*/public static List<Element> getAllEle(Element node, List<Element> elems) {elems.add(node);List<Element> listElement = node.elements();// 所有一级子节点的listfor (Element e : listElement) {// 遍历所有一级子节点getAllEle(e, elems);// 递归}return elems;}/*** 修改、增加xml属性值 元素无重复 不能修改根元素 String elem 需要修改的标签名, String key属性名, String* value修改后的值* * @return* @throws Exception*/public static boolean edit(Document doc, String elem, String key, String value, String outUrl) throws Exception {Element element = doc.getRootElement();// 当前节点下面子节点迭代器Element tmpEle = doc.getRootElement();List<Element> elems = new ArrayList<Element>();List<Element> listElement = getAllEle(element, elems);if (listElement.size() != 0) {for (Element e : listElement) {// 遍历所有一级子节点if (e.getName().equals(elem)) {tmpEle = e;}}}if (tmpEle.isRootElement()) {return false;} else {// 2.通过增加同名属性的方法,修改属性值tmpEle.addAttribute(key, value); // key相同,覆盖;不存在key,则添加// 指定文件输出的位置writer(doc, outUrl);return true;}}/*** 把document对象写入新的文件* * @param document* @throws Exception*/public static void writer(Document document, String url) throws Exception {// 紧凑的格式// OutputFormat format = OutputFormat.createCompactFormat();// 排版缩进的格式OutputFormat format = OutputFormat.createPrettyPrint();// 设置编码format.setEncoding("UTF-8");// 创建XMLWriter对象,指定了写出文件及编码格式// XMLWriter writer = new XMLWriter(new FileWriter(new// File("src//a.xml")),format);XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(url)), "UTF-8"), format);// 写入writer.setEscapeText(false);writer.write(document);// 立即写入writer.flush();// 关闭操作writer.close();}public static void createSignaturesXml(String signs,String signTmp) throws Exception {Document doc1 = new SAXReader().read(new File(signs + "/Signatures.xml"));Element Signatures = doc1.getRootElement();Element Signature = Signatures.addElement("ofd:Signature");Signature.addAttribute("ID", "1");Signature.addAttribute("Type", "Seal");Signature.addAttribute("BaseLoc", signTmp.substring(signTmp.length()-6) + "/Signature.xml");OfdXmlUtil.writer(doc1, signs + "/Signatures.xml");}
}

三、压缩解压缩处理工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class ZipUtil {private ZipUtil() {}public static void doCompress(String srcFile, String zipFile) throws Exception {String rootdDirectory = srcFile.split("/")[srcFile.split("/").length - 1]; // 获取根目录名称doCompress(new File(srcFile), new File(zipFile), rootdDirectory);}/*** 文件压缩* * @param srcFile 目录或者单个文件* @param zipFile 压缩后的ZIP文件*/public static void doCompress(File srcFile, File zipFile, String rootdDirectory) throws Exception {ZipOutputStream out = null;try {out = new ZipOutputStream(new FileOutputStream(zipFile));doCompress(srcFile, out, rootdDirectory);} catch (Exception e) {throw e;} finally {out.close();// 记得关闭资源}}public static void doCompress(File file, ZipOutputStream out, String rootdDirectory) throws IOException {doCompress(file, out, "", rootdDirectory);}public static void doCompress(File inFile, ZipOutputStream out, String dir, String rootdDirectory)throws IOException {if (inFile.isDirectory()) {File[] files = inFile.listFiles();if (files != null && files.length > 0) {for (File file : files) {String name = inFile.getName();if (!"".equals(dir)) {name = dir + "/" + name;}ZipUtil.doCompress(file, out, name, rootdDirectory);}}} else {// 将根目录干掉,否则无法打开OFD文件dir = dir.replaceAll(rootdDirectory, "");ZipUtil.doZip(inFile, out, dir);}}public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {String entryName = null;if (!"".equals(dir)) {dir = dir.substring(1, dir.length());entryName = dir + "/" + inFile.getName();} else {entryName = inFile.getName();}ZipEntry entry = new ZipEntry(entryName);out.putNextEntry(entry);int len = 0;byte[] buffer = new byte[1024 * 1024];FileInputStream fis = new FileInputStream(inFile);while ((len = fis.read(buffer)) > 0) {out.write(buffer, 0, len);out.flush();}out.closeEntry();fis.close();}
}
package com.koalii.oes.util;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;/*** @项目名称:meter* @类名称:ZipTools * @类描述:采用zip压缩文件* @创建人:meter* @创建时间:2018年5月4日 上午10:25:09* @版权:Copyright@2018.All Rights Reserved* @version v1.0*/
public class UnzipUtil {/*** 读写zip文件*/private static void write(InputStream inputStream,OutputStream outStream) throws IOException{byte[] data=new byte[4096];int length=0;while((length=inputStream.read(data)) != -1){outStream.write(data,0,length);}outStream.flush();//刷新输出流inputStream.close();//关闭输入流}public static void unzip(String zipFile, String destDir) throws IOException {unzip(new File(zipFile), new File(destDir));}/*** 解压文件n */public static void unzip(File zipFile, File destDir) throws IOException {ZipFile zipOutFile = new ZipFile(zipFile);Enumeration<? extends ZipEntry> entries = zipOutFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();if(entry.isDirectory()){File tempFile = new File(destDir.getAbsolutePath()+File.separator+entry.getName());if (!tempFile.exists()) {tempFile.mkdirs();}}else{File tempFile=new File(destDir.getAbsolutePath()+File.separator+entry.getName());checkParentDir(tempFile);FileOutputStream fileOutStream=new FileOutputStream(tempFile);BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);write(zipOutFile.getInputStream(entry),bufferOutStream);bufferOutStream.close();fileOutStream.close();}}zipOutFile.close();//关闭zip文件}/*** 验证父目录是否存在,否则创建*/private static void checkParentDir(File file){if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}}}

基于java处理ofd格式文件相关推荐

  1. java导入csv分隔符_基于Java的CSV格式文件处理(excel逗号分隔符文件) | 学步园...

    导出 用流写出即可.导出csv文件. /** * 获取csv 文件中的内容 * @param path csv的文件位置 * @return 内容集合 * @throws Exception */ p ...

  2. 基于Java的CSV格式文件处理(excel逗号分隔符文件)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 导出 用 ...

  3. java 导出csv 格式,java导出csv格式文件的方法

    这篇文章主要为大家详细介绍了java导出csv格式文件的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 导出csv格式文件的本质是导出以逗号为分隔的文本数据 imp ...

  4. PHP 把ofd格式文件转PDF,打开OFD格式文件及将OFD格式文件转换成PDF文件

    今天收到一张浙江开具的ofd格式发票,便在网上找相关的软件来打开方便打印出来给财务做账,但是找了一大圈没有解决方案,有些收费的解决方法,大部分所谓的OFD软件是骗人的,下载安装后根本打不开OFD格式的 ...

  5. OFD格式文件怎么转图片?分享两种OFD转图片方法

    OFD格式的文件怎么转换成图片呢?大家在办公中如果收到对方发来的OFD格式文件时,不知道怎么打开文件,从而影响我们的正常工作进度.实际上,这种格式的文件一般是需要使用相关的阅读工具才能够打开,但是我们 ...

  6. ofd格式文件转换成pdf格式的方法

    ofd格式文件很多人还比较陌生,很多人接收到文件都不知如何打开阅读,把文件发给对方,还需要对方安装个专门的阅读软件,我们还有另一个办法,就是将OFD文件转换为PDF格式文件,然后把PDF格式文件发给对 ...

  7. Java 导出DBF格式文件(标题中文)

    本文章不是原创,但是我抄袭的那个连接我找不到了,还是有些自己写的东西,见谅! 目录 前言 一.DBF是什么? 二.使用步骤 1.引入库 2.正文 总结 前言 写这篇文章是有原因的,我前两天有一个需求, ...

  8. Java读写CSV格式文件(opencsv)

    使用opencsv(opencsv-2.3.jar),下载地址:http://download.csdn.net/detail/jinwufeiyang/9664120 一,读取CSV格式文件: im ...

  9. OFD格式文件怎么转PDF格式?分享一个轻松转换小妙招

    怎么把OFD格式的文件转换成PDF格式呢?大家都知道,文件的格式分为很多种,每一种的格式用途都不一样,PDF格式的文件是我们日常办公中比较常见到的文件,用来发送和储存文件都很方便,实际上还有一种文件格 ...

最新文章

  1. TensorFlow 1.7.0正式发布,Bug修复和改进内容都在这里了
  2. 博士买房后发现被坑,于是写万字论文维权,网友:维权界的天花板...
  3. 【算法】均匀的生成圆内的随机点
  4. 网页按钮跳转位置_RPA工具BizRobo!之运用网页数据处理
  5. java-Integer的自动装箱与拆箱
  6. Java 8 - Interface Default Method接口默认方法
  7. [线段树][树上差分] Jzoj P3397 雨天的尾巴
  8. Linux IO模型
  9. pyplot 画多个图时搅合到了一起_这些认数字游戏,宝宝最喜欢,家长可以和宝宝一起玩...
  10. rpm linux gcc安装目录,Linux环境下通过rpm安装gcc的顺序
  11. 《Hadoop实战(第2版)》迷你书
  12. CCNA学习指南中文第七版-1
  13. 阿里P4 - P14技能要求及对应薪资曝光
  14. Linux pthread详解
  15. element-ui 删除input框尾部默认图标和获取焦点边框高亮问题
  16. C++中的防卫式声明
  17. 《C Primer Plus》学习笔记—第9章
  18. 苹果计算机 win10,苹果怎么装win10苹果装win10详细教程【图文】
  19. URLOS实战入门—制作LAP网站环境
  20. IOS企业签名的APP怎么做分发?

热门文章

  1. uniapp使用阿里云多色图标
  2. 【极简版GH60】【GH60剖析】【七】分析源代码
  3. 滥用 ESI 详解(上)
  4. 在线办公实例:我如何在实际工作中使用超级表格
  5. docker-compose搭建prometheus+granafa+alertmanager+dingtalk
  6. 汽车电子行业常见工具方法汇总
  7. Redist-Java 有序列表操作
  8. 计算机的教育领域的应用研究,浅析计算机科学技术在现代教育中的应用研究
  9. easymock平台语法初体验
  10. 用python把视频分解成图片