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.

ZipInputStreamZipEntrySource.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.util;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.ArrayList;
  21. import java.util.Enumeration;
  22. import java.util.Iterator;
  23. import java.util.zip.ZipEntry;
  24. import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream;
  25. /**
  26. * Provides a way to get at all the ZipEntries
  27. * from a ZipInputStream, as many times as required.
  28. * Allows a ZipInputStream to be treated much like
  29. * a ZipFile, for a price in terms of memory.
  30. * Be sure to call {@link #close()} as soon as you're
  31. * done, to free up that memory!
  32. */
  33. public class ZipInputStreamZipEntrySource implements ZipEntrySource {
  34. private ArrayList<FakeZipEntry> zipEntries;
  35. /**
  36. * Reads all the entries from the ZipInputStream
  37. * into memory, and closes the source stream.
  38. * We'll then eat lots of memory, but be able to
  39. * work with the entries at-will.
  40. */
  41. public ZipInputStreamZipEntrySource(ThresholdInputStream inp) throws IOException {
  42. zipEntries = new ArrayList<FakeZipEntry>();
  43. boolean going = true;
  44. while(going) {
  45. ZipEntry zipEntry = inp.getNextEntry();
  46. if(zipEntry == null) {
  47. going = false;
  48. } else {
  49. FakeZipEntry entry = new FakeZipEntry(zipEntry, inp);
  50. inp.closeEntry();
  51. zipEntries.add(entry);
  52. }
  53. }
  54. inp.close();
  55. }
  56. public Enumeration<? extends ZipEntry> getEntries() {
  57. return new EntryEnumerator();
  58. }
  59. public InputStream getInputStream(ZipEntry zipEntry) {
  60. assert (zipEntry instanceof FakeZipEntry);
  61. FakeZipEntry entry = (FakeZipEntry)zipEntry;
  62. return entry.getInputStream();
  63. }
  64. public void close() {
  65. // Free the memory
  66. zipEntries = null;
  67. }
  68. public boolean isClosed() {
  69. return (zipEntries == null);
  70. }
  71. /**
  72. * Why oh why oh why are Iterator and Enumeration
  73. * still not compatible?
  74. */
  75. private class EntryEnumerator implements Enumeration<ZipEntry> {
  76. private Iterator<? extends ZipEntry> iterator;
  77. private EntryEnumerator() {
  78. iterator = zipEntries.iterator();
  79. }
  80. public boolean hasMoreElements() {
  81. return iterator.hasNext();
  82. }
  83. public ZipEntry nextElement() {
  84. return iterator.next();
  85. }
  86. }
  87. /**
  88. * So we can close the real zip entry and still
  89. * effectively work with it.
  90. * Holds the (decompressed!) data in memory, so
  91. * close this as soon as you can!
  92. */
  93. public static class FakeZipEntry extends ZipEntry {
  94. private byte[] data;
  95. public FakeZipEntry(ZipEntry entry, InputStream inp) throws IOException {
  96. super(entry.getName());
  97. // Grab the de-compressed contents for later
  98. ByteArrayOutputStream baos;
  99. long entrySize = entry.getSize();
  100. if (entrySize !=-1) {
  101. if (entrySize>=Integer.MAX_VALUE) {
  102. throw new IOException("ZIP entry size is too large");
  103. }
  104. baos = new ByteArrayOutputStream((int) entrySize);
  105. } else {
  106. baos = new ByteArrayOutputStream();
  107. }
  108. byte[] buffer = new byte[4096];
  109. int read = 0;
  110. while( (read = inp.read(buffer)) != -1 ) {
  111. baos.write(buffer, 0, read);
  112. }
  113. data = baos.toByteArray();
  114. }
  115. public InputStream getInputStream() {
  116. return new ByteArrayInputStream(data);
  117. }
  118. }
  119. }