这是在Java中比较两个文档而没有任何复杂性和精确结果的最佳方法 [英] Which is the best way to Compare two documents in Java without any complexity and precise result

查看:35
本文介绍了这是在Java中比较两个文档而没有任何复杂性和精确结果的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要在java中进行比较的word文档.我尝试使用

I have two word documents which i am trying to compare in java . I tried using

md5哈希码

HashCode newFile = Files.asByteSource(newFileInput).hash(Hashing.md5());
HashCode oldFile = Files.asByteSource(oldFileInput).hash(Hashing.md5());

并同时使用

布尔值isEqual = FileUtils.contentEquals(oldFile,newFile);

即使内容相同,还是使用在线工具对内容进行了比较,无可比拟,上面两种方法中的哈希码仍然是MISMATCH.

Even though the contents are same ,compared the content using online tools and beyond compare, still the hashcode in both above method comes as MISMATCH.

有什么解决方案吗?或使用Java中的任何API比较任何文件类型的方法.我需要在两个单词文件之间进行深层比较,如空格,字体,内容.等.

any solutions? or way to compare any file type using any API in Java. i need to do deep compare between two word files as in for spaces,fonts , content. etc..

预期结果:两个文件都应匹配

Expected Result : Both file should match

推荐答案

对于我的问题,有很多建议和答案,谢谢.docx文件不匹配的原因在元数据信息中,每当我们创建doc/docx文件时,时间戳都会更改.虽然我尝试更改两个文件的时间戳(可访问,已修改和已创建),以使其相同并进行比较,但均无法解决.原因除了这些时间戳之外,还有一个名为Zip Modify Date的元信息,当我们看到文件属性时看不到该信息.我发现此时间戳是哈希码不匹配的原因之一.此外,由于zip时间戳,base64编码的字符串也有所不同.

There were multiple suggestions and answers to my question, thanks for that. The reasons for mismatch in docx file is there in the metadata info, everytime we create a doc/docx file, the timestamp changes. Though i tried to change the timestamp(accessed,modified and created) of both the files to make it same and compare, which didn't work out. The reason is apart from these time stamps, there is a meta info called Zip Modify Date, which isn't visible when we see the file properties. this timestamp i found as one of the reason there was mismatch in hashcode. Also, the base64 encoded strings was different because of the zip timestamp.

因此,我必须进行比较的选项是:

So, the options i had to do the comparison were :

1. converting the docx file to xml file
2. Zip the docx file, unzip it and iterate though all the xml files to find  the hashcode and compare the hascodes.(suggeted as of the answers)

"2"表示很好,但是需要很多次迭代,解压缩会创建很多文件夹.

"2" was good but it required lot of iterations and unzipping would create lot many folders.

"1"表示,是直接fwd,因为我使用外部lib->尝试过docx4j,它将docx转换为xml,然后我可以匹配该哈希码,它起作用了.

"1" , was straight fwd, as i tried it using external lib -> docx4j , which converted the docx to xml and then i could match the hashcode , it worked.

将DOCX转换为XML文件

Convert DOCX to XML file

由于我一直在寻找最简单而不是那么复杂的方式来比较Word文档的内容和样式,因此我不得不尝试不同的选择.

I had to try different options since i was looking for simplest and not so complex way to compare content and styles of the word document.

这篇关于这是在Java中比较两个文档而没有任何复杂性和精确结果的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆