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.

POIFSLister.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.dev;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.util.Iterator;
  20. import org.apache.poi.poifs.filesystem.DirectoryNode;
  21. import org.apache.poi.poifs.filesystem.DocumentNode;
  22. import org.apache.poi.poifs.filesystem.Entry;
  23. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  24. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  25. /**
  26. * A lister of the entries in POIFS files.
  27. *
  28. * Much simpler than {@link POIFSViewer}
  29. */
  30. public class POIFSLister {
  31. /**
  32. * Display the entries of multiple POIFS files
  33. *
  34. * @param args the names of the files to be displayed
  35. */
  36. public static void main(final String args[]) throws IOException {
  37. if (args.length == 0) {
  38. System.err.println("Must specify at least one file to view");
  39. System.exit(1);
  40. }
  41. boolean withSizes = false;
  42. boolean newPOIFS = true;
  43. for (int j = 0; j < args.length; j++) {
  44. if (args[j].equalsIgnoreCase("-size") || args[j].equalsIgnoreCase("-sizes")) {
  45. withSizes = true;
  46. } else if (args[j].equalsIgnoreCase("-old") || args[j].equalsIgnoreCase("-old-poifs")) {
  47. newPOIFS = false;
  48. } else {
  49. if(newPOIFS) {
  50. viewFile(args[j], withSizes);
  51. } else {
  52. viewFileOld(args[j], withSizes);
  53. }
  54. }
  55. }
  56. }
  57. public static void viewFile(final String filename, boolean withSizes) throws IOException {
  58. NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(filename));
  59. displayDirectory(fs.getRoot(), "", withSizes);
  60. }
  61. public static void viewFileOld(final String filename, boolean withSizes) throws IOException {
  62. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
  63. displayDirectory(fs.getRoot(), "", withSizes);
  64. }
  65. public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) {
  66. System.out.println(indent + dir.getName() + " -");
  67. String newIndent = indent + " ";
  68. boolean hadChildren = false;
  69. for(Iterator<Entry> it = dir.getEntries(); it.hasNext();) {
  70. hadChildren = true;
  71. Entry entry = it.next();
  72. if (entry instanceof DirectoryNode) {
  73. displayDirectory((DirectoryNode) entry, newIndent, withSizes);
  74. } else {
  75. DocumentNode doc = (DocumentNode) entry;
  76. String name = doc.getName();
  77. String size = "";
  78. if (name.charAt(0) < 10) {
  79. String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1);
  80. name = name.substring(1) + " <" + altname + ">";
  81. }
  82. if (withSizes) {
  83. size = " [" + doc.getSize() + " / 0x" +
  84. Integer.toHexString(doc.getSize()) + "]";
  85. }
  86. System.out.println(newIndent + name + size);
  87. }
  88. }
  89. if (!hadChildren) {
  90. System.out.println(newIndent + "(no children)");
  91. }
  92. }
  93. }