jdk/src/java.base/share/classes/java/util/zip/ZipOutputStream.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 29226 b675016fabfd
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    30
import java.nio.charset.Charset;
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    31
import java.nio.charset.StandardCharsets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
    34
import static java.util.zip.ZipConstants64.*;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
    35
import static java.util.zip.ZipUtils.*;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29226
diff changeset
    36
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class implements an output stream filter for writing files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * ZIP file format. Includes support for both compressed and uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
16713
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    48
    /**
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    49
     * Whether to use ZIP64 for zip files with more than 64k entries.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    50
     * Until ZIP64 support in zip implementations is ubiquitous, this
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    51
     * system property allows the creation of zip files which can be
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    52
     * read by legacy zip implementations which tolerate "incorrect"
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    53
     * total entry count fields, such as the ones in jdk6, and even
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    54
     * some in jdk7.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    55
     */
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    56
    private static final boolean inhibitZip64 =
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    57
        Boolean.parseBoolean(
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29226
diff changeset
    58
            GetPropertyAction.getProperty("jdk.util.zip.inhibitZip64"));
16713
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static class XEntry {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    61
        final ZipEntry entry;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    62
        final long offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public XEntry(ZipEntry entry, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private XEntry current;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    70
    private Vector<XEntry> xentries = new Vector<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    71
    private HashSet<String> names = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private long written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private long locoff = 0;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    75
    private byte[] comment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int method = DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    81
    private final ZipCoder zc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    82
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static int version(ZipEntry e) throws ZipException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        case DEFLATED: return 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        case STORED:   return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        default: throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Checks to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Compression method for uncompressed (STORED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Compression method for compressed (DEFLATED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Creates a new ZIP output stream.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   111
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   112
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   113
     * to encode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   114
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param out the actual output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public ZipOutputStream(OutputStream out) {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
   118
        this(out, StandardCharsets.UTF_8);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   119
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   120
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   121
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   122
     * Creates a new ZIP output stream.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   123
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   124
     * @param out the actual output stream
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   125
     *
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   126
     * @param charset the {@linkplain java.nio.charset.Charset charset}
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   127
     *                to be used to encode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   128
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   129
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   130
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   131
    public ZipOutputStream(OutputStream out, Charset charset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   133
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   134
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   135
        this.zc = ZipCoder.get(charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Sets the ZIP file comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param comment the comment string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *            ZIP file comment is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void setComment(String comment) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   146
        if (comment != null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   147
            this.comment = zc.getBytes(comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   148
            if (this.comment.length > 0xffff)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   149
                throw new IllegalArgumentException("ZIP file comment too long.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Sets the default compression method for subsequent entries. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * default will be used whenever the compression method is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * for an individual ZIP file entry, and is initially set to DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param method the default compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @exception IllegalArgumentException if the specified compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *            is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (method != DEFLATED && method != STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Sets the compression level for subsequent entries which are DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The default setting is DEFAULT_COMPRESSION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        def.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Begins writing a new ZIP file entry and positions the stream to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * start of the entry data. Closes the current entry if still active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The default compression method will be used if no compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * was specified for the entry, and the current time will be used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the entry has no set modification time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param e the ZIP entry to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void putNextEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            closeEntry();       // close previous entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   193
        if (e.xdostime == -1) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   194
            // by default, do NOT use extended timestamps in extra
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   195
            // data, for now.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (e.method == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            e.method = method;  // use default method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   201
        // store size, compressed size, and crc-32 in LOC header
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   202
        e.flag = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        case DEFLATED:
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   205
            // store size, compressed size, and crc-32 in data descriptor
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   206
            // immediately following the compressed entry data
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   207
            if (e.size  == -1 || e.csize == -1 || e.crc   == -1)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   208
                e.flag = 8;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // compressed size, uncompressed size, and crc-32 must all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // set for entries using STORED compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (e.size == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                e.size = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } else if (e.csize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                e.csize = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else if (e.size != e.csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    "STORED entry where compressed != uncompressed size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (e.size == -1 || e.crc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    "STORED entry missing size, compressed size, or crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (! names.add(e.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new ZipException("duplicate entry: " + e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   233
        if (zc.isUTF8())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   234
            e.flag |= EFS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        current = new XEntry(e, written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        xentries.add(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        writeLOC(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Closes the current ZIP entry and positions the stream for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * the next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            ZipEntry e = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   256
                if ((e.flag & 8) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    // verify size, compressed size, and crc-32 settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (e.size != def.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            " but got " + def.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    if (e.csize != def.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                            "invalid entry compressed size (expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            e.csize + " but got " + def.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                            "invalid entry CRC-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    e.size  = def.getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    e.csize = def.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    e.crc = crc.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    writeEXT(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                def.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                written += e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                // we already know that both e.size and e.csize are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (e.size != written - locoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        " but got " + (written - locoff) + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                         "invalid entry crc-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                         Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                         Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Writes an array of bytes to the current ZIP entry data. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public synchronized void write(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new ZipException("no current ZIP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        ZipEntry entry = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            super.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (written - locoff > entry.size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    "attempt to write past end of STORED entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Finishes writing the contents of the ZIP output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception IOException if an I/O exception has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // write central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        long off = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        for (XEntry xentry : xentries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            writeCEN(xentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        writeEND(off, written - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Closes the ZIP output stream as well as the stream being filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Writes local file (LOC) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    private void writeLOC(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        ZipEntry e = xentry.entry;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   386
        int flag = e.flag;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   387
        boolean hasZip64 = false;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   388
        int elen = getExtraLen(e.extra);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   389
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   390
        writeInt(LOCSIG);               // LOC header signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if ((flag & 8) == 8) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   392
            writeShort(version(e));     // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   393
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   394
            writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   395
            writeInt(e.xdostime);       // last modification time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // store size, uncompressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // immediately following compressed entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } else {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   402
            if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   403
                hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   404
                writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   405
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   406
                writeShort(version(e)); // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   407
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   408
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   409
            writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   410
            writeInt(e.xdostime);       // last modification time
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   411
            writeInt(e.crc);            // crc-32
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   412
            if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   413
                writeInt(ZIP64_MAGICVAL);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   414
                writeInt(ZIP64_MAGICVAL);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   415
                elen += 20;        //headid(2) + size(2) + size(8) + csize(8)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   416
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   417
                writeInt(e.csize);  // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   418
                writeInt(e.size);   // uncompressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   419
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   421
        byte[] nameBytes = zc.getBytes(e.name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        writeShort(nameBytes.length);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   423
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   424
        int elenEXTT = 0;               // info-zip extended timestamp
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   425
        int flagEXTT = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   426
        if (e.mtime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   427
            elenEXTT += 4;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   428
            flagEXTT |= EXTT_FLAG_LMT;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   429
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   430
        if (e.atime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   431
            elenEXTT += 4;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   432
            flagEXTT |= EXTT_FLAG_LAT;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   433
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   434
        if (e.ctime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   435
            elenEXTT += 4;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   436
            flagEXTT |= EXTT_FLAT_CT;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   437
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   438
        if (flagEXTT != 0)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   439
            elen += (elenEXTT + 5);    // headid(2) + size(2) + flag(1) + data
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   440
        writeShort(elen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        writeBytes(nameBytes, 0, nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   442
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   443
            writeShort(ZIP64_EXTID);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   444
            writeShort(16);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   445
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   446
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   447
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   448
        if (flagEXTT != 0) {
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   449
            writeShort(EXTID_EXTT);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   450
            writeShort(elenEXTT + 1);      // flag + data
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   451
            writeByte(flagEXTT);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   452
            if (e.mtime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   453
                writeInt(fileTimeToUnixTime(e.mtime));
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   454
            if (e.atime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   455
                writeInt(fileTimeToUnixTime(e.atime));
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   456
            if (e.ctime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   457
                writeInt(fileTimeToUnixTime(e.ctime));
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   458
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   459
        writeExtra(e.extra);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        locoff = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Writes extra data descriptor (EXT) for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void writeEXT(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        writeInt(EXTSIG);           // EXT header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   469
        if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   470
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   471
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   472
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   473
            writeInt(e.csize);          // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   474
            writeInt(e.size);           // uncompressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   475
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Write central directory (CEN) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * REMIND: add support for file attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private void writeCEN(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        ZipEntry e  = xentry.entry;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   484
        int flag = e.flag;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int version = version(e);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   486
        long csize = e.csize;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   487
        long size = e.size;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   488
        long offset = xentry.offset;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   489
        int elenZIP64 = 0;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   490
        boolean hasZip64 = false;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   491
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   492
        if (e.csize >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   493
            csize = ZIP64_MAGICVAL;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   494
            elenZIP64 += 8;              // csize(8)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   495
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   496
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   497
        if (e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   498
            size = ZIP64_MAGICVAL;    // size(8)
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   499
            elenZIP64 += 8;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   500
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   501
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   502
        if (xentry.offset >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   503
            offset = ZIP64_MAGICVAL;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   504
            elenZIP64 += 8;              // offset(8)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   505
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   506
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        writeInt(CENSIG);           // CEN header signature
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   508
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   509
            writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   510
            writeShort(45);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   511
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   512
            writeShort(version);    // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   513
            writeShort(version);    // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   514
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        writeShort(flag);           // general purpose bit flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   517
        writeInt(e.xdostime);       // last modification time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   519
        writeInt(csize);            // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   520
        writeInt(size);             // uncompressed size
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   521
        byte[] nameBytes = zc.getBytes(e.name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        writeShort(nameBytes.length);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   523
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   524
        int elen = getExtraLen(e.extra);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   525
        if (hasZip64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   526
            elen += (elenZIP64 + 4);// + headid(2) + datasize(2)
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   527
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   528
        // cen info-zip extended timestamp only outputs mtime
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   529
        // but set the flag for a/ctime, if present in loc
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   530
        int flagEXTT = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   531
        if (e.mtime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   532
            elen += 4;              // + mtime(4)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   533
            flagEXTT |= EXTT_FLAG_LMT;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   534
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   535
        if (e.atime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   536
            flagEXTT |= EXTT_FLAG_LAT;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   537
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   538
        if (e.ctime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   539
            flagEXTT |= EXTT_FLAT_CT;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   540
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   541
        if (flagEXTT != 0) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   542
            elen += 5;             // headid + sz + flag
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   543
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   544
        writeShort(elen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        byte[] commentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (e.comment != null) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   547
            commentBytes = zc.getBytes(e.comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   548
            writeShort(Math.min(commentBytes.length, 0xffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            commentBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        writeShort(0);              // starting disk number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        writeShort(0);              // internal file attributes (unused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        writeInt(0);                // external file attributes (unused)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   556
        writeInt(offset);           // relative offset of local header
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        writeBytes(nameBytes, 0, nameBytes.length);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   558
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   559
        // take care of EXTID_ZIP64 and EXTID_EXTT
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   560
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   561
            writeShort(ZIP64_EXTID);// Zip64 extra
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   562
            writeShort(elenZIP64);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   563
            if (size == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   564
                writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   565
            if (csize == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   566
                writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   567
            if (offset == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   568
                writeLong(xentry.offset);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   569
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   570
        if (flagEXTT != 0) {
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   571
            writeShort(EXTID_EXTT);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   572
            if (e.mtime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   573
                writeShort(5);      // flag + mtime
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   574
                writeByte(flagEXTT);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   575
                writeInt(fileTimeToUnixTime(e.mtime));
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   576
            } else {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   577
                writeShort(1);      // flag only
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   578
                writeByte(flagEXTT);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   579
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   580
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   581
        writeExtra(e.extra);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (commentBytes != null) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   583
            writeBytes(commentBytes, 0, Math.min(commentBytes.length, 0xffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Writes end of central directory (END) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private void writeEND(long off, long len) throws IOException {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   591
        boolean hasZip64 = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   592
        long xlen = len;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   593
        long xoff = off;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   594
        if (xlen >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   595
            xlen = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   596
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   597
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   598
        if (xoff >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   599
            xoff = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   600
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   601
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        int count = xentries.size();
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   603
        if (count >= ZIP64_MAGICCOUNT) {
16713
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
   604
            hasZip64 |= !inhibitZip64;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
   605
            if (hasZip64) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
   606
                count = ZIP64_MAGICCOUNT;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents: 14342
diff changeset
   607
            }
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   608
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   609
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   610
            long off64 = written;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   611
            //zip64 end of central directory record
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   612
            writeInt(ZIP64_ENDSIG);        // zip64 END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   613
            writeLong(ZIP64_ENDHDR - 12);  // size of zip64 end
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   614
            writeShort(45);                // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   615
            writeShort(45);                // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   616
            writeInt(0);                   // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   617
            writeInt(0);                   // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   618
            writeLong(xentries.size());    // number of directory entires on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   619
            writeLong(xentries.size());    // number of directory entires
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   620
            writeLong(len);                // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   621
            writeLong(off);                // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   622
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   623
            //zip64 end of central directory locator
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   624
            writeInt(ZIP64_LOCSIG);        // zip64 END locator signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   625
            writeInt(0);                   // zip64 END start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   626
            writeLong(off64);              // offset of zip64 END
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   627
            writeInt(1);                   // total number of disks (?)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   628
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   629
        writeInt(ENDSIG);                 // END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   630
        writeShort(0);                    // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   631
        writeShort(0);                    // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   632
        writeShort(count);                // number of directory entries on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   633
        writeShort(count);                // total number of directory entries
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   634
        writeInt(xlen);                   // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   635
        writeInt(xoff);                   // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   636
        if (comment != null) {            // zip file comment
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   637
            writeShort(comment.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   638
            writeBytes(comment, 0, comment.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /*
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   645
     * Returns the length of extra data without EXTT and ZIP64.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   646
     */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   647
    private int getExtraLen(byte[] extra) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   648
        if (extra == null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   649
            return 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   650
        int skipped = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   651
        int len = extra.length;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   652
        int off = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   653
        while (off + 4 <= len) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   654
            int tag = get16(extra, off);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   655
            int sz = get16(extra, off + 2);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   656
            if (sz < 0 || (off + 4 + sz) > len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   657
                break;
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   658
            }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   659
            if (tag == EXTID_EXTT || tag == EXTID_ZIP64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   660
                skipped += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   661
            }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   662
            off += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   663
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   664
        return len - skipped;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   665
    }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   666
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   667
    /*
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   668
     * Writes extra data without EXTT and ZIP64.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   669
     *
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   670
     * Extra timestamp and ZIP64 data is handled/output separately
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   671
     * in writeLOC and writeCEN.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   672
     */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   673
    private void writeExtra(byte[] extra) throws IOException {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   674
        if (extra != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   675
            int len = extra.length;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   676
            int off = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   677
            while (off + 4 <= len) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   678
                int tag = get16(extra, off);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   679
                int sz = get16(extra, off + 2);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   680
                if (sz < 0 || (off + 4 + sz) > len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   681
                    writeBytes(extra, off, len - off);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   682
                    return;
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   683
                }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   684
                if (tag != EXTID_EXTT && tag != EXTID_ZIP64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   685
                    writeBytes(extra, off, sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   686
                }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   687
                off += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   688
            }
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   689
            if (off < len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   690
                writeBytes(extra, off, len - off);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   691
            }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   692
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   693
    }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   694
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   695
    /*
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   696
     * Writes a 8-bit byte to the output stream.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   697
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   698
    private void writeByte(int v) throws IOException {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   699
        OutputStream out = this.out;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   700
        out.write(v & 0xff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   701
        written += 1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   702
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   703
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   704
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Writes a 16-bit short to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    private void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        out.write((v >>> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        out.write((v >>> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Writes a 32-bit int to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    private void writeInt(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        out.write((int)((v >>>  0) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        out.write((int)((v >>>  8) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        out.write((int)((v >>> 16) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        out.write((int)((v >>> 24) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        written += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   727
     * Writes a 64-bit int to the output stream in little-endian byte order.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   728
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   729
    private void writeLong(long v) throws IOException {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   730
        OutputStream out = this.out;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   731
        out.write((int)((v >>>  0) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   732
        out.write((int)((v >>>  8) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   733
        out.write((int)((v >>> 16) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   734
        out.write((int)((v >>> 24) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   735
        out.write((int)((v >>> 32) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   736
        out.write((int)((v >>> 40) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   737
        out.write((int)((v >>> 48) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   738
        out.write((int)((v >>> 56) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   739
        written += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   740
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   741
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   742
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Writes an array of bytes to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    private void writeBytes(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        super.out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
}