/** * Copies an Entry into a target POIFS directory, recursively */ @Internal public static void copyNodeRecursively( Entry entry, DirectoryEntry target ) throws IOException { if ( entry.isDirectoryEntry() ) { DirectoryEntry dirEntry = (DirectoryEntry)entry; DirectoryEntry newTarget = target.createDirectory( entry.getName() ); newTarget.setStorageClsid( dirEntry.getStorageClsid() ); Iterator<Entry> entries = dirEntry.getEntries(); while ( entries.hasNext() ) { copyNodeRecursively( entries.next(), newTarget ); } } else { DocumentEntry dentry = (DocumentEntry) entry; DocumentInputStream dstream = new DocumentInputStream( dentry ); target.createDocument( dentry.getName(), dstream ); dstream.close(); } }
public static DocumentEntry createEncryptionEntry(DirectoryEntry dir, String path, EncryptionRecord out) throws IOException { String parts[] = path.split("/"); for (int i=0; i<parts.length-1; i++) { dir = dir.hasEntry(parts[i]) ? (DirectoryEntry)dir.getEntry(parts[i]) : dir.createDirectory(parts[i]); } final byte buf[] = new byte[5000]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0); out.write(bos); String fileName = parts[parts.length-1]; if (dir.hasEntry(fileName)) { dir.getEntry(fileName).delete(); } return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){ public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { event.getStream().write(buf, 0, event.getLimit()); } catch (IOException e) { throw new EncryptedDocumentException(e); } } }); }
public String getName() { return directory.getName(); }
public int getEntryCount() { int size = directory.getEntryCount(); for (String excl : excludes) { if (directory.hasEntry(excl)) { size--; } } return size; }
/** * Add the \1OLE marker entry, which is not the Ole10Native entry. * Beside this "\u0001Ole" record there were several other records, e.g. CompObj, * OlePresXXX, but it seems, that they aren't necessary */ public static void createOleMarkerEntry(final DirectoryEntry parent) throws IOException { if (!parent.hasEntry(OLE_MARKER_NAME)) { parent.createDocument(OLE_MARKER_NAME, new ByteArrayInputStream(OLE_MARKER_BYTES)); } }
@Override public boolean equals(Object other) { if (!(other instanceof DirectoryDelegate)) { return false; } DirectoryDelegate dd = (DirectoryDelegate)other; if (this == dd) { return true; } // First, check names if (!Objects.equals(dir.getName(),dd.dir.getName())) { return false; } // Next up, check they have the same number of children if (dir.getEntryCount() != dd.dir.getEntryCount()) { return false; } return entries().equals(dd.entries()); } }
if (dir.hasEntry("Package")) { Entry ooxml = dir.getEntry("Package"); return; handleEmbeddedResource(stream, null, dir.getName(), dir.getStorageClsid(), type.toString(), xhtml, true); return; metadata.set(TikaCoreProperties.EMBEDDED_RELATIONSHIP_ID, dir.getName()); if (dir.getStorageClsid() != null) { metadata.set(TikaCoreProperties.EMBEDDED_STORAGE_CLASS_ID, dir.getStorageClsid().toString()); String rName = (resourceName == null) ? dir.getName() : resourceName; try { if (type == POIFSDocumentType.OLE10_NATIVE) { contentsEntry = (DocumentEntry) dir.getEntry("CONTENTS"); } catch (FileNotFoundException ioe) { contentsEntry = (DocumentEntry) dir.getEntry("Contents");
/** * Copies an Entry into a target POIFS directory, recursively */ @Internal public static void copyNodeRecursively( Entry entry, DirectoryEntry target ) throws IOException { // logger.log( POILogger.ERROR, "copyNodeRecursively called with "+entry.getName()+ // ","+target.getName()); DirectoryEntry newTarget = null; if ( entry.isDirectoryEntry() ) { newTarget = target.createDirectory( entry.getName() ); Iterator<Entry> entries = ( (DirectoryEntry) entry ).getEntries(); while ( entries.hasNext() ) { copyNodeRecursively( entries.next(), newTarget ); } } else { DocumentEntry dentry = (DocumentEntry) entry; DocumentInputStream dstream = new DocumentInputStream( dentry ); target.createDocument( dentry.getName(), dstream ); dstream.close(); } }
protected void copy(DirectoryEntry sourceDir, DirectoryEntry destDir) throws IOException { for (Entry entry : sourceDir) { if (entry instanceof DirectoryEntry) { // Need to recurse DirectoryEntry newDir = destDir.createDirectory(entry.getName()); copy((DirectoryEntry) entry, newDir); } else { // Copy entry try (InputStream contents = new DocumentInputStream((DocumentEntry) entry)) { destDir.createDocument(entry.getName(), contents); } } } } }
/** * Writes a property set to a document in a POI filesystem directory. * * @param dir The directory in the POI filesystem to write the document to. * @param name The document's name. If there is already a document with the * same name in the directory the latter will be overwritten. * * @throws WritingNotSupportedException if the filesystem doesn't support writing * @throws IOException if the old entry can't be deleted or the new entry be written */ public void write(final DirectoryEntry dir, final String name) throws WritingNotSupportedException, IOException { /* If there is already an entry with the same name, remove it. */ if (dir.hasEntry(name)) { final Entry e = dir.getEntry(name); e.delete(); } /* Create the new entry. */ dir.createDocument(name, toInputStream()); }
protected void sanitize(BleachSession session, DirectoryEntry rootIn, DirectoryEntry rootOut) { LOGGER.debug("Entries before: {}", rootIn.getEntryNames()); // Save the changes to a new file // Returns false if the entry should be removed Predicate<Entry> visitor = ((Predicate<Entry>) (e -> true)) .and(new MacroRemover(session)) .and(new ObjectRemover(session)) .and(new SummaryInformationSanitiser(session)); LOGGER.debug("Root ClassID: {}", rootIn.getStorageClsid()); // https://blogs.msdn.microsoft.com/heaths/2006/02/27/identifying-windows-installer-file-types/ rootOut.setStorageClsid(rootIn.getStorageClsid()); rootIn .getEntries() .forEachRemaining( entry -> { if (!visitor.test(entry)) { return; } copyNodesRecursively(session, entry, rootOut); }); LOGGER.debug("Entries after: {}", rootOut.getEntryNames()); // Save the changes to a new file }
public static void dump(DirectoryEntry root) throws IOException { System.out.println(root.getName()+" : storage CLSID "+root.getStorageClsid()); for(Iterator it = root.getEntries(); it.hasNext();){ Entry entry = (Entry)it.next(); if(entry instanceof DocumentNode){
public Entry getEntry(String name) throws FileNotFoundException { if (excludes.contains(name)) { throw new FileNotFoundException(name); } Entry entry = directory.getEntry(name); return wrapEntry(entry); } private Entry wrapEntry(Entry entry) {
private FilteringIterator() { parent = directory.getEntries(); locateNext(); } private void locateNext() {
public DocumentEntry createDocument(String name, InputStream stream) throws IOException { return directory.createDocument(name, stream); }
if (! dirA.getName().equals(dirB.getName())) { return false; if (dirA.getEntryCount() != dirB.getEntryCount()) { return false; Entry b = dirB.getEntry(a.getName()); boolean match; if (a.isDirectoryEntry()) {
if (!dir.hasEntry(OLESTREAM_NAME)) { dir.createDocument(OLESTREAM_NAME, new ByteArrayInputStream(oleBytes)); if (!root.hasEntry(storageStr)) { oleDir = root.createDirectory(storageStr); oleDir.setStorageClsid(PACKAGE.getClassID()); byte buf1[] = bos.toByteArray(); oleDir.createDocument(Ole10Native2.OLE10_NATIVE, new ByteArrayInputStream(buf1));
@Override public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException { // check if we were created by POIFS otherwise create a new dummy POIFS for storing the package data if (initDirectory()) { preserveNodes = true; } // get free MBD-Node int storageId = 0; DirectoryEntry oleDir = null; do { String storageStr = "MBD"+ HexDump.toHex(++storageId); if (!getDirectory().hasEntry(storageStr)) { oleDir = getDirectory().createDirectory(storageStr); oleDir.setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID()); } } while (oleDir == null); Ole10Native.createOleMarkerEntry(oleDir); Ole10Native oleNative = new Ole10Native(label, fileName, command, oleData); ByteArrayOutputStream bos = new ByteArrayOutputStream(); oleNative.writeOut(bos); oleDir.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray())); return storageId; }
private String dumpFileSystem( DirectoryEntry directory ) { StringBuilder result = new StringBuilder(); result.append( "+ " ); result.append( directory.getName() ); for ( Iterator<Entry> iterator = directory.getEntries(); iterator .hasNext(); ) { Entry entry = iterator.next(); String entryToString = "\n" + dumpFileSystem( entry ); entryToString = entryToString.replaceAll( "\n", "\n+---" ); result.append( entryToString ); } result.append( "\n" ); return result.toString(); }
public DirectoryEntry createDirectory(String name) throws IOException { return directory.createDirectory(name); }