Esempio n. 1
0
        public void TestOpenPackage()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestOpenPackageTMP.docx");

            FileInfo inputFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageINPUT.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageOUTPUT.docx");

            // Copy the input file in the output directory
            FileHelper.CopyFile(inputFile.FullName, targetFile.FullName);

            // Create a namespace
            OPCPackage pkg = OPCPackage.Open(targetFile.FullName);

            // Modify core part
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            PackagePart corePart = pkg.GetPart(corePartName);

            // Delete some part to have a valid document
            foreach (PackageRelationship rel in corePart.Relationships)
            {
                corePart.RemoveRelationship(rel.Id);
                pkg.RemovePart(PackagingUriHelper.CreatePartName(PackagingUriHelper
                                                                 .ResolvePartUri(corePart.PartName.URI, rel
                                                                                 .TargetUri)));
            }

            //// Create a content
            //Document doc = DocumentHelper.CreateDocument();
            //Namespace nsWordProcessinML = new Namespace("w",
            //        "http://schemas.openxmlformats.org/wordProcessingml/2006/main");
            //Element elDocument = doc.AddElement(new QName("document",
            //        nsWordProcessinML));
            //Element elBody = elDocument.AddElement(new QName("body",
            //        nsWordProcessinML));
            //Element elParagraph = elBody.AddElement(new QName("p",
            //        nsWordProcessinML));
            //Element elRun = elParagraph
            //        .AddElement(new QName("r", nsWordProcessinML));
            //Element elText = elRun.AddElement(new QName("t", nsWordProcessinML));
            //elText.SetText("Hello Open XML !");

            //StreamHelper.saveXmlInStream(doc, corePart.GetOutputStream());

            //// Save and close
            //try
            //{
            //    pkg.Close();
            //}
            //catch (IOException e)
            //{
            //    Assert.Fail();
            //}

            //ZipFileAssert.AssertEqual(expectedFile, targetFile);
            //File.Delete(targetFile.FullName);
        }
Esempio n. 2
0
        public void TestCreatePackageAddPart()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestCreatePackageTMP.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestCreatePackageOUTPUT.docx");

            // Zap the target file, in case of an earlier run
            if (targetFile.Exists)
            {
                targetFile.Delete();
                targetFile.Refresh();
                Assert.IsFalse(targetFile.Exists);
            }

            // Create a namespace
            OPCPackage      pkg          = OPCPackage.Create(targetFile.FullName);
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            pkg.AddRelationship(corePartName, TargetMode.Internal,
                                PackageRelationshipTypes.CORE_DOCUMENT, "rId1");

            PackagePart corePart = pkg
                                   .CreatePart(
                corePartName,
                "application/vnd.openxmlformats-officedocument.wordProcessingml.document.main+xml");

            XmlDocument doc = new XmlDocument();

            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            string wuri             = "http://schemas.openxmlformats.org/wordProcessingml/2006/main";

            mgr.AddNamespace("w", wuri);
            XmlElement elDocument = doc.CreateElement("w:document", wuri);

            doc.AppendChild(elDocument);
            XmlElement elBody = doc.CreateElement("w:body", wuri);

            elDocument.AppendChild(elBody);
            XmlElement elParagraph = doc.CreateElement("w:p", wuri);

            elBody.AppendChild(elParagraph);
            XmlElement elRun = doc.CreateElement("w:r", wuri);

            elParagraph.AppendChild(elRun);
            XmlElement elText = doc.CreateElement("w:t", wuri);

            elRun.AppendChild(elText);
            elText.InnerText = ("Hello Open XML !");

            StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            pkg.Close();

            ZipFileAssert.AssertEqual(expectedFile, targetFile);
            File.Delete(targetFile.FullName);
        }
Esempio n. 3
0
        public void TestCreatePackageAddPart()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestCreatePackageTMP.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestCreatePackageOUTPUT.docx");

            // Zap the target file, in case of an earlier run
            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            // Create a namespace
            OPCPackage      pkg          = OPCPackage.Create(targetFile.FullName);
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            pkg.AddRelationship(corePartName, TargetMode.Internal,
                                PackageRelationshipTypes.CORE_DOCUMENT, "rId1");

            PackagePart corePart = pkg
                                   .CreatePart(
                corePartName,
                "application/vnd.openxmlformats-officedocument.wordProcessingml.document.main+xml");

            //Document doc = DocumentHelper.CreateDocument();
            //Namespace nsWordProcessinML = new Namespace("w",
            //        "http://schemas.openxmlformats.org/wordProcessingml/2006/main");
            //Element elDocument = doc.AddElement(new QName("document",
            //        nsWordProcessinML));
            //Element elBody = elDocument.AddElement(new QName("body",
            //        nsWordProcessinML));
            //Element elParagraph = elBody.AddElement(new QName("p",
            //        nsWordProcessinML));
            //Element elRun = elParagraph
            //        .AddElement(new QName("r", nsWordProcessinML));
            //Element elText = elRun.AddElement(new QName("t", nsWordProcessinML));
            //elText.SetText("Hello Open XML !");

            //StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            //pkg.Close();

            //ZipFileAssert.AssertEqual(expectedFile, targetFile);
            //File.Delete(targetFile.FullName);
        }
Esempio n. 4
0
        public void TestOpenFileThenSaveDelete()
        {
            string   tempFile  = TempFile.GetTempFilePath("poiTesting", "tmp");
            string   tempFile2 = TempFile.GetTempFilePath("poiTesting", "tmp");
            FileInfo origFile  = OpenXml4NetTestDataSamples.GetSampleFile("TestPackageCommon.docx");

            FileHelper.CopyFile(origFile.FullName, tempFile);

            // Open the temp file
            OPCPackage p = OPCPackage.Open(tempFile, PackageAccess.READ_WRITE);

            // Save it to a different file
            p.Save(tempFile2);
            p.Close();

            // Delete both the files
            File.Delete(tempFile);
            File.Delete(tempFile2);
        }
Esempio n. 5
0
        public void TestOpenFileThenSaveDelete()
        {
            string   tempFile  = TempFile.GetTempFilePath("poiTesting", "tmp");
            string   tempFile2 = TempFile.GetTempFilePath("poiTesting", "tmp");
            FileInfo origFile  = OpenXml4NetTestDataSamples.GetSampleFile("TestPackageCommon.docx");

            FileHelper.CopyFile(origFile.FullName, tempFile);

            // Open the temp file
            OPCPackage p = OPCPackage.Open(tempFile, PackageAccess.READ_WRITE);

            // Save it to a different file
            p.Save(tempFile2);
            p.Close();

            // Delete both the files
            File.Delete(tempFile);
            File.Delete(tempFile2);

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }
Esempio n. 6
0
        public void TestOpenFileThenOverWrite()
        {
            string   tempFile = TempFile.GetTempFilePath("poiTesting", "tmp");
            FileInfo origFile = OpenXml4NetTestDataSamples.GetSampleFile("TestPackageCommon.docx");

            FileHelper.CopyFile(origFile.FullName, tempFile);

            // Open the temp file
            OPCPackage p = OPCPackage.Open(tempFile, PackageAccess.READ_WRITE);

            // Close it
            p.Close();
            // Delete it
            File.Delete(tempFile);

            // Reset
            FileHelper.CopyFile(origFile.FullName, tempFile);
            p = OPCPackage.Open(tempFile, PackageAccess.READ_WRITE);

            // Save it to the same file - not allowed
            try
            {
                p.Save(tempFile);
                Assert.Fail("You shouldn't be able to call save(File) to overwrite the current file");
            }
            catch (IOException) { }

            p.Close();
            // Delete it
            File.Delete(tempFile);


            // Open it read only, then close and delete - allowed
            FileHelper.CopyFile(origFile.FullName, tempFile);
            p = OPCPackage.Open(tempFile, PackageAccess.READ);
            p.Close();
            File.Delete(tempFile);

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }
Esempio n. 7
0
        public void TestCorruptFile()
        {
            OPCPackage pkg  = null;
            FileInfo   file = OpenXml4NetTestDataSamples.GetSampleFile("invalid.xlsx");

            try
            {
                pkg = OPCPackage.Open(file, PackageAccess.READ);
            }
            catch (Exception e)
            {
                //System.out.println(e.GetClass().getName());
                //System.out.println(e.GetMessage());
                //e.printStackTrace();
            }
            finally
            {
                if (pkg != null)
                {
                    pkg.Close();
                }
            }
        }
Esempio n. 8
0
        public void TestOpenPackage()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestOpenPackageTMP.docx");

            FileInfo inputFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageINPUT.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageOUTPUT.docx");

            // Copy the input file in the output directory
            FileHelper.CopyFile(inputFile.FullName, targetFile.FullName);

            // Create a namespace
            OPCPackage pkg = OPCPackage.Open(targetFile.FullName);

            // Modify core part
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            PackagePart corePart = pkg.GetPart(corePartName);

            // Delete some part to have a valid document
            foreach (PackageRelationship rel in corePart.Relationships)
            {
                corePart.RemoveRelationship(rel.Id);
                pkg.RemovePart(PackagingUriHelper.CreatePartName(PackagingUriHelper
                                                                 .ResolvePartUri(corePart.PartName.URI, rel
                                                                                 .TargetUri)));
            }

            // Create a content
            XmlDocument doc = new XmlDocument();

            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            string wuri             = "http://schemas.openxmlformats.org/wordProcessingml/2006/main";

            mgr.AddNamespace("w", wuri);
            XmlElement elDocument = doc.CreateElement("w:document", wuri);

            doc.AppendChild(elDocument);
            XmlElement elBody = doc.CreateElement("w:body", wuri);

            elDocument.AppendChild(elBody);
            XmlElement elParagraph = doc.CreateElement("w:p", wuri);

            elBody.AppendChild(elParagraph);
            XmlElement elRun = doc.CreateElement("w:r", wuri);

            elParagraph.AppendChild(elRun);
            XmlElement elText = doc.CreateElement("w:t", wuri);

            elRun.AppendChild(elText);
            elText.InnerText = ("Hello Open XML !");

            StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            // Save and close
            try
            {
                pkg.Close();
            }
            catch (IOException)
            {
                Assert.Fail();
            }

            ZipFileAssert.AssertEqual(expectedFile, targetFile);
            File.Delete(targetFile.FullName);

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }
Esempio n. 9
0
        public void ZipBombCreateAndHandle()
        {
            // #50090 / #56865
            ZipFile zipFile = ZipHelper.OpenZipFile(OpenXml4NetTestDataSamples.GetSampleFile("sample.xlsx"));

            Assert.IsNotNull(zipFile);

            ByteArrayOutputStream bos    = new ByteArrayOutputStream(2500000);
            ZipOutputStream       append = new ZipOutputStream(bos);
            // first, copy contents from existing war
            IEnumerator entries = zipFile.GetEnumerator();

            while (entries.MoveNext())
            {
                ZipEntry e2 = (ZipEntry)entries.Current;
                ZipEntry e  = new ZipEntry(e2.Name);

                e.DateTime = (e2.DateTime);
                e.Comment  = (e2.Comment);
                e.Size     = (e2.Size);

                append.PutNextEntry(e);
                if (!e.IsDirectory)
                {
                    Stream is1 = zipFile.GetInputStream(e);
                    if (e.Name.Equals("[Content_Types].xml"))
                    {
                        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
                        IOUtils.Copy(is1, bos2);
                        long size = bos2.Length - "</Types>".Length;
                        append.Write(bos2.ToByteArray(), 0, (int)size);
                        byte[] spam = new byte[0x7FFF];
                        for (int i = 0; i < spam.Length; i++)
                        {
                            spam[i] = (byte)' ';
                        }
                        // 0x7FFF0000 is the maximum for 32-bit zips, but less still works
                        while (size < 0x7FFF0000)
                        {
                            append.Write(spam, 0, spam.Length);
                            size += spam.Length;
                        }
                        append.Write(Encoding.ASCII.GetBytes("</Types>"), 0, 8);
                        size  += 8;
                        e.Size = (size);
                    }
                    else
                    {
                        IOUtils.Copy(is1, append);
                    }
                }
                append.CloseEntry();
            }

            append.Close();
            zipFile.Close();

            byte[] buf = bos.ToByteArray();
            bos = null;

            IWorkbook wb = WorkbookFactory.Create(new ByteArrayInputStream(buf));

            wb.GetSheetAt(0);
            wb.Close();
            zipFile.Close();
        }
Esempio n. 10
0
 public void UnparseableCentralDirectory()
 {
     FileInfo f = OpenXml4NetTestDataSamples.GetSampleFile("at.pzp.www_uploads_media_PP_Scheinecker-jdk6error.pptx");
     //SlideShow<?,?> ppt = SlideShowFactory.create(f, null, true);
     //ppt.close();
 }