Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EntryUtils.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 ...
  99. * <ul>
  100. * <li>they must have entries with the same names</li>
  101. * <li>no entries in one but not the other</li>
  102. * <li>the size+contents of each entry must match</li>
  103. * <li>the storage classid of the directories must match</li>
  104. * </ul>
  105. * To exclude certain parts of the Directory from being checked,
  106. * use a {@link FilteringDirectoryNode}
  107. */
  108. public static boolean areDirectoriesIdentical(DirectoryEntry dirA, DirectoryEntry dirB) {
  109. return new DirectoryDelegate(dirA).equals(new DirectoryDelegate(dirB));
  110. }
  111. /**
  112. * Compares two {@link DocumentEntry} instances of a POI file system.
  113. * Documents that are not property set streams must be bitwise identical.
  114. * Property set streams must be logically equal.<p>
  115. *
  116. * (Their parent directories are not checked)
  117. */
  118. @SuppressWarnings("WeakerAccess")
  119. public static boolean areDocumentsIdentical(DocumentEntry docA, DocumentEntry docB)
  120. throws IOException {
  121. try {
  122. return new DocumentDelegate(docA).equals(new DocumentDelegate(docB));
  123. } catch (RuntimeException e) {
  124. if (e.getCause() instanceof IOException) {
  125. throw (IOException)e.getCause();
  126. } else {
  127. throw e;
  128. }
  129. }
  130. }
  131. private interface POIDelegate {
  132. }
  133. private static class DirectoryDelegate implements POIDelegate {
  134. final DirectoryEntry dir;
  135. DirectoryDelegate(DirectoryEntry dir) {
  136. this.dir = dir;
  137. }
  138. private Map<String,POIDelegate> entries() {
  139. return StreamSupport.stream(dir.spliterator(), false)
  140. .collect(Collectors.toMap(Entry::getName, DirectoryDelegate::toDelegate));
  141. }
  142. private static POIDelegate toDelegate(Entry entry) {
  143. return (entry.isDirectoryEntry())
  144. ? new DirectoryDelegate((DirectoryEntry)entry)
  145. : new DocumentDelegate((DocumentEntry)entry);
  146. }
  147. @Override
  148. public int hashCode() {
  149. return dir.getName().hashCode();
  150. }
  151. @Override
  152. public boolean equals(Object other) {
  153. if (!(other instanceof DirectoryDelegate)) {
  154. return false;
  155. }
  156. DirectoryDelegate dd = (DirectoryDelegate)other;
  157. if (this == dd) {
  158. return true;
  159. }
  160. // First, check names
  161. if (!Objects.equals(dir.getName(),dd.dir.getName())) {
  162. return false;
  163. }
  164. // Next up, check they have the same number of children
  165. if (dir.getEntryCount() != dd.dir.getEntryCount()) {
  166. return false;
  167. }
  168. if (!dir.getStorageClsid().equals(dd.dir.getStorageClsid())) {
  169. return false;
  170. }
  171. return entries().equals(dd.entries());
  172. }
  173. }
  174. private static class DocumentDelegate implements POIDelegate {
  175. final DocumentEntry doc;
  176. DocumentDelegate(DocumentEntry doc) {
  177. this.doc = doc;
  178. }
  179. @Override
  180. public int hashCode() {
  181. return doc.getName().hashCode();
  182. }
  183. @Override
  184. public boolean equals(Object other) {
  185. if (!(other instanceof DocumentDelegate)) {
  186. return false;
  187. }
  188. DocumentDelegate dd = (DocumentDelegate)other;
  189. if (this == dd) {
  190. return true;
  191. }
  192. if (!Objects.equals(doc.getName(), dd.doc.getName())) {
  193. // Names don't match, not the same
  194. return false;
  195. }
  196. try (DocumentInputStream inpA = new DocumentInputStream(doc);
  197. DocumentInputStream inpB = new DocumentInputStream(dd.doc)) {
  198. if (PropertySet.isPropertySetStream(inpA) &&
  199. PropertySet.isPropertySetStream(inpB)) {
  200. final PropertySet ps1 = PropertySetFactory.create(inpA);
  201. final PropertySet ps2 = PropertySetFactory.create(inpB);
  202. return ps1.equals(ps2);
  203. } else {
  204. return isEqual(inpA, inpB);
  205. }
  206. } catch (MarkUnsupportedException | NoPropertySetStreamException | IOException ex) {
  207. throw new RuntimeException(ex);
  208. }
  209. }
  210. private static boolean isEqual(DocumentInputStream i1, DocumentInputStream i2)
  211. throws IOException {
  212. final byte[] buf1 = new byte[4*1024];
  213. final byte[] buf2 = new byte[4*1024];
  214. try {
  215. int len;
  216. while ((len = i1.read(buf1)) > 0) {
  217. i2.readFully(buf2,0,len);
  218. for(int i=0;i<len;i++) {
  219. if (buf1[i] != buf2[i]) {
  220. return false;
  221. }
  222. }
  223. }
  224. // is the end of the second file also.
  225. return i2.read() < 0;
  226. } catch(EOFException | RuntimeException ioe) {
  227. return false;
  228. }
  229. }
  230. }
  231. }