public void TestDeletion() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry root = fs.Root; Assert.IsFalse(root.Delete()); Assert.IsTrue(root.IsEmpty); DirectoryEntry dir = fs.CreateDirectory("myDir"); Assert.IsFalse(root.IsEmpty); Assert.IsTrue(dir.IsEmpty); Assert.IsFalse(root.Delete()); // Verify can Delete empty directory Assert.IsTrue(dir.Delete()); dir = fs.CreateDirectory("NextDir"); DocumentEntry doc = dir.CreateDocument("foo", new MemoryStream(new byte[1])); Assert.IsFalse(root.IsEmpty); Assert.IsFalse(dir.IsEmpty); Assert.IsFalse(dir.Delete()); // Verify cannot Delete empty directory Assert.IsTrue(!dir.Delete()); Assert.IsTrue(doc.Delete()); Assert.IsTrue(dir.IsEmpty); // Verify now we can Delete it Assert.IsTrue(dir.Delete()); Assert.IsTrue(root.IsEmpty); fs.Close(); }
public void TestWriteSimplePropertySet() { String AUTHOR = "Rainer Klute"; String TITLE = "Test Document"; FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc"); FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite); FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); MutableSection si = new MutableSection(); si.SetFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); ps.Sections[0] = si; MutableProperty p = new MutableProperty(); p.ID = PropertyIDMap.PID_AUTHOR; p.Type = Variant.VT_LPWSTR; p.Value = AUTHOR; si.SetProperty(p); si.SetProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE); poiFs.CreateDocument(ps.ToInputStream(), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.WriteFileSystem(out1); poiFs.Close(); //out1.Close(); file.Position = 0; POIFSReader reader1 = new POIFSReader(); //reader1.StreamReaded += new POIFSReaderEventHandler(reader1_StreamReaded); POIFSReaderListener1 psl = new POIFSReaderListener1(); reader1.RegisterListener(psl); reader1.Read(file); Assert.IsNotNull(psa[0]); Assert.IsTrue(psa[0].IsSummaryInformation); Section s = (Section)(psa[0].Sections[0]); Object p1 = s.GetProperty(PropertyIDMap.PID_AUTHOR); Object p2 = s.GetProperty(PropertyIDMap.PID_TITLE); Assert.AreEqual(AUTHOR, p1); Assert.AreEqual(TITLE, p2); file.Close(); try { File.Delete(fi.FullName); } catch { } }
public void ProtectedTempZip() { FileInfo tmpFile = TempFile.CreateTempFile("protectedXlsx", ".zip"); FileInfo tikaProt = XSSFTestDataSamples.GetSampleFile("protected_passtika.xlsx"); FileInputStream fis = new FileInputStream(tikaProt.Open(FileMode.Open)); POIFSFileSystem poifs = new POIFSFileSystem(fis); EncryptionInfo ei = new EncryptionInfo(poifs); Decryptor dec = ei.Decryptor; bool passOk = dec.VerifyPassword("tika"); Assert.IsTrue(passOk); // generate session key SecureRandom sr = new SecureRandom(); byte[] ivBytes = new byte[16], keyBytes = new byte[16]; sr.NextBytes(ivBytes); sr.NextBytes(keyBytes); // extract encrypted ooxml file and write to custom encrypted zip file InputStream is1 = dec.GetDataStream(poifs); CopyToFile(is1, tmpFile, CipherAlgorithm.aes128, keyBytes, ivBytes); is1.Close(); // provide ZipEntrySource to poi which decrypts on the fly ZipEntrySource source = fileToSource(tmpFile, CipherAlgorithm.aes128, keyBytes, ivBytes); // test the source OPCPackage opc = OPCPackage.Open(source); String expected = "This is an Encrypted Excel spreadsheet."; //XSSFEventBasedExcelExtractor extractor = new XSSFEventBasedExcelExtractor(opc); //extractor.IncludeSheetNames = (/*setter*/false); //String txt = extractor.Text; //Assert.AreEqual(expected, txt.Trim()); //XSSFWorkbook wb = new XSSFWorkbook(opc); //txt = wb.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue; //Assert.AreEqual(expected, txt); //extractor.Close(); //wb.Close(); opc.Close(); source.Close(); poifs.Close(); fis.Close(); tmpFile.Delete(); throw new NotImplementedException(); }
/** * Performs the check described in {@link #TestReCreate()} for a single * POI filesystem. * * @param f the POI filesystem to check */ private void TestRecreate(FileInfo f) { Console.WriteLine("Recreating file \"" + f.Name + "\""); /* Read the POI filesystem's property Set streams: */ POIFile[] psf1 = Util.ReadPropertySets(f); /* Create a new POI filesystem containing the origin file's * property Set streams: */ FileInfo copy = TempFile.CreateTempFile(f.Name, ""); //File.Create(Path.Combine(TestContext.CurrentContext.TestDirectory,f.Name)); //copy.deleteOnExit(); FileStream out1 = copy.OpenWrite(); POIFSFileSystem poiFs = new POIFSFileSystem(); for (int i = 0; i < psf1.Length; i++) { Stream in1 = new ByteArrayInputStream(psf1[i].GetBytes()); PropertySet psIn = PropertySetFactory.Create(in1); MutablePropertySet psOut = new MutablePropertySet(psIn); MemoryStream psStream = new MemoryStream(); psOut.Write(psStream); psStream.Close(); byte[] streamData = psStream.ToArray(); poiFs.CreateDocument(new ByteArrayInputStream(streamData), psf1[i].GetName()); poiFs.WriteFileSystem(out1); } poiFs.Close(); out1.Close(); /* Read the property Set streams from the POI filesystem just * Created. */ POIFile[] psf2 = Util.ReadPropertySets(copy); for (int i = 0; i < psf2.Length; i++) { byte[] bytes1 = psf1[i].GetBytes(); byte[] bytes2 = psf2[i].GetBytes(); Stream in1 = new ByteArrayInputStream(bytes1); Stream in2 = new ByteArrayInputStream(bytes2); PropertySet ps1 = PropertySetFactory.Create(in1); PropertySet ps2 = PropertySetFactory.Create(in2); /* Compare the property Set stream with the corresponding one * from the origin file and check whether they are equal. */ Assert.AreEqual(ps1, ps2, "Equality for file " + f.Name); } out1.Close(); }
/** * Write out, with any properties changes, but nothing else */ public override void Write(FileInfo newFile) { POIFSFileSystem fs = POIFSFileSystem.Create(newFile); try { Write(fs); fs.WriteFileSystem(); } finally { fs.Close(); } }
public void TestWithoutAFormatID() { FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc"); using (FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite)) { //FileStream filename = File.OpenRead(dataDir + POI_FS); //filename.deleteOnExit(); /* Create a mutable property Set with a section that does not have the * formatID Set: */ FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); ps.ClearSections(); ps.AddSection(new MutableSection()); /* Write it to a POIFS and the latter to disk: */ try { MemoryStream psStream = new MemoryStream(); ps.Write(psStream); psStream.Close(); byte[] streamData = psStream.ToArray(); poiFs.CreateDocument(new MemoryStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.WriteFileSystem(out1); out1.Close(); Assert.Fail("Should have thrown a NoFormatIDException."); } catch (Exception ex) { Assert.IsTrue(ex is NoFormatIDException); } finally { poiFs.Close(); out1.Close(); } } try { File.Delete(fi.FullName); } catch { } }
public void TestEncryptionInfoSHA512() { POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_sha512.xlsx")); EncryptionInfo info = new EncryptionInfo(fs); Assert.AreEqual(4, info.VersionMajor); Assert.AreEqual(4, info.VersionMinor); Assert.AreEqual(CipherAlgorithm.aes256, info.Header.CipherAlgorithm); Assert.AreEqual(HashAlgorithm.sha512, info.Header.HashAlgorithm); Assert.AreEqual(256, info.Header.KeySize); Assert.AreEqual(64, info.Verifier.EncryptedVerifierHash.Length); Assert.AreEqual(CipherProvider.aes, info.Header.CipherProvider); // Assert.AreEqual("Microsoft Enhanced RSA and AES Cryptographic Provider", info.Header.CspName); fs.Close(); }
public void TestWriteEmptyPropertySet() { FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc"); using (FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite)) { //filename.deleteOnExit(); /* Create a mutable property Set and Write it to a POIFS: */ FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); MutableSection s = (MutableSection)ps.Sections[0]; s.SetFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); MemoryStream psStream = new MemoryStream(); ps.Write(psStream); psStream.Close(); byte[] streamData = psStream.ToArray(); poiFs.CreateDocument(new MemoryStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.WriteFileSystem(out1); //out1.Close(); file.Position = 0; /* Read the POIFS: */ POIFSReader reader3 = new POIFSReader(); reader3.StreamReaded += new POIFSReaderEventHandler(reader3_StreamReaded); reader3.Read(file); poiFs.Close(); file.Close(); //File.Delete(dataDir + POI_FS); } try { File.Delete(fi.FullName); } catch { } }
public void TestDictionary() { using (FileStream copy = File.Create(@".\Test-HPSF.ole2")) { /* Write: */ POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps1 = new MutablePropertySet(); MutableSection s = (MutableSection)ps1.Sections[0]; Hashtable m = new Hashtable(3, 1.0f); m[1] = "String 1"; m[2] = "String 2"; m[3] = "String 3"; s.Dictionary = (m); s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1); int codepage = CodePageUtil.CP_UNICODE; s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage); poiFs.CreateDocument(ps1.ToInputStream(), "Test"); poiFs.WriteFileSystem(copy); poiFs.Close(); /* Read back: */ POIFile[] psf = Util.ReadPropertySets(copy); Assert.AreEqual(1, psf.Length); byte[] bytes = psf[0].GetBytes(); Stream in1 = new ByteArrayInputStream(bytes); PropertySet ps2 = PropertySetFactory.Create(in1); /* Check if the result is a DocumentSummaryInformation stream, as * specified. */ Assert.IsTrue(ps2.IsDocumentSummaryInformation); /* Compare the property Set stream with the corresponding one * from the origin file and check whether they are equal. */ Assert.IsTrue(ps1.Equals(ps2)); } if (File.Exists(@".\Test-HPSF.ole2")) { File.Delete(@".\Test-HPSF.ole2"); } }
public void TestRename() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry root = fs.Root; // Verify cannot Rename the root directory Assert.IsTrue(!root.RenameTo("foo")); DirectoryEntry dir = fs.CreateDirectory("myDir"); Assert.IsTrue(dir.RenameTo("foo")); Assert.AreEqual("foo", dir.Name); DirectoryEntry dir2 = fs.CreateDirectory("myDir"); Assert.IsTrue(!dir2.RenameTo("foo")); Assert.AreEqual("myDir", dir2.Name); Assert.IsTrue(dir.RenameTo("FirstDir")); Assert.IsTrue(dir2.RenameTo("foo")); Assert.AreEqual("foo", dir2.Name); fs.Close(); }
public void TestDictionaryWithInvalidCodepage() { using (FileStream copy = File.Create(@".\Test-HPSF.ole2")) { /* Write: */ POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps1 = new MutablePropertySet(); MutableSection s = (MutableSection)ps1.Sections[0]; Hashtable m = new Hashtable(3, 1.0f); m[1] = "String 1"; m[2] = "String 2"; m[3] = "String 3"; try { Assert.Throws <IllegalPropertySetDataException>(() => { s.Dictionary = m; s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1); s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, 12345); poiFs.CreateDocument(ps1.ToInputStream(), "Test"); poiFs.WriteFileSystem(copy); }); } finally { poiFs.Close(); } } if (File.Exists(@".\Test-HPSF.ole2")) { File.Delete(@".\Test-HPSF.ole2"); } }
public void TestWriteTwoSections() { String STREAM_NAME = "PropertySetStream"; String SECTION1 = "Section 1"; String SECTION2 = "Section 2"; FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc"); FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite); //filename.deleteOnExit(); FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); ps.ClearSections(); ClassID formatID = new ClassID(); formatID.Bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; MutableSection s1 = new MutableSection(); s1.SetFormatID(formatID); s1.SetProperty(2, SECTION1); ps.AddSection(s1); MutableSection s2 = new MutableSection(); s2.SetFormatID(formatID); s2.SetProperty(2, SECTION2); ps.AddSection(s2); poiFs.CreateDocument(ps.ToInputStream(), STREAM_NAME); poiFs.WriteFileSystem(out1); poiFs.Close(); //out1.Close(); /* Read the POIFS: */ psa = new PropertySet[1]; POIFSReader reader2 = new POIFSReader(); //reader2.StreamReaded += new POIFSReaderEventHandler(reader2_StreamReaded); POIFSReaderListener2 prl = new POIFSReaderListener2(); reader2.RegisterListener(prl); reader2.Read(file); Assert.IsNotNull(psa[0]); Section s = (Section)(psa[0].Sections[0]); Assert.AreEqual(s.FormatID, formatID); Object p = s.GetProperty(2); Assert.AreEqual(SECTION1, p); s = (Section)(psa[0].Sections[1]); p = s.GetProperty(2); Assert.AreEqual(SECTION2, p); file.Close(); //File.Delete(dataDir + POI_FS); try { File.Delete(fi.FullName); } catch { } }
public void TestReallyEmbedSomething() { HSSFWorkbook wb1 = new HSSFWorkbook(); ISheet sheet = wb1.CreateSheet(); HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch; byte[] pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png"); byte[] picturePPT = POIDataSamples.GetSlideShowInstance().ReadFile("clock.jpg"); int imgIdx = wb1.AddPicture(pictureData, PictureType.PNG); POIFSFileSystem pptPoifs = GetSamplePPT(); int pptIdx = wb1.AddOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt"); POIFSFileSystem xlsPoifs = GetSampleXLS(); int imgPPT = wb1.AddPicture(picturePPT, PictureType.JPEG); int xlsIdx = wb1.AddOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls"); int txtIdx = wb1.AddOlePackage(GetSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt"); int rowoffset = 5; int coloffset = 5; ICreationHelper ch = wb1.GetCreationHelper(); HSSFClientAnchor anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(2 + coloffset), 1 + rowoffset, 0, 0, (short)(3 + coloffset), 5 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, pptIdx, imgPPT); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(5 + coloffset), 1 + rowoffset, 0, 0, (short)(6 + coloffset), 5 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, xlsIdx, imgIdx); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(3 + coloffset), 10 + rowoffset, 0, 0, (short)(5 + coloffset), 11 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); patriarch.CreateObjectData(anchor, txtIdx, imgIdx); anchor = (HSSFClientAnchor)ch.CreateClientAnchor(); anchor.SetAnchor((short)(1 + coloffset), -2 + rowoffset, 0, 0, (short)(7 + coloffset), 14 + rowoffset, 0, 0); anchor.AnchorType = (/*setter*/ AnchorType.DontMoveAndResize); HSSFSimpleShape circle = patriarch.CreateSimpleShape(anchor); circle.ShapeType = (/*setter*/ HSSFSimpleShape.OBJECT_TYPE_OVAL); circle.IsNoFill = (/*setter*/ true); //if (false) //{ // FileStream fos = new FileStream("embed.xls", FileMode.Create); // wb.Write(fos); // fos.Close(); //} HSSFWorkbook wb2 = HSSFTestDataSamples.WriteOutAndReadBack(wb1 as HSSFWorkbook); wb1.Close(); MemoryStream bos = new MemoryStream(); HSSFObjectData od = wb2.GetAllEmbeddedObjects()[0]; Ole10Native ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); bos = new MemoryStream(); pptPoifs.WriteFileSystem(bos); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray())); od = wb2.GetAllEmbeddedObjects()[1]; ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); bos = new MemoryStream(); xlsPoifs.WriteFileSystem(bos); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, bos.ToArray())); od = wb2.GetAllEmbeddedObjects()[2]; ole10 = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)od.GetDirectory()); Assert.IsTrue(Arrays.Equals(ole10.DataBuffer, GetSampleTXT())); xlsPoifs.Close(); pptPoifs.Close(); wb2.Close(); }