Search in sources :

Example 21 with DirectoryNode

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

the class TestNonStandardWorkbookStreamNames method testOpenWORKBOOK.

/**
	 * Test that we can open a file with WORKBOOK
	 */
@Test
public void testOpenWORKBOOK() throws IOException {
    // Try to open the workbook
    InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
    HSSFWorkbook wb = new HSSFWorkbook(is);
    is.close();
    DirectoryNode root = wb.getDirectory();
    // Ensure that we have a WORKBOOK entry and a summary
    assertTrue(root.hasEntry("WORKBOOK"));
    assertTrue(root.hasEntry("\005SummaryInformation"));
    // But not a Workbook one
    assertFalse(root.hasEntry("Workbook"));
    wb.close();
}
Also used : InputStream(java.io.InputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Test(org.junit.Test)

Example 22 with DirectoryNode

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

the class TestNonStandardWorkbookStreamNames method testOpenBOOK.

/**
    * Test that we can open a file with BOOK
    */
@Test
public void testOpenBOOK() throws IOException {
    // Try to open the workbook
    InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsB);
    HSSFWorkbook wb = new HSSFWorkbook(is);
    is.close();
    DirectoryNode root = wb.getDirectory();
    // Ensure that we have a BOOK entry
    assertTrue(root.hasEntry("BOOK"));
    // But not a Workbook one and not a Summary one
    assertFalse(root.hasEntry("Workbook"));
    assertFalse(root.hasEntry("\\005SummaryInformation"));
    wb.close();
}
Also used : InputStream(java.io.InputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Test(org.junit.Test)

Example 23 with DirectoryNode

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

the class TestHSSFWorkbook method testRewriteFileBug58480.

@Test
public void testRewriteFileBug58480() throws IOException {
    final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
    try {
        // create new workbook
        {
            final Workbook workbook = new HSSFWorkbook();
            final Sheet sheet = workbook.createSheet("foo");
            final Row row = sheet.createRow(1);
            row.createCell(1).setCellValue("bar");
            writeAndCloseWorkbook(workbook, file);
        }
        // edit the workbook
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
            try {
                DirectoryNode root = fs.getRoot();
                final Workbook workbook = new HSSFWorkbook(root, true);
                final Sheet sheet = workbook.getSheet("foo");
                sheet.getRow(1).createCell(2).setCellValue("baz");
                writeAndCloseWorkbook(workbook, file);
            } finally {
                fs.close();
            }
        }
    } finally {
        assertTrue(file.exists());
        assertTrue(file.delete());
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Row(org.apache.poi.ss.usermodel.Row) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) Test(org.junit.Test)

Example 24 with DirectoryNode

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

the class XSSFWorkbook method addOlePackage.

@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException {
    // find an unused part name
    OPCPackage opc = getPackage();
    PackagePartName pnOLE;
    int oleId = 0;
    do {
        try {
            pnOLE = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + (++oleId) + ".bin");
        } catch (InvalidFormatException e) {
            throw new IOException("ole object name not recognized", e);
        }
    } while (opc.containPart(pnOLE));
    PackagePart pp = opc.createPart(pnOLE, "application/vnd.openxmlformats-officedocument.oleObject");
    Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length + 500);
    ole10.writeOut(bos);
    POIFSFileSystem poifs = new POIFSFileSystem();
    DirectoryNode root = poifs.getRoot();
    root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
    root.setStorageClsid(ClassID.OLE10_PACKAGE);
    // TODO: generate CombObj stream
    OutputStream os = pp.getOutputStream();
    poifs.writeFilesystem(os);
    os.close();
    poifs.close();
    return oleId;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 25 with DirectoryNode

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

the class TestEncryptor method listEntry.

private void listEntry(DocumentNode de, String ext, String path) throws IOException {
    path += "\\" + de.getName().replaceAll("[\\p{Cntrl}]", "_");
    System.out.println(ext + ": " + path + " (" + de.getSize() + " bytes)");
    String name = de.getName().replaceAll("[\\p{Cntrl}]", "_");
    InputStream is = ((DirectoryNode) de.getParent()).createDocumentInputStream(de);
    FileOutputStream fos = new FileOutputStream("solr." + name + "." + ext);
    IOUtils.copy(is, fos);
    fos.close();
    is.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode)

Aggregations

DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)47 Test (org.junit.Test)16 InputStream (java.io.InputStream)15 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)13 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)12 Entry (org.apache.poi.poifs.filesystem.Entry)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)8 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)6 FileInputStream (java.io.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)5 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)4 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AttachmentChunks (org.apache.poi.hsmf.datatypes.AttachmentChunks)3 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)3 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)3