aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
blob: 96484760019beb2cc18ad1967d01e307cc083da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.openxml4j.util;

import java.io.*;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.poifs.crypt.temp.EncryptedTempData;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;

/**
 * So we can close the real zip entry and still
 *  effectively work with it.
 * Holds the (decompressed!) data in memory (or since POI 5.1.0, possibly in a temp file), so
 *  close this as soon as you can!
 * @see ZipInputStreamZipEntrySource#setThresholdBytesForTempFiles(int)
 */
public final class ZipArchiveFakeEntry extends ZipArchiveEntry implements Closeable {
    private static final Logger LOG = PoiLogManager.getLogger(ZipArchiveFakeEntry.class);

    // how large a single entry in a zip-file should become at max
    // can be overwritten via IOUtils.setByteArrayMaxOverride()
    private static final int DEFAULT_MAX_ENTRY_SIZE = 100_000_000;
    private static int MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE;

    /**
     * Set the maximum size of a single entry in a zip-file.
     * @param maxEntrySize number of bytes at which a zip entry is regarded as too large for holding in memory
     *                     - defaults to 100_000_000 (approx 100Mb). A value of -1 means the default value is used.
     */
    public static void setMaxEntrySize(int maxEntrySize) {
        if(maxEntrySize < 0) {
            MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE;
        } else {
            MAX_ENTRY_SIZE = maxEntrySize;
        }
    }

    public static int getMaxEntrySize() {
        final int ioMaxSize = IOUtils.getByteArrayMaxOverride();
        return ioMaxSize < 0 ? MAX_ENTRY_SIZE : Math.min(MAX_ENTRY_SIZE, ioMaxSize);
    }

    private byte[] data;
    private File tempFile;
    private EncryptedTempData encryptedTempData;

    ZipArchiveFakeEntry(ZipArchiveEntry entry, InputStream inp) throws IOException {
        super(entry.getName());

        final long entrySize = entry.getSize();

        final int threshold = ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles();
        if (threshold >= 0 && (entrySize >= threshold || entrySize == -1)) {
            if (ZipInputStreamZipEntrySource.shouldEncryptTempFiles()) {
                encryptedTempData = new EncryptedTempData();
                try (OutputStream os = encryptedTempData.getOutputStream()) {
                    IOUtils.copy(inp, os);
                }
            } else {
                tempFile = TempFile.createTempFile("poi-zip-entry", ".tmp");
                LOG.atInfo().log("Creating temp file {} for zip entry {} of size {} bytes",
                        tempFile.getAbsolutePath(), entry.getName(), entrySize);
                IOUtils.copy(inp, tempFile);
            }
        } else {
            if (entrySize < -1 || entrySize >= Integer.MAX_VALUE) {
                throw new IOException("ZIP entry size is too large or invalid");
            }

            // Grab the de-compressed contents for later
            data = (entrySize == -1) ? IOUtils.toByteArrayWithMaxLength(inp, getMaxEntrySize()) :
                    IOUtils.toByteArray(inp, entrySize, getMaxEntrySize());
        }
    }

    /**
     * Returns zip entry.
     * @return input stream
     * @throws IOException since POI 5.2.0,
     * an IOException can occur if the optional temp file has been removed (was a RuntimeException in POI 5.1.0)
     * @see ZipInputStreamZipEntrySource#setThresholdBytesForTempFiles(int)
     */
    public InputStream getInputStream() throws IOException {
        if (encryptedTempData != null) {
            try {
                return encryptedTempData.getInputStream();
            } catch (IOException e) {
                throw new IOException("failed to read from encrypted temp data", e);
            }
        } else if (tempFile != null) {
            try {
                return Files.newInputStream(tempFile.toPath());
            } catch (FileNotFoundException e) {
                throw new IOException("temp file " + tempFile.getAbsolutePath() + " is missing");
            }
        } else if (data != null) {
            return UnsynchronizedByteArrayInputStream.builder().setByteArray(data).get();
        } else {
            throw new IOException("Cannot retrieve data from Zip Entry, probably because the Zip Entry was closed before the data was requested.");
        }
    }

    /**
     * Deletes any temp files and releases any byte arrays.
     * @throws IOException If closing the entry fails.
     * @since POI 5.1.0
     */
    @Override
    public void close() throws IOException {
        data = null;
        if (encryptedTempData != null) {
            encryptedTempData.dispose();
        }
        if (tempFile != null && tempFile.exists()) {
            if (!tempFile.delete()) {
                LOG.atDebug().log("temp file was already deleted (probably due to previous call to close this resource)");
            }
        }
    }
}