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 8.9KB

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