Search in sources :

Example 6 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class ReadCustomPropertySets method main.

/**
     * <p>Runs the example program.</p>
     *
     * @param args Command-line arguments (unused).
     * @throws IOException if any I/O exception occurs.
     */
public static void main(final String[] args) throws IOException {
    final String filename = args[0];
    POIFSReader r = new POIFSReader();
    /* Register a listener for *all* documents. */
    r.registerListener(new MyPOIFSReaderListener());
    r.read(new FileInputStream(filename));
}
Also used : POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Example 7 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class ReadTitle method main.

/**
     * <p>Runs the example program.</p>
     *
     * @param args Command-line arguments. The first command-line argument must
     * be the name of a POI filesystem to read.
     * @throws IOException if any I/O exception occurs.
     */
public static void main(final String[] args) throws IOException {
    final String filename = args[0];
    POIFSReader r = new POIFSReader();
    r.registerListener(new MyPOIFSReaderListener(), "\005SummaryInformation");
    r.read(new FileInputStream(filename));
}
Also used : POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Example 8 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class CopyCompare method main.

/**
     * <p>Runs the example program. The application expects one or two
     * arguments:</p>
     * 
     * <ol>
     * 
     * <li><p>The first argument is the disk file name of the POI filesystem to
     * copy.</p></li>
     * 
     * <li><p>The second argument is optional. If it is given, it is the name of
     * a disk file the copy of the POI filesystem will be written to. If it is
     * not given, the copy will be written to a temporary file which will be
     * deleted at the end of the program.</p></li>
     * 
     * </ol>
     *
     * @param args Command-line arguments.
     * @exception MarkUnsupportedException if a POI document stream does not
     * support the mark() operation.
     * @exception NoPropertySetStreamException if the application tries to
     * create a property set from a POI document stream that is not a property
     * set stream.
     * @exception IOException if any I/O exception occurs.
     * @exception UnsupportedEncodingException if a character encoding is not
     * supported.
     */
public static void main(final String[] args) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
    String originalFileName = null;
    String copyFileName = null;
    /* Check the command-line arguments. */
    if (args.length == 1) {
        originalFileName = args[0];
        File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");
        f.deleteOnExit();
        copyFileName = f.getAbsolutePath();
    } else if (args.length == 2) {
        originalFileName = args[0];
        copyFileName = args[1];
    } else {
        System.err.println("Usage: " + CopyCompare.class.getName() + "originPOIFS [copyPOIFS]");
        System.exit(1);
    }
    /* Read the origin POIFS using the eventing API. The real work is done
         * in the class CopyFile which is registered here as a POIFSReader. */
    final POIFSReader r = new POIFSReader();
    final CopyFile cf = new CopyFile(copyFileName);
    r.registerListener(cf);
    r.setNotifyEmptyDirectories(true);
    FileInputStream fis = new FileInputStream(originalFileName);
    r.read(fis);
    fis.close();
    /* Write the new POIFS to disk. */
    cf.close();
    /* Read all documents from the original POI file system and compare them
         * with the equivalent document from the copy. */
    POIFSFileSystem opfs = null, cpfs = null;
    try {
        opfs = new POIFSFileSystem(new File(originalFileName));
        cpfs = new POIFSFileSystem(new File(copyFileName));
        final DirectoryEntry oRoot = opfs.getRoot();
        final DirectoryEntry cRoot = cpfs.getRoot();
        final StringBuffer messages = new StringBuffer();
        if (equal(oRoot, cRoot, messages)) {
            System.out.println("Equal");
        } else {
            System.out.println("Not equal: " + messages);
        }
    } finally {
        IOUtils.closeQuietly(cpfs);
        IOUtils.closeQuietly(opfs);
    }
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Example 9 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class TestWrite method writeSimplePropertySet.

/**
     * <p>Writes a simple property set with a SummaryInformation section to a
     * POIFS and reads it back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception UnsupportedVariantTypeException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeSimplePropertySet() throws IOException, UnsupportedVariantTypeException {
    final String AUTHOR = "Rainer Klute";
    final String TITLE = "Test Document";
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    final MutableSection si = new MutableSection();
    si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    ps.clearSections();
    ps.addSection(si);
    final MutableProperty p = new MutableProperty();
    p.setID(PropertyIDMap.PID_AUTHOR);
    p.setType(Variant.VT_LPWSTR);
    p.setValue(AUTHOR);
    si.setProperty(p);
    si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);
    poiFs.createDocument(ps.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final PropertySet[] psa = new PropertySet[1];
    final POIFSReader r = new POIFSReader();
    r.registerListener(new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                psa[0] = PropertySetFactory.create(event.getStream());
            } catch (Exception ex) {
                fail(ex.getMessage());
            }
        }
    }, SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    assertTrue(psa[0].isSummaryInformation());
    final Section s = (psa[0].getSections().get(0));
    Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
    Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
    assertEquals(AUTHOR, p1);
    assertEquals(TITLE, p2);
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) MutableSection(org.apache.poi.hpsf.MutableSection) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) NoPropertySetStreamException(org.apache.poi.hpsf.NoPropertySetStreamException) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalPropertySetDataException(org.apache.poi.hpsf.IllegalPropertySetDataException) UnsupportedVariantTypeException(org.apache.poi.hpsf.UnsupportedVariantTypeException) IOException(java.io.IOException) NoFormatIDException(org.apache.poi.hpsf.NoFormatIDException) ReadingNotSupportedException(org.apache.poi.hpsf.ReadingNotSupportedException) HPSFException(org.apache.poi.hpsf.HPSFException) FileInputStream(java.io.FileInputStream) POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 10 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class Util method readPOIFiles.

/**
     * <p>Reads a set of files from a POI filesystem and returns them
     * as an array of {@link POIFile} instances. This method loads all
     * files into memory and thus does not cope well with large POI
     * filessystems.</p>
     * 
     * @param poiFs The name of the POI filesystem as seen by the
     * operating system. (This is the "filename".)
     *
     * @param poiFiles The names of the POI files to be read.
     *
     * @return The POI files. The elements are ordered in the same way
     * as the files in the POI filesystem.
     * 
     * @exception FileNotFoundException if the file containing the POI 
     * filesystem does not exist
     * 
     * @exception IOException if an I/O exception occurs
     */
public static List<POIFile> readPOIFiles(final File poiFs, final String... poiFiles) throws FileNotFoundException, IOException {
    final List<POIFile> files = new ArrayList<POIFile>();
    POIFSReader r = new POIFSReader();
    POIFSReaderListener pfl = new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                final POIFile f = new POIFile();
                f.setName(event.getName());
                f.setPath(event.getPath());
                final InputStream in = event.getStream();
                f.setBytes(IOUtils.toByteArray(in));
                in.close();
                files.add(f);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    };
    if (poiFiles.length == 0) {
        /* Register the listener for all POI files. */
        r.registerListener(pfl);
    } else {
        for (String poiFile : poiFiles) {
            r.registerListener(pfl, poiFile);
        }
    }
    /* Read the POI filesystem. */
    FileInputStream stream = new FileInputStream(poiFs);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    return files;
}
Also used : POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) IOException(java.io.IOException) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Aggregations

FileInputStream (java.io.FileInputStream)11 POIFSReader (org.apache.poi.poifs.eventfilesystem.POIFSReader)11 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 POIFSReaderEvent (org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent)4 POIFSReaderListener (org.apache.poi.poifs.eventfilesystem.POIFSReaderListener)4 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)4 TempFile (org.apache.poi.util.TempFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)3 MutableSection (org.apache.poi.hpsf.MutableSection)3 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)3 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2