Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZipHelper.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.openxml4j.opc.internal;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.PushbackInputStream;
  22. import java.net.URI;
  23. import java.net.URISyntaxException;
  24. import java.util.Enumeration;
  25. import java.util.zip.ZipEntry;
  26. import java.util.zip.ZipFile;
  27. import java.util.zip.ZipInputStream;
  28. import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
  29. import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
  30. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  31. import org.apache.poi.openxml4j.opc.PackageRelationship;
  32. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  33. import org.apache.poi.openxml4j.opc.ZipPackage;
  34. import org.apache.poi.openxml4j.util.ZipSecureFile;
  35. import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream;
  36. import org.apache.poi.poifs.common.POIFSConstants;
  37. import org.apache.poi.poifs.storage.HeaderBlockConstants;
  38. import org.apache.poi.util.IOUtils;
  39. import org.apache.poi.util.LittleEndian;
  40. public final class ZipHelper {
  41. /**
  42. * Forward slash use to convert part name between OPC and zip item naming
  43. * conventions.
  44. */
  45. private final static String FORWARD_SLASH = "/";
  46. /**
  47. * Buffer to read data from file. Use big buffer to improve performaces. the
  48. * InputStream class is reading only 8192 bytes per read call (default value
  49. * set by sun)
  50. */
  51. public static final int READ_WRITE_FILE_BUFFER_SIZE = 8192;
  52. /**
  53. * Prevent this class to be instancied.
  54. */
  55. private ZipHelper() {
  56. // Do nothing
  57. }
  58. /**
  59. * Retrieve the zip entry of the core properties part.
  60. *
  61. * @throws OpenXML4JException
  62. * Throws if internal error occurs.
  63. */
  64. public static ZipEntry getCorePropertiesZipEntry(ZipPackage pkg) {
  65. PackageRelationship corePropsRel = pkg.getRelationshipsByType(
  66. PackageRelationshipTypes.CORE_PROPERTIES).getRelationship(0);
  67. if (corePropsRel == null)
  68. return null;
  69. return new ZipEntry(corePropsRel.getTargetURI().getPath());
  70. }
  71. /**
  72. * Retrieve the Zip entry of the content types part.
  73. */
  74. public static ZipEntry getContentTypeZipEntry(ZipPackage pkg) {
  75. Enumeration<? extends ZipEntry> entries = pkg.getZipArchive().getEntries();
  76. // Enumerate through the Zip entries until we find the one named
  77. // '[Content_Types].xml'.
  78. while (entries.hasMoreElements()) {
  79. ZipEntry entry = entries.nextElement();
  80. if (entry.getName().equals(
  81. ContentTypeManager.CONTENT_TYPES_PART_NAME))
  82. return entry;
  83. }
  84. return null;
  85. }
  86. /**
  87. * Convert a zip name into an OPC name by adding a leading forward slash to
  88. * the specified item name.
  89. *
  90. * @param zipItemName
  91. * Zip item name to convert.
  92. * @return An OPC compliant name.
  93. */
  94. public static String getOPCNameFromZipItemName(String zipItemName) {
  95. if (zipItemName == null)
  96. throw new IllegalArgumentException("zipItemName");
  97. if (zipItemName.startsWith(FORWARD_SLASH)) {
  98. return zipItemName;
  99. }
  100. return FORWARD_SLASH + zipItemName;
  101. }
  102. /**
  103. * Convert an OPC item name into a zip item name by removing any leading
  104. * forward slash if it exist.
  105. *
  106. * @param opcItemName
  107. * The OPC item name to convert.
  108. * @return A zip item name without any leading slashes.
  109. */
  110. public static String getZipItemNameFromOPCName(String opcItemName) {
  111. if (opcItemName == null)
  112. throw new IllegalArgumentException("opcItemName");
  113. String retVal = opcItemName;
  114. while (retVal.startsWith(FORWARD_SLASH))
  115. retVal = retVal.substring(1);
  116. return retVal;
  117. }
  118. /**
  119. * Convert an OPC item name into a zip URI by removing any leading forward
  120. * slash if it exist.
  121. *
  122. * @param opcItemName
  123. * The OPC item name to convert.
  124. * @return A zip URI without any leading slashes.
  125. */
  126. public static URI getZipURIFromOPCName(String opcItemName) {
  127. if (opcItemName == null)
  128. throw new IllegalArgumentException("opcItemName");
  129. String retVal = opcItemName;
  130. while (retVal.startsWith(FORWARD_SLASH))
  131. retVal = retVal.substring(1);
  132. try {
  133. return new URI(retVal);
  134. } catch (URISyntaxException e) {
  135. return null;
  136. }
  137. }
  138. /**
  139. * Verifies that the given stream starts with a Zip structure.
  140. *
  141. * Warning - this will consume the first few bytes of the stream,
  142. * you should push-back or reset the stream after use!
  143. */
  144. public static void verifyZipHeader(InputStream stream)
  145. throws NotOfficeXmlFileException, IOException {
  146. // Grab the first 8 bytes
  147. byte[] data = new byte[8];
  148. IOUtils.readFully(stream, data);
  149. // OLE2?
  150. long signature = LittleEndian.getLong(data);
  151. if (signature == HeaderBlockConstants._signature) {
  152. throw new OLE2NotOfficeXmlFileException(
  153. "The supplied data appears to be in the OLE2 Format. " +
  154. "You are calling the part of POI that deals with OOXML "+
  155. "(Office Open XML) Documents. You need to call a different " +
  156. "part of POI to process this data (eg HSSF instead of XSSF)");
  157. }
  158. // Raw XML?
  159. byte[] RAW_XML_FILE_HEADER = POIFSConstants.RAW_XML_FILE_HEADER;
  160. if (data[0] == RAW_XML_FILE_HEADER[0] &&
  161. data[1] == RAW_XML_FILE_HEADER[1] &&
  162. data[2] == RAW_XML_FILE_HEADER[2] &&
  163. data[3] == RAW_XML_FILE_HEADER[3] &&
  164. data[4] == RAW_XML_FILE_HEADER[4]) {
  165. throw new NotOfficeXmlFileException(
  166. "The supplied data appears to be a raw XML file. " +
  167. "Formats such as Office 2003 XML are not supported");
  168. }
  169. // Don't check for a Zip header, as to maintain backwards
  170. // compatibility we need to let them seek over junk at the
  171. // start before beginning processing.
  172. // Put things back
  173. if (stream instanceof PushbackInputStream) {
  174. ((PushbackInputStream)stream).unread(data);
  175. } else if (stream.markSupported()) {
  176. stream.reset();
  177. } else if (stream instanceof FileInputStream) {
  178. // File open check, about to be closed, nothing to do
  179. } else {
  180. // Oh dear... I hope you know what you're doing!
  181. }
  182. }
  183. private static InputStream prepareToCheckHeader(InputStream stream) {
  184. if (stream instanceof PushbackInputStream) {
  185. return stream;
  186. }
  187. if (stream.markSupported()) {
  188. stream.mark(8);
  189. return stream;
  190. }
  191. return new PushbackInputStream(stream, 8);
  192. }
  193. /**
  194. * Opens the specified stream as a secure zip
  195. *
  196. * @param stream
  197. * The stream to open.
  198. * @return The zip stream freshly open.
  199. */
  200. @SuppressWarnings("resource")
  201. public static ThresholdInputStream openZipStream(InputStream stream) throws IOException {
  202. // Peek at the first few bytes to sanity check
  203. InputStream checkedStream = prepareToCheckHeader(stream);
  204. verifyZipHeader(checkedStream);
  205. // Open as a proper zip stream
  206. InputStream zis = new ZipInputStream(checkedStream);
  207. return ZipSecureFile.addThreshold(zis);
  208. }
  209. /**
  210. * Opens the specified file as a secure zip, or returns null if no
  211. * such file exists
  212. *
  213. * @param file
  214. * The file to open.
  215. * @return The zip archive freshly open.
  216. * @throws IOException if the zip file cannot be opened or closed to read the header signature
  217. * @throws NotOfficeXmlFileException if stream does not start with zip header signature
  218. */
  219. public static ZipFile openZipFile(File file) throws IOException, NotOfficeXmlFileException {
  220. if (!file.exists()) {
  221. throw new FileNotFoundException("File does not exist");
  222. }
  223. if (file.isDirectory()) {
  224. throw new IOException("File is a directory");
  225. }
  226. // Peek at the first few bytes to sanity check
  227. FileInputStream input = new FileInputStream(file);
  228. try {
  229. verifyZipHeader(input);
  230. } finally {
  231. input.close();
  232. }
  233. // Open as a proper zip file
  234. return new ZipSecureFile(file);
  235. }
  236. /**
  237. * Retrieve and open as a secure zip file with the specified path.
  238. *
  239. * @param path
  240. * The file path.
  241. * @return The zip archive freshly open.
  242. */
  243. public static ZipFile openZipFile(String path) throws IOException {
  244. return openZipFile(new File(path));
  245. }
  246. }