You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OPCFileHandler.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.stress;
  16. import static org.junit.Assert.assertEquals;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.InputStream;
  20. import java.io.PushbackInputStream;
  21. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  22. import org.apache.poi.openxml4j.opc.ContentTypes;
  23. import org.apache.poi.openxml4j.opc.OPCPackage;
  24. import org.apache.poi.openxml4j.opc.PackagePart;
  25. import org.apache.poi.xwpf.usermodel.XWPFRelation;
  26. import org.junit.Test;
  27. public class OPCFileHandler extends AbstractFileHandler {
  28. @Override
  29. public void handleFile(InputStream stream) throws Exception {
  30. // ignore password protected files
  31. if (POIXMLDocumentHandler.isEncrypted(stream)) return;
  32. InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
  33. OPCPackage p = OPCPackage.open(is);
  34. for (PackagePart part : p.getParts()) {
  35. if (part.getPartName().toString().equals("/docProps/core.xml")) {
  36. assertEquals(ContentTypes.CORE_PROPERTIES_PART, part.getContentType());
  37. }
  38. if (part.getPartName().toString().equals("/word/document.xml")) {
  39. assertEquals(XWPFRelation.DOCUMENT.getContentType(), part.getContentType());
  40. }
  41. if (part.getPartName().toString().equals("/word/theme/theme1.xml")) {
  42. assertEquals(XWPFRelation.THEME.getContentType(), part.getContentType());
  43. }
  44. }
  45. }
  46. public void handleExtracting(File file) throws Exception {
  47. // text-extraction is not possible currenlty for these types of files
  48. }
  49. // a test-case to test this locally without executing the full TestAllFiles
  50. @Test
  51. public void test() throws Exception {
  52. File file = new File("test-data/openxml4j/dcterms_bug_56479.zip");
  53. InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
  54. try {
  55. handleFile(stream);
  56. } finally {
  57. stream.close();
  58. }
  59. handleExtracting(file);
  60. }
  61. }