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.

DataExtraction.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.examples.hslf;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import org.apache.poi.hslf.usermodel.HSLFObjectData;
  21. import org.apache.poi.hslf.usermodel.HSLFObjectShape;
  22. import org.apache.poi.hslf.usermodel.HSLFPictureData;
  23. import org.apache.poi.hslf.usermodel.HSLFPictureShape;
  24. import org.apache.poi.hslf.usermodel.HSLFShape;
  25. import org.apache.poi.hslf.usermodel.HSLFSlide;
  26. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  27. import org.apache.poi.hslf.usermodel.HSLFSoundData;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.hwpf.HWPFDocument;
  30. import org.apache.poi.hwpf.usermodel.Paragraph;
  31. import org.apache.poi.hwpf.usermodel.Range;
  32. import org.apache.poi.util.IOUtils;
  33. /**
  34. * Demonstrates how you can extract misc embedded data from a ppt file
  35. */
  36. @SuppressWarnings({"java:S106","java:S4823"})
  37. public final class DataExtraction {
  38. private DataExtraction() {}
  39. public static void main(String[] args) throws Exception {
  40. if (args.length == 0) {
  41. usage();
  42. return;
  43. }
  44. try (FileInputStream fis = new FileInputStream(args[0]);
  45. HSLFSlideShow ppt = new HSLFSlideShow(fis)) {
  46. //extract all sound files embedded in this presentation
  47. HSLFSoundData[] sound = ppt.getSoundData();
  48. for (HSLFSoundData aSound : sound) {
  49. handleSound(aSound);
  50. }
  51. int oleIdx = -1;
  52. int picIdx = -1;
  53. for (HSLFSlide slide : ppt.getSlides()) {
  54. //extract embedded OLE documents
  55. for (HSLFShape shape : slide.getShapes()) {
  56. if (shape instanceof HSLFObjectShape) {
  57. handleShape((HSLFObjectShape) shape, ++oleIdx);
  58. } else if (shape instanceof HSLFPictureShape) {
  59. handlePicture((HSLFPictureShape) shape, ++picIdx);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. private static void handleShape(HSLFObjectShape ole, int oleIdx) throws IOException {
  66. HSLFObjectData data = ole.getObjectData();
  67. String name = ole.getInstanceName();
  68. switch (name == null ? "" : name) {
  69. case "Worksheet":
  70. //read xls
  71. handleWorkbook(data, name, oleIdx);
  72. break;
  73. case "Document":
  74. //read the word document
  75. handleDocument(data, name, oleIdx);
  76. break;
  77. default:
  78. handleUnknown(data, ole.getProgId(), oleIdx);
  79. break;
  80. }
  81. }
  82. private static void handleWorkbook(HSLFObjectData data, String name, int oleIdx) throws IOException {
  83. try (InputStream is = data.getInputStream();
  84. HSSFWorkbook wb = new HSSFWorkbook(is);
  85. FileOutputStream out = new FileOutputStream(name + "-(" + (oleIdx) + ").xls")) {
  86. wb.write(out);
  87. }
  88. }
  89. private static void handleDocument(HSLFObjectData data, String name, int oleIdx) throws IOException {
  90. try (InputStream is = data.getInputStream();
  91. HWPFDocument doc = new HWPFDocument(is);
  92. FileOutputStream out = new FileOutputStream(name + "-(" + (oleIdx) + ").doc")) {
  93. Range r = doc.getRange();
  94. for (int k = 0; k < r.numParagraphs(); k++) {
  95. Paragraph p = r.getParagraph(k);
  96. System.out.println(p.text());
  97. }
  98. //save on disk
  99. doc.write(out);
  100. }
  101. }
  102. private static void handleUnknown(HSLFObjectData data, String name, int oleIdx) throws IOException {
  103. try (InputStream is = data.getInputStream();
  104. FileOutputStream out = new FileOutputStream(name + "-" + (oleIdx + 1) + ".dat")) {
  105. IOUtils.copy(is, out);
  106. }
  107. }
  108. private static void handlePicture(HSLFPictureShape p, int picIdx) throws IOException {
  109. HSLFPictureData data = p.getPictureData();
  110. String ext = data.getType().extension;
  111. try (FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext)) {
  112. out.write(data.getData());
  113. }
  114. }
  115. private static void handleSound(HSLFSoundData aSound) throws IOException {
  116. String type = aSound.getSoundType(); //*.wav
  117. String name = aSound.getSoundName(); //typically file name
  118. //save the sound on disk
  119. try (FileOutputStream out = new FileOutputStream(name + type)) {
  120. out.write(aSound.getData());
  121. }
  122. }
  123. private static void usage(){
  124. System.out.println("Usage: DataExtraction ppt");
  125. }
  126. }