You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntryUtils.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import java.io.EOFException;
  17. import java.io.IOException;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.StreamSupport;
  24. import org.apache.poi.hpsf.NoPropertySetStreamException;
  25. import org.apache.poi.hpsf.PropertySet;
  26. import org.apache.poi.hpsf.PropertySetFactory;
  27. import org.apache.poi.util.Internal;
  28. @Internal
  29. public final class EntryUtils {
  30. private EntryUtils() {}
  31. /**
  32. * Copies an Entry into a target POIFS directory, recursively
  33. */
  34. @Internal
  35. public static void copyNodeRecursively( Entry entry, DirectoryEntry target )
  36. throws IOException {
  37. if ( entry.isDirectoryEntry() ) {
  38. DirectoryEntry dirEntry = (DirectoryEntry)entry;
  39. DirectoryEntry newTarget = target.createDirectory( entry.getName() );
  40. newTarget.setStorageClsid( dirEntry.getStorageClsid() );
  41. Iterator<Entry> entries = dirEntry.getEntries();
  42. while ( entries.hasNext() ) {
  43. copyNodeRecursively( entries.next(), newTarget );
  44. }
  45. } else {
  46. DocumentEntry dentry = (DocumentEntry) entry;
  47. DocumentInputStream dstream = new DocumentInputStream( dentry );
  48. target.createDocument( dentry.getName(), dstream );
  49. dstream.close();
  50. }
  51. }
  52. /**
  53. * Copies all the nodes from one POIFS Directory to another
  54. *
  55. * @param sourceRoot
  56. * is the source Directory to copy from
  57. * @param targetRoot
  58. * is the target Directory to copy to
  59. */
  60. public static void copyNodes(DirectoryEntry sourceRoot, DirectoryEntry targetRoot)
  61. throws IOException {
  62. for (Entry entry : sourceRoot) {
  63. copyNodeRecursively( entry, targetRoot );
  64. }
  65. }
  66. /**
  67. * Copies all nodes from one POIFS to the other
  68. *
  69. * @param source
  70. * is the source POIFS to copy from
  71. * @param target
  72. * is the target POIFS to copy to
  73. */
  74. public static void copyNodes(POIFSFileSystem source, POIFSFileSystem target )
  75. throws IOException {
  76. copyNodes( source.getRoot(), target.getRoot() );
  77. }
  78. /**
  79. * Copies nodes from one POIFS to the other, minus the excepts.
  80. * This delegates the filtering work to {@link FilteringDirectoryNode},
  81. * so excepts can be of the form "NodeToExclude" or
  82. * "FilteringDirectory/ExcludedChildNode"
  83. *
  84. * @param source is the source POIFS to copy from
  85. * @param target is the target POIFS to copy to
  86. * @param excepts is a list of Entry Names to be excluded from the copy
  87. */
  88. public static void copyNodes(POIFSFileSystem source, POIFSFileSystem target, List<String> excepts )
  89. throws IOException {
  90. copyNodes(
  91. new FilteringDirectoryNode(source.getRoot(), excepts),
  92. new FilteringDirectoryNode(target.getRoot(), excepts)
  93. );
  94. }
  95. /**
  96. * Checks to see if the two Directories hold the same contents.
  97. * For this to be true ...
  98. * <ul>
  99. * <li>they must have entries with the same names</li>
  100. * <li>no entries in one but not the other</li>
  101. * <li>the size+contents of each entry must match</li>
  102. * <li>the storage classid of the directories must match</li>
  103. * </ul>
  104. * To exclude certain parts of the Directory from being checked,
  105. * use a {@link FilteringDirectoryNode}
  106. */
  107. public static boolean areDirectoriesIdentical(DirectoryEntry dirA, DirectoryEntry dirB) {
  108. return new DirectoryDelegate(dirA).equals(new DirectoryDelegate(dirB));
  109. }
  110. /**
  111. * Compares two {@link DocumentEntry} instances of a POI file system.
  112. * Documents that are not property set streams must be bitwise identical.
  113. * Property set streams must be logically equal.<p>
  114. *
  115. * (Their parent directories are not checked)
  116. */
  117. @SuppressWarnings("WeakerAccess")
  118. public static boolean areDocumentsIdentical(DocumentEntry docA, DocumentEntry docB)
  119. throws IOException {
  120. try {
  121. return new DocumentDelegate(docA).equals(new DocumentDelegate(docB));
  122. } catch (RuntimeException e) {
  123. if (e.getCause() instanceof IOException) {
  124. throw (IOException)e.getCause();
  125. } else {
  126. throw e;
  127. }
  128. }
  129. }
  130. private interface POIDelegate {
  131. }
  132. private static class DirectoryDelegate implements POIDelegate {
  133. final DirectoryEntry dir;
  134. DirectoryDelegate(DirectoryEntry dir) {
  135. this.dir = dir;
  136. }
  137. private Map<String,POIDelegate> entries() {
  138. return StreamSupport.stream(dir.spliterator(), false)
  139. .collect(Collectors.toMap(Entry::getName, DirectoryDelegate::toDelegate));
  140. }
  141. private static POIDelegate toDelegate(Entry entry) {
  142. return (entry.isDirectoryEntry())
  143. ? new DirectoryDelegate((DirectoryEntry)entry)
  144. : new DocumentDelegate((DocumentEntry)entry);
  145. }
  146. @Override
  147. public int hashCode() {
  148. return dir.getName().hashCode();
  149. }
  150. @Override
  151. public boolean equals(Object other) {
  152. if (!(other instanceof DirectoryDelegate)) {
  153. return false;
  154. }
  155. DirectoryDelegate dd = (DirectoryDelegate)other;
  156. if (this == dd) {
  157. return true;
  158. }
  159. // First, check names
  160. if (!Objects.equals(dir.getName(),dd.dir.getName())) {
  161. return false;
  162. }
  163. // Next up, check they have the same number of children
  164. if (dir.getEntryCount() != dd.dir.getEntryCount()) {
  165. return false;
  166. }
  167. if (!dir.getStorageClsid().equals(dd.dir.getStorageClsid())) {
  168. return false;
  169. }
  170. return entries().equals(dd.entries());
  171. }
  172. }
  173. private static class DocumentDelegate implements POIDelegate {
  174. final DocumentEntry doc;
  175. DocumentDelegate(DocumentEntry doc) {
  176. this.doc = doc;
  177. }
  178. @Override
  179. public int hashCode() {
  180. return doc.getName().hashCode();
  181. }
  182. @Override
  183. public boolean equals(Object other) {
  184. if (!(other instanceof DocumentDelegate)) {
  185. return false;
  186. }
  187. DocumentDelegate dd = (DocumentDelegate)other;
  188. if (this == dd) {
  189. return true;
  190. }
  191. if (!Objects.equals(doc.getName(), dd.doc.getName())) {
  192. // Names don't match, not the same
  193. return false;
  194. }
  195. try (DocumentInputStream inpA = new DocumentInputStream(doc);
  196. DocumentInputStream inpB = new DocumentInputStream(dd.doc)) {
  197. if (PropertySet.isPropertySetStream(inpA) &&
  198. PropertySet.isPropertySetStream(inpB)) {
  199. final PropertySet ps1 = PropertySetFactory.create(inpA);
  200. final PropertySet ps2 = PropertySetFactory.create(inpB);
  201. return ps1.equals(ps2);
  202. } else {
  203. return isEqual(inpA, inpB);
  204. }
  205. } catch (NoPropertySetStreamException | IOException ex) {
  206. throw new RuntimeException(ex);
  207. }
  208. }
  209. private static boolean isEqual(DocumentInputStream i1, DocumentInputStream i2)
  210. throws IOException {
  211. final byte[] buf1 = new byte[4*1024];
  212. final byte[] buf2 = new byte[4*1024];
  213. try {
  214. int len;
  215. while ((len = i1.read(buf1)) > 0) {
  216. i2.readFully(buf2,0,len);
  217. for(int i=0;i<len;i++) {
  218. if (buf1[i] != buf2[i]) {
  219. return false;
  220. }
  221. }
  222. }
  223. // is the end of the second file also.
  224. return i2.read() < 0;
  225. } catch(EOFException | RuntimeException ioe) {
  226. return false;
  227. }
  228. }
  229. }
  230. }