Search in sources :

Example 51 with NPOIFSFileSystem

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

the class TestBugs method bug46904.

/**
     * java.io.IOException: block[ 0 ] already removed
     * (is an excel 95 file though)
     */
@Test
public void bug46904() throws Exception {
    try {
        OPOIFSFileSystem fs = new OPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
        new HSSFWorkbook(fs.getRoot(), false).close();
        fail("Should catch exception here");
    } catch (OldExcelFormatException e) {
        assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
    }
    try {
        NPOIFSFileSystem fs = new NPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
        try {
            new HSSFWorkbook(fs.getRoot(), false).close();
            fail("Should catch exception here");
        } finally {
            fs.close();
        }
    } catch (OldExcelFormatException e) {
        assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) Test(org.junit.Test)

Example 52 with NPOIFSFileSystem

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

the class TestBugs method bug51535.

/**
     * Large row numbers and NPOIFS vs POIFS
     */
@SuppressWarnings("resource")
@Test
public void bug51535() throws Exception {
    byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");
    HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(new ByteArrayInputStream(data)).getRoot(), false);
    HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(new ByteArrayInputStream(data)).getRoot(), false);
    for (HSSFWorkbook wb : new HSSFWorkbook[] { wbPOIFS, wbNPOIFS }) {
        assertEquals(3, wb.getNumberOfSheets());
        // Check directly
        HSSFSheet s = wb.getSheetAt(0);
        assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
        assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
        assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
        assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
        // Extract and check
        ExcelExtractor ex = new ExcelExtractor(wb);
        String text = ex.getText();
        assertContains(text, "Top Left Cell");
        assertContains(text, "Top Right Cell");
        assertContains(text, "Bottom Left Cell");
        assertContains(text, "Bottom Right Cell");
        ex.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) Test(org.junit.Test)

Example 53 with NPOIFSFileSystem

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

the class HWPFDocument method write.

/**
     * Writes out the word file that is represented by an instance of this class.
     * 
     * If the {@link File} exists, it will be replaced, otherwise a new one 
     * will be created
     *
     * @param newFile The File to write to.
     * @throws IOException If there is an unexpected IOException from writing
     *         to the File.
     *         
     * @since 3.15 beta 3
     */
@Override
public void write(File newFile) throws IOException {
    NPOIFSFileSystem pfs = POIFSFileSystem.create(newFile);
    write(pfs, true);
    pfs.writeFilesystem();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem)

Example 54 with NPOIFSFileSystem

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

the class POIDocument method getPropertySet.

/** 
     * For a given named property entry, either return it or null if
     *  if it wasn't found
     *  
     *  @param setName The property to read
     *  @param encryptionInfo the encryption descriptor in case of cryptoAPI encryption
     *  @return The value of the given property or null if it wasn't found.
     */
protected PropertySet getPropertySet(String setName, EncryptionInfo encryptionInfo) {
    DirectoryNode dirNode = directory;
    NPOIFSFileSystem encPoifs = null;
    String step = "getting";
    try {
        if (encryptionInfo != null) {
            step = "getting encrypted";
            String encryptedStream = null;
            for (String s : encryptedStreamNames) {
                if (dirNode.hasEntry(s)) {
                    encryptedStream = s;
                }
            }
            if (encryptedStream == null) {
                throw new EncryptedDocumentException("can't find matching encrypted property stream");
            }
            CryptoAPIDecryptor dec = (CryptoAPIDecryptor) encryptionInfo.getDecryptor();
            encPoifs = dec.getSummaryEntries(dirNode, encryptedStream);
            dirNode = encPoifs.getRoot();
        }
        //directory can be null when creating new documents
        if (dirNode == null || !dirNode.hasEntry(setName)) {
            return null;
        }
        // Find the entry, and get an input stream for it
        step = "getting";
        DocumentInputStream dis = dirNode.createDocumentInputStream(dirNode.getEntry(setName));
        try {
            // Create the Property Set
            step = "creating";
            return PropertySetFactory.create(dis);
        } finally {
            dis.close();
        }
    } catch (Exception e) {
        logger.log(POILogger.WARN, "Error " + step + " property set with name " + setName, e);
        return null;
    } finally {
        if (encPoifs != null) {
            try {
                encPoifs.close();
            } catch (IOException e) {
                logger.log(POILogger.WARN, "Error closing encrypted property poifs", e);
            }
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) CryptoAPIDecryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor) IOException(java.io.IOException)

Example 55 with NPOIFSFileSystem

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

the class VBAMacroReader method openOOXML.

private void openOOXML(InputStream zipFile) throws IOException {
    ZipInputStream zis = new ZipInputStream(zipFile);
    ZipEntry zipEntry;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) {
            try {
                // Make a NPOIFS from the contents, and close the stream
                this.fs = new NPOIFSFileSystem(zis);
                return;
            } catch (IOException e) {
                // Tidy up
                zis.close();
                // Pass on
                throw e;
            }
        }
    }
    zis.close();
    throw new IllegalArgumentException("No VBA project found");
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

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