Java Code Examples for org.apache.poi.poifs.filesystem.DirectoryEntry#getEntries()

The following examples show how to use org.apache.poi.poifs.filesystem.DirectoryEntry#getEntries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: POIFSDump.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static void dump(DirectoryEntry root, File parent) throws IOException {
    for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
        Entry entry = it.next();
        if(entry instanceof DocumentNode){
            DocumentNode node = (DocumentNode)entry;
            DocumentInputStream is = new DocumentInputStream(node);
            byte[] bytes = IOUtils.toByteArray(is);
            is.close();

            OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
            try {
            	out.write(bytes);
            } finally {
            	out.close();
            }
        } else if (entry instanceof DirectoryEntry){
            DirectoryEntry dir = (DirectoryEntry)entry;
            File file = new File(parent, entry.getName());
            if(!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(dir, file);
        } else {
            System.err.println("Skipping unsupported POIFS entry: " + entry);
        }
    }
}
 
Example 2
Source File: PoiTreeModel.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieves child nodes from a directory entry.
 *
 * @param parent parent directory entry
 * @return list of child nodes
 */
private List<Entry> getChildNodes(DirectoryEntry parent)
{
   List<Entry> result = new ArrayList<>();
   Iterator<Entry> entries = parent.getEntries();
   while (entries.hasNext())
   {
      result.add(entries.next());
   }
   return result;
}
 
Example 3
Source File: MppDump.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method recursively descends the directory structure, dumping
 * details of any files it finds to the output file.
 *
 * @param pw Output PrintWriter
 * @param dir DirectoryEntry to dump
 * @param prefix prefix used to identify path to this object
 * @param showData flag indicating if data is dumped, or just structure
 * @param hex set to true if hex output is required
 * @param indent indent used if displaying structure only
 * @throws Exception Thrown on file read errors
 */
private static void dumpTree(PrintWriter pw, DirectoryEntry dir, String prefix, boolean showData, boolean hex, String indent) throws Exception
{
   long byteCount;

   for (Iterator<Entry> iter = dir.getEntries(); iter.hasNext();)
   {
      Entry entry = iter.next();
      if (entry instanceof DirectoryEntry)
      {
         String childIndent = indent;
         if (childIndent != null)
         {
            childIndent += " ";
         }

         String childPrefix = prefix + "[" + entry.getName() + "].";
         pw.println("start dir: " + prefix + entry.getName());
         dumpTree(pw, (DirectoryEntry) entry, childPrefix, showData, hex, childIndent);
         pw.println("end dir: " + prefix + entry.getName());
      }
      else
         if (entry instanceof DocumentEntry)
         {
            if (showData)
            {
               pw.println("start doc: " + prefix + entry.getName());
               if (hex == true)
               {
                  byteCount = hexdump(new DocumentInputStream((DocumentEntry) entry), pw);
               }
               else
               {
                  byteCount = asciidump(new DocumentInputStream((DocumentEntry) entry), pw);
               }
               pw.println("end doc: " + prefix + entry.getName() + " (" + byteCount + " bytes read)");
            }
            else
            {
               if (indent != null)
               {
                  pw.print(indent);
               }
               pw.println("doc: " + prefix + entry.getName());
            }
         }
         else
         {
            pw.println("found unknown: " + prefix + entry.getName());
         }
   }
}