Search in sources :

Example 1 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class RecordLister method run.

public void run() throws IOException {
    NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true);
    try {
        InputStream din = BiffViewer.getPOIFSInputStream(fs);
        try {
            RecordInputStream rinp = new RecordInputStream(din);
            while (rinp.hasNextRecord()) {
                int sid = rinp.getNextSid();
                rinp.nextRecord();
                int size = rinp.available();
                Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
                System.out.print(formatSID(sid) + " - " + formatSize(size) + " bytes");
                if (clz != null) {
                    System.out.print("  \t");
                    System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
                }
                System.out.println();
                byte[] data = rinp.readRemainder();
                if (data.length > 0) {
                    System.out.print("   ");
                    System.out.println(formatData(data));
                }
            }
        } finally {
            din.close();
        }
    } finally {
        fs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) RecordInputStream(org.apache.poi.hssf.record.RecordInputStream) InputStream(java.io.InputStream) File(java.io.File) RecordInputStream(org.apache.poi.hssf.record.RecordInputStream)

Example 2 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class POIFSViewer method viewFile.

private static void viewFile(String filename, boolean printName) {
    if (printName) {
        StringBuffer flowerbox = new StringBuffer();
        flowerbox.append(".");
        for (int j = 0; j < filename.length(); j++) {
            flowerbox.append("-");
        }
        flowerbox.append(".");
        System.out.println(flowerbox);
        System.out.println("|" + filename + "|");
        System.out.println(flowerbox);
    }
    try {
        NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(filename));
        List<String> strings = POIFSViewEngine.inspectViewable(fs, true, 0, "  ");
        for (String s : strings) {
            System.out.print(s);
        }
        fs.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) IOException(java.io.IOException) File(java.io.File)

Example 3 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class POIFSDump method main.

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.err.println("Must specify at least one file to dump");
        System.exit(1);
    }
    boolean dumpProps = false, dumpMini = false;
    for (String filename : args) {
        if (filename.equalsIgnoreCase("-dumprops") || filename.equalsIgnoreCase("-dump-props") || filename.equalsIgnoreCase("-dump-properties")) {
            dumpProps = true;
            continue;
        }
        if (filename.equalsIgnoreCase("-dumpmini") || filename.equalsIgnoreCase("-dump-mini") || filename.equalsIgnoreCase("-dump-ministream") || filename.equalsIgnoreCase("-dump-mini-stream")) {
            dumpMini = true;
            continue;
        }
        System.out.println("Dumping " + filename);
        FileInputStream is = new FileInputStream(filename);
        NPOIFSFileSystem fs;
        try {
            fs = new NPOIFSFileSystem(is);
        } finally {
            is.close();
        }
        try {
            DirectoryEntry root = fs.getRoot();
            String filenameWithoutPath = new File(filename).getName();
            File dumpDir = new File(filenameWithoutPath + "_dump");
            File file = new File(dumpDir, root.getName());
            if (!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(root, file);
            if (dumpProps) {
                HeaderBlock header = fs.getHeaderBlock();
                dump(fs, header.getPropertyStart(), "properties", file);
            }
            if (dumpMini) {
                NPropertyTable props = fs.getPropertyTable();
                int startBlock = props.getRoot().getStartBlock();
                if (startBlock == POIFSConstants.END_OF_CHAIN) {
                    System.err.println("No Mini Stream in file");
                } else {
                    dump(fs, startBlock, "mini-stream", file);
                }
            }
        } finally {
            fs.close();
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) IOException(java.io.IOException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class ModifyDocumentSummaryInformation method main.

/**
     * <p>Main method - see class description.</p>
     *
     * @param args The command-line parameters.
     * @throws IOException
     * @throws MarkUnsupportedException
     * @throws NoPropertySetStreamException
     * @throws UnexpectedPropertySetTypeException
     * @throws WritingNotSupportedException
     */
public static void main(final String[] args) throws IOException, NoPropertySetStreamException, MarkUnsupportedException, UnexpectedPropertySetTypeException, WritingNotSupportedException {
    /* Read the name of the POI filesystem to modify from the command line.
         * For brevity to boundary check is performed on the command-line
         * arguments. */
    File summaryFile = new File(args[0]);
    /* Open the POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(summaryFile, false);
    /* Read the summary information. */
    DirectoryEntry dir = poifs.getRoot();
    SummaryInformation si;
    try {
        si = (SummaryInformation) PropertySetFactory.create(dir, SummaryInformation.DEFAULT_STREAM_NAME);
    } catch (FileNotFoundException ex) {
        // There is no summary information yet. We have to create a new one
        si = PropertySetFactory.newSummaryInformation();
    }
    /* Change the author to "Rainer Klute". Any former author value will
         * be lost. If there has been no author yet, it will be created. */
    si.setAuthor("Rainer Klute");
    System.out.println("Author changed to " + si.getAuthor() + ".");
    /* Handling the document summary information is analogous to handling
         * the summary information. An additional feature, however, are the
         * custom properties. */
    /* Read the document summary information. */
    DocumentSummaryInformation dsi;
    try {
        dsi = (DocumentSummaryInformation) PropertySetFactory.create(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    } catch (FileNotFoundException ex) {
        /* There is no document summary information yet. We have to create a
             * new one. */
        dsi = PropertySetFactory.newDocumentSummaryInformation();
    }
    /* Change the category to "POI example". Any former category value will
         * be lost. If there has been no category yet, it will be created. */
    dsi.setCategory("POI example");
    System.out.println("Category changed to " + dsi.getCategory() + ".");
    /* Read the custom properties. If there are no custom properties yet,
         * the application has to create a new CustomProperties object. It will
         * serve as a container for custom properties. */
    CustomProperties customProperties = dsi.getCustomProperties();
    if (customProperties == null)
        customProperties = new CustomProperties();
    /* Insert some custom properties into the container. */
    customProperties.put("Key 1", "Value 1");
    customProperties.put("Schlüssel 2", "Wert 2");
    customProperties.put("Sample Number", new Integer(12345));
    customProperties.put("Sample Boolean", Boolean.TRUE);
    customProperties.put("Sample Date", new Date());
    /* Read a custom property. */
    Object value = customProperties.get("Sample Number");
    System.out.println("Custom Sample Number is now " + value);
    /* Write the custom properties back to the document summary
         * information. */
    dsi.setCustomProperties(customProperties);
    /* Write the summary information and the document summary information
         * to the POI filesystem. */
    si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    /* Write the POI filesystem back to the original file. Please note that
         * in production code you should take care when write directly to the 
         * origin, to make sure you don't loose things on error */
    poifs.writeFilesystem();
    poifs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileNotFoundException(java.io.FileNotFoundException) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File) Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 5 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class ExtractorFactory method createExtractor.

public static POITextExtractor createExtractor(File f) throws IOException, OpenXML4JException, XmlException {
    NPOIFSFileSystem fs = null;
    try {
        fs = new NPOIFSFileSystem(f);
        POIOLE2TextExtractor extractor = createExtractor(fs);
        extractor.setFilesystem(fs);
        return extractor;
    } catch (OfficeXmlFileException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        return createExtractor(OPCPackage.open(f.toString(), PackageAccess.READ));
    } catch (NotOLE2FileException ne) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file");
    } catch (OpenXML4JException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (XmlException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (IOException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (RuntimeException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) NotOLE2FileException(org.apache.poi.poifs.filesystem.NotOLE2FileException) XmlException(org.apache.xmlbeans.XmlException) IOException(java.io.IOException) POIOLE2TextExtractor(org.apache.poi.POIOLE2TextExtractor)

Aggregations

NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)101 Test (org.junit.Test)57 File (java.io.File)35 InputStream (java.io.InputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)14 FileOutputStream (java.io.FileOutputStream)12 TempFile (org.apache.poi.util.TempFile)12 FileInputStream (java.io.FileInputStream)11 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)10 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)10 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)9 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)8 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)7 TikaInputStream (org.apache.tika.io.TikaInputStream)6 AgileDecryptor (org.apache.poi.poifs.crypt.agile.AgileDecryptor)5 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)5