aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/Zip64Impl.java
blob: 647d94d4365358dd58ae5321a15678c600247e60 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* ====================================================================
   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.xssf.streaming;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
 * Excel compatible Zip64 implementation.
 * For more information see https://github.com/rzymek/opczip
 */
class Zip64Impl {
    private static final long PK0102 = 0x02014b50L;
    private static final long PK0304 = 0x04034b50L;
    private static final long PK0506 = 0x06054b50L;
    private static final long PK0708 = 0x08074b50L;

    private static final int VERSION_20 = 20;
    private static final int VERSION_45 = 45;
    private static final int DATA_DESCRIPTOR_USED = 0x08;
    private static final int ZIP64_FIELD = 0x0001;
    private static final long MAX32 = 0xffffffffL;

    private final OutputStream out;
    private int written = 0;

    static class Entry {
        final String filename;
        long crc;
        long size;
        long compressedSize;
        long offset;

        Entry(String filename) {
            this.filename = filename;
        }
    }

    Zip64Impl(OutputStream out) {
        this.out = out;
    }

    /**
     * Write Local File Header
     */
    int writeLFH(Entry entry) throws IOException {
        written = 0;
        writeInt(PK0304);                        // "PK\003\004"
        writeShort(VERSION_45);                  // version required: 4.5
        writeShort(DATA_DESCRIPTOR_USED);        // flags: 8 = data descriptor used
        writeShort(ZipEntry.DEFLATED);           // compression method: 8 = deflate
        writeInt(0);                          // file modification time & date
        writeInt(entry.crc);                     // CRC-32
        writeInt(0);                          // compressed file size
        writeInt(0);                          // uncompressed file size
        writeShort(entry.filename.length());     // filename length
        writeShort(0);                        // extra flags size
        byte[] filenameBytes = entry.filename.getBytes(US_ASCII);
        out.write(filenameBytes);                // filename characters
        return written + filenameBytes.length;
    }

    /**
     * Write Data Descriptor
     */
    int writeDAT(Entry entry) throws IOException {
        written = 0;
        writeInt(PK0708);                        // data descriptor signature "PK\007\008"
        writeInt(entry.crc);                     // crc-32
        writeLong(entry.compressedSize);         // compressed size (zip64)
        writeLong(entry.size);                   // uncompressed size (zip64)
        return written;
    }

    /**
     * Write Central directory file header
     */
    int writeCEN(Entry entry) throws IOException {
        written = 0;
        boolean useZip64 = entry.size > MAX32;
        writeInt(PK0102);                              // "PK\001\002"
        writeShort(VERSION_45);                        // version made by: 4.5
        writeShort(useZip64 ? VERSION_45 : VERSION_20);// version required: 4.5
        writeShort(DATA_DESCRIPTOR_USED);              // flags: 8 = data descriptor used
        writeShort(ZipEntry.DEFLATED);                 // compression method: 8 = deflate
        writeInt(0);                                // file modification time & date
        writeInt(entry.crc);                           // CRC-32
        writeInt(entry.compressedSize);                // compressed size
        writeInt(useZip64 ? MAX32 : entry.size); // uncompressed size
        writeShort(entry.filename.length());           // filename length
        writeShort(useZip64
                ? (2 + 2 + 8)  /* short + short + long*/
                : 0);                                  // extra field len
        writeShort(0);                              // comment length
        writeShort(0);                              // disk number where file starts
        writeShort(0);                              // internal file attributes (unused)
        writeInt(0);                                // external file attributes (unused)
        writeInt(entry.offset);                        // LFH offset
        byte[] filenameBytes = entry.filename.getBytes(US_ASCII);
        out.write(filenameBytes);                      // filename characters
        if (useZip64) {
            // Extra field:
            writeShort(ZIP64_FIELD);                   // ZIP64 field signature
            writeShort(8);                          // size of extra field (below)
            writeLong(entry.size);                     // uncompressed size
        }
        return written + filenameBytes.length;
    }

    /**
     * Write End of central directory record (EOCD)
     */
    int writeEND(int entriesCount, long offset, long length) throws IOException {
        written = 0;
        writeInt(PK0506);         // "PK\005\006"
        writeShort(0);         // number of this disk
        writeShort(0);         // central directory start disk
        writeShort(entriesCount); // number of directory entries on disk
        writeShort(entriesCount); // total number of directory entries
        writeInt(length);         // length of central directory
        writeInt(offset);         // offset of central directory
        writeShort(0);         // comment length
        return written;
    }

    /**
     * Writes a 16-bit short to the output stream in little-endian byte order.
     */
    private void writeShort(int v) throws IOException {
        out.write(new byte[]{
                        (byte) ((v >>> 0) & 0xff),
                        (byte) ((v >>> 8) & 0xff)
        });
        written += 2;
    }

    /**
     * Writes a 32-bit int to the output stream in little-endian byte order.
     */
    private void writeInt(long v) throws IOException {
        out.write(new byte[]{
                (byte) ((v >>> 0) & 0xff),
                (byte) ((v >>> 8) & 0xff),
                (byte) ((v >>> 16) & 0xff),
                (byte) ((v >>> 24) & 0xff)
        });
        written += 4;
    }

    /**
     * Writes a 64-bit int to the output stream in little-endian byte order.
     */
    private void writeLong(long v) throws IOException {
        out.write(new byte[]{
                (byte) ((v >>> 0) & 0xff),
                (byte) ((v >>> 8) & 0xff),
                (byte) ((v >>> 16) & 0xff),
                (byte) ((v >>> 24) & 0xff),
                (byte) ((v >>> 32) & 0xff),
                (byte) ((v >>> 40) & 0xff),
                (byte) ((v >>> 48) & 0xff),
                (byte) ((v >>> 56) & 0xff)
        });
        written += 8;
    }

}