Search in sources :

Example 26 with NPOIFSFileSystem

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

the class TestDocumentEncryption method cryptoAPIChangeKeySize.

@Test
public void cryptoAPIChangeKeySize() throws Exception {
    String pptFile = "cryptoapi-proc2356.ppt";
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true);
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    // need to cache data (i.e. read all data) before changing the key size
    List<HSLFPictureData> picsExpected = hss.getPictureData();
    hss.getDocumentSummaryInformation();
    EncryptionInfo ei = hss.getDocumentEncryptionAtom().getEncryptionInfo();
    ((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    hss.write(bos);
    hss.close();
    fs.close();
    fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
    hss = new HSLFSlideShowImpl(fs);
    List<HSLFPictureData> picsActual = hss.getPictureData();
    assertEquals(picsExpected.size(), picsActual.size());
    for (int i = 0; i < picsExpected.size(); i++) {
        assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData());
    }
    hss.close();
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Example 27 with NPOIFSFileSystem

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

the class TestHWPFWrite method testInPlaceWrite.

/**
     * Writing to the file we opened from - note, uses a temp file to avoid
     * changing our test files!
     */
@Test
public void testInPlaceWrite() throws Exception {
    // Setup as a copy of a known-good file
    final File file = TempFile.createTempFile("TestDocument", ".doc");
    InputStream inputStream = SAMPLES.openResourceAsStream("SampleDoc.doc");
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        try {
            IOUtils.copy(inputStream, outputStream);
        } finally {
            outputStream.close();
        }
    } finally {
        inputStream.close();
    }
    // Open from the temp file in read-write mode
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(file, false);
    HWPFDocument doc = new HWPFDocument(poifs.getRoot());
    Range r = doc.getRange();
    assertEquals("I am a test document\r", r.getParagraph(0).text());
    // Change
    r.replaceText("X XX a test document\r", false);
    // Save in-place, close, re-open and check
    doc.write();
    doc.close();
    poifs.close();
    poifs = new NPOIFSFileSystem(file);
    doc = new HWPFDocument(poifs.getRoot());
    r = doc.getRange();
    assertEquals("X XX a test document\r", r.getParagraph(0).text());
    doc.close();
    poifs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 28 with NPOIFSFileSystem

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

the class TestHWPFWrite method testInvalidInPlaceWriteNPOIFS.

@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteNPOIFS() throws Exception {
    // Can't work for Read-Only files
    NPOIFSFileSystem fs = new NPOIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true);
    HWPFDocument doc = new HWPFDocument(fs.getRoot());
    try {
        doc.write();
    } finally {
        doc.close();
        fs.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Test(org.junit.Test)

Example 29 with NPOIFSFileSystem

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

the class TestWordExtractor method testDifferentPOIFS.

/**
     * Tests that we can work with both {@link POIFSFileSystem}
     *  and {@link NPOIFSFileSystem}
     */
@Test
public void testDifferentPOIFS() throws Exception {
    // Open the two filesystems
    File file = docTests.getFile("test2.doc");
    InputStream is = new FileInputStream(file);
    OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
    is.close();
    NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
    DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
    // Open directly 
    for (DirectoryNode dir : files) {
        @SuppressWarnings("resource") WordExtractor extractor = new WordExtractor(dir);
        assertEqualsTrim(p_text1_block, extractor.getText());
    // extractor.close();
    }
    // Open via a HWPFDocument
    for (DirectoryNode dir : files) {
        HWPFDocument doc = new HWPFDocument(dir);
        WordExtractor extractor = new WordExtractor(doc);
        assertEqualsTrim(p_text1_block, extractor.getText());
        extractor.close();
    }
    npoifs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 30 with NPOIFSFileSystem

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

the class TestBugs method test51671.

/**
     * [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem
     * throws a NullPointerException
     */
@Test
public void test51671() throws Exception {
    InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("empty.doc");
    NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
    try {
        HWPFDocument hwpfDocument = new HWPFDocument(npoifsFileSystem.getRoot());
        hwpfDocument.write(new ByteArrayOutputStream());
        hwpfDocument.close();
    } finally {
        npoifsFileSystem.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

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