jdk/src/java.base/share/classes/java/util/zip/ZipOutputStream.java
author mli
Wed, 31 May 2017 19:54:16 -0700
changeset 45434 4582657c7260
parent 39772 c2a5d2de5253
permissions -rw-r--r--
8181082: class-level since tag issues in java.base & java.datatransfer module Reviewed-by: alanb, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
     2
 * Copyright (c) 1996, 2016, 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
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 39772
diff changeset
    44
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
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
    49
    /**
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
     * 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
    51
     * 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
    52
     * 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
    53
     * 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
    54
     * 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
    55
     * 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
    56
     */
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
    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
    58
        Boolean.parseBoolean(
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    59
            GetPropertyAction.privilegedGetProperty("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
    60
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static class XEntry {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    62
        final ZipEntry entry;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    63
        final long offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public XEntry(ZipEntry entry, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private XEntry current;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    71
    private Vector<XEntry> xentries = new Vector<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    72
    private HashSet<String> names = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private long written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private long locoff = 0;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    76
    private byte[] comment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private int method = DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    82
    private final ZipCoder zc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    83
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static int version(ZipEntry e) throws ZipException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        case DEFLATED: return 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        case STORED:   return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        default: throw new ZipException("unsupported compression method");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Checks to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Compression method for uncompressed (STORED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Compression method for compressed (DEFLATED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Creates a new ZIP output stream.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   112
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   113
     * <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
   114
     * to encode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   115
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param out the actual output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public ZipOutputStream(OutputStream out) {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
   119
        this(out, StandardCharsets.UTF_8);
2592
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
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   123
     * Creates a new ZIP output stream.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   124
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   125
     * @param out the actual output stream
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   126
     *
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   127
     * @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
   128
     *                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
   129
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   130
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   131
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   132
    public ZipOutputStream(OutputStream out, Charset charset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        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
   134
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   135
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   136
        this.zc = ZipCoder.get(charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Sets the ZIP file comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param comment the comment string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *            ZIP file comment is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void setComment(String comment) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   147
        if (comment != null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   148
            this.comment = zc.getBytes(comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   149
            if (this.comment.length > 0xffff)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   150
                throw new IllegalArgumentException("ZIP file comment too long.");
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Sets the default compression method for subsequent entries. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * default will be used whenever the compression method is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * for an individual ZIP file entry, and is initially set to DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param method the default compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception IllegalArgumentException if the specified compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *            is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (method != DEFLATED && method != STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Sets the compression level for subsequent entries which are DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The default setting is DEFAULT_COMPRESSION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        def.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Begins writing a new ZIP file entry and positions the stream to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * start of the entry data. Closes the current entry if still active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * The default compression method will be used if no compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * was specified for the entry, and the current time will be used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the entry has no set modification time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param e the ZIP entry to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void putNextEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            closeEntry();       // close previous entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   194
        if (e.xdostime == -1) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   195
            // by default, do NOT use extended timestamps in extra
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   196
            // data, for now.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (e.method == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            e.method = method;  // use default method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   202
        // 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
   203
        e.flag = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        case DEFLATED:
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   206
            // 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
   207
            // immediately following the compressed entry data
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   208
            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
   209
                e.flag = 8;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   210
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // compressed size, uncompressed size, and crc-32 must all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // set for entries using STORED compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (e.size == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                e.size = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } else if (e.csize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                e.csize = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } else if (e.size != e.csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    "STORED entry where compressed != uncompressed size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (e.size == -1 || e.crc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    "STORED entry missing size, compressed size, or crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (! names.add(e.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new ZipException("duplicate entry: " + e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   234
        if (zc.isUTF8())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   235
            e.flag |= EFS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        current = new XEntry(e, written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        xentries.add(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        writeLOC(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Closes the current ZIP entry and positions the stream for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * the next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            ZipEntry e = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   257
                if ((e.flag & 8) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    // verify size, compressed size, and crc-32 settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if (e.size != def.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                            " but got " + def.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    if (e.csize != def.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            "invalid entry compressed size (expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            e.csize + " but got " + def.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            "invalid entry CRC-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    e.size  = def.getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    e.csize = def.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    e.crc = crc.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    writeEXT(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                def.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                written += e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                // we already know that both e.size and e.csize are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (e.size != written - locoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        " but got " + (written - locoff) + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                         "invalid entry crc-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                         Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                         Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            current = null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Writes an array of bytes to the current ZIP entry data. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public synchronized void write(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new ZipException("no current ZIP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        ZipEntry entry = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            super.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (written - locoff > entry.size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    "attempt to write past end of STORED entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Finishes writing the contents of the ZIP output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception IOException if an I/O exception has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // write central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        long off = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        for (XEntry xentry : xentries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            writeCEN(xentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        writeEND(off, written - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Closes the ZIP output stream as well as the stream being filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            closed = true;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Writes local file (LOC) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private void writeLOC(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        ZipEntry e = xentry.entry;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   387
        int flag = e.flag;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   388
        boolean hasZip64 = false;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   389
        int elen = getExtraLen(e.extra);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   390
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   391
        writeInt(LOCSIG);               // LOC header signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if ((flag & 8) == 8) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   393
            writeShort(version(e));     // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   394
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   395
            writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   396
            writeInt(e.xdostime);       // last modification time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // store size, uncompressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // immediately following compressed entry data
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
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } else {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   403
            if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   404
                hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   405
                writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   406
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   407
                writeShort(version(e)); // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   408
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   409
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   410
            writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   411
            writeInt(e.xdostime);       // last modification time
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   412
            writeInt(e.crc);            // crc-32
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   413
            if (hasZip64) {
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
                writeInt(ZIP64_MAGICVAL);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   416
                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
   417
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   418
                writeInt(e.csize);  // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   419
                writeInt(e.size);   // uncompressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   420
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   422
        byte[] nameBytes = zc.getBytes(e.name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        writeShort(nameBytes.length);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   424
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   425
        int elenEXTT = 0;         // info-zip extended timestamp
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   426
        int flagEXTT = 0;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   427
        long umtime = -1;
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   428
        long uatime = -1;
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   429
        long uctime = -1;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   430
        if (e.mtime != 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_LMT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   433
            umtime = fileTimeToUnixTime(e.mtime);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   434
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   435
        if (e.atime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   436
            elenEXTT += 4;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   437
            flagEXTT |= EXTT_FLAG_LAT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   438
            uatime = fileTimeToUnixTime(e.atime);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   439
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   440
        if (e.ctime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   441
            elenEXTT += 4;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   442
            flagEXTT |= EXTT_FLAT_CT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   443
            uctime = fileTimeToUnixTime(e.ctime);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   444
        }
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   445
        if (flagEXTT != 0) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   446
            // to use ntfs time if any m/a/ctime is beyond unixtime upper bound
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   447
            if (umtime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   448
                uatime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   449
                uctime > UPPER_UNIXTIME_BOUND) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   450
                elen += 36;                // NTFS time, total 36 bytes
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   451
            } else {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   452
                elen += (elenEXTT + 5);    // headid(2) + size(2) + flag(1) + data
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   453
            }
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   454
        }
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   455
        writeShort(elen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        writeBytes(nameBytes, 0, nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   457
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   458
            writeShort(ZIP64_EXTID);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   459
            writeShort(16);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   460
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   461
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   462
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   463
        if (flagEXTT != 0) {
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   464
            if (umtime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   465
                uatime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   466
                uctime > UPPER_UNIXTIME_BOUND) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   467
                writeShort(EXTID_NTFS);    // id
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   468
                writeShort(32);            // data size
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   469
                writeInt(0);               // reserved
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   470
                writeShort(0x0001);        // NTFS attr tag
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   471
                writeShort(24);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   472
                writeLong(e.mtime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   473
                                          : fileTimeToWinTime(e.mtime));
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   474
                writeLong(e.atime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   475
                                          : fileTimeToWinTime(e.atime));
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   476
                writeLong(e.ctime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   477
                                          : fileTimeToWinTime(e.ctime));
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   478
            } else {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   479
                writeShort(EXTID_EXTT);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   480
                writeShort(elenEXTT + 1);  // flag + data
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   481
                writeByte(flagEXTT);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   482
                if (e.mtime != null)
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   483
                    writeInt(umtime);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   484
                if (e.atime != null)
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   485
                    writeInt(uatime);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   486
                if (e.ctime != null)
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   487
                    writeInt(uctime);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   488
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   489
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   490
        writeExtra(e.extra);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        locoff = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Writes extra data descriptor (EXT) for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private void writeEXT(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        writeInt(EXTSIG);           // EXT header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   500
        if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   501
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   502
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   503
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   504
            writeInt(e.csize);          // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   505
            writeInt(e.size);           // uncompressed size
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Write central directory (CEN) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * REMIND: add support for file attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private void writeCEN(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        ZipEntry e  = xentry.entry;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   515
        int flag = e.flag;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int version = version(e);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   517
        long csize = e.csize;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   518
        long size = e.size;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   519
        long offset = xentry.offset;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   520
        int elenZIP64 = 0;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   521
        boolean hasZip64 = false;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   522
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   523
        if (e.csize >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   524
            csize = ZIP64_MAGICVAL;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   525
            elenZIP64 += 8;              // csize(8)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   526
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   527
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   528
        if (e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   529
            size = ZIP64_MAGICVAL;    // size(8)
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   530
            elenZIP64 += 8;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   531
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   532
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   533
        if (xentry.offset >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   534
            offset = ZIP64_MAGICVAL;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   535
            elenZIP64 += 8;              // offset(8)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   536
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   537
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        writeInt(CENSIG);           // CEN header signature
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   539
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   540
            writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   541
            writeShort(45);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   542
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   543
            writeShort(version);    // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   544
            writeShort(version);    // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   545
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        writeShort(flag);           // general purpose bit flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        writeShort(e.method);       // compression method
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   548
        writeInt(e.xdostime);       // last modification time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   550
        writeInt(csize);            // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   551
        writeInt(size);             // uncompressed size
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   552
        byte[] nameBytes = zc.getBytes(e.name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        writeShort(nameBytes.length);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   554
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   555
        int elen = getExtraLen(e.extra);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   556
        if (hasZip64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   557
            elen += (elenZIP64 + 4);// + headid(2) + datasize(2)
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   558
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   559
        // cen info-zip extended timestamp only outputs mtime
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   560
        // but set the flag for a/ctime, if present in loc
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   561
        int flagEXTT = 0;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   562
        long umtime = -1;
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   563
        long uatime = -1;
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   564
        long uctime = -1;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   565
        if (e.mtime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   566
            flagEXTT |= EXTT_FLAG_LMT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   567
            umtime = fileTimeToUnixTime(e.mtime);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   568
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   569
        if (e.atime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   570
            flagEXTT |= EXTT_FLAG_LAT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   571
            uatime = fileTimeToUnixTime(e.atime);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   572
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   573
        if (e.ctime != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   574
            flagEXTT |= EXTT_FLAT_CT;
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   575
            uctime = fileTimeToUnixTime(e.ctime);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   576
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   577
        if (flagEXTT != 0) {
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   578
            // to use ntfs time if any m/a/ctime is beyond unixtime upper bound
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   579
            if (umtime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   580
                uatime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   581
                uctime > UPPER_UNIXTIME_BOUND) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   582
                elen += 36;         // NTFS time total 36 bytes
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   583
            } else {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   584
                elen += 9;          // headid(2) + sz(2) + flag(1) + mtime (4)
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   585
            }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   586
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   587
        writeShort(elen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        byte[] commentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (e.comment != null) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   590
            commentBytes = zc.getBytes(e.comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   591
            writeShort(Math.min(commentBytes.length, 0xffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            commentBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        writeShort(0);              // starting disk number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        writeShort(0);              // internal file attributes (unused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        writeInt(0);                // external file attributes (unused)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   599
        writeInt(offset);           // relative offset of local header
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        writeBytes(nameBytes, 0, nameBytes.length);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   601
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   602
        // take care of EXTID_ZIP64 and EXTID_EXTT
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   603
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   604
            writeShort(ZIP64_EXTID);// Zip64 extra
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   605
            writeShort(elenZIP64);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   606
            if (size == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   607
                writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   608
            if (csize == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   609
                writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   610
            if (offset == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   611
                writeLong(xentry.offset);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   612
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   613
        if (flagEXTT != 0) {
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   614
            if (umtime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   615
                uatime > UPPER_UNIXTIME_BOUND ||
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   616
                uctime > UPPER_UNIXTIME_BOUND) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   617
                writeShort(EXTID_NTFS);    // id
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   618
                writeShort(32);            // data size
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   619
                writeInt(0);               // reserved
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   620
                writeShort(0x0001);        // NTFS attr tag
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   621
                writeShort(24);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   622
                writeLong(e.mtime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   623
                                          : fileTimeToWinTime(e.mtime));
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   624
                writeLong(e.atime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   625
                                          : fileTimeToWinTime(e.atime));
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   626
                writeLong(e.ctime == null ? WINDOWS_TIME_NOT_AVAILABLE
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   627
                                          : fileTimeToWinTime(e.ctime));
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   628
            } else {
39772
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   629
                writeShort(EXTID_EXTT);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   630
                if (e.mtime != null) {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   631
                    writeShort(5);      // flag + mtime
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   632
                    writeByte(flagEXTT);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   633
                    writeInt(umtime);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   634
                } else {
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   635
                    writeShort(1);      // flag only
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   636
                    writeByte(flagEXTT);
c2a5d2de5253 8161942: java.util.zip.ZipEntry.java not covering UpperLimit range of DOS epoch
sherman
parents: 37781
diff changeset
   637
                }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   638
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   639
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   640
        writeExtra(e.extra);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (commentBytes != null) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   642
            writeBytes(commentBytes, 0, Math.min(commentBytes.length, 0xffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Writes end of central directory (END) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    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
   650
        boolean hasZip64 = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   651
        long xlen = len;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   652
        long xoff = off;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   653
        if (xlen >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   654
            xlen = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   655
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   656
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   657
        if (xoff >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   658
            xoff = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   659
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   660
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        int count = xentries.size();
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   662
        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
   663
            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
   664
            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
   665
                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
   666
            }
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   667
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   668
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   669
            long off64 = written;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   670
            //zip64 end of central directory record
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   671
            writeInt(ZIP64_ENDSIG);        // zip64 END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   672
            writeLong(ZIP64_ENDHDR - 12);  // size of zip64 end
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   673
            writeShort(45);                // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   674
            writeShort(45);                // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   675
            writeInt(0);                   // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   676
            writeInt(0);                   // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   677
            writeLong(xentries.size());    // number of directory entires on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   678
            writeLong(xentries.size());    // number of directory entires
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   679
            writeLong(len);                // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   680
            writeLong(off);                // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   681
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   682
            //zip64 end of central directory locator
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   683
            writeInt(ZIP64_LOCSIG);        // zip64 END locator signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   684
            writeInt(0);                   // zip64 END start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   685
            writeLong(off64);              // offset of zip64 END
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   686
            writeInt(1);                   // total number of disks (?)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   687
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   688
        writeInt(ENDSIG);                 // END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   689
        writeShort(0);                    // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   690
        writeShort(0);                    // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   691
        writeShort(count);                // number of directory entries on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   692
        writeShort(count);                // total number of directory entries
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   693
        writeInt(xlen);                   // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   694
        writeInt(xoff);                   // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   695
        if (comment != null) {            // zip file comment
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   696
            writeShort(comment.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   697
            writeBytes(comment, 0, comment.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /*
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   704
     * Returns the length of extra data without EXTT and ZIP64.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   705
     */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   706
    private int getExtraLen(byte[] extra) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   707
        if (extra == null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   708
            return 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   709
        int skipped = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   710
        int len = extra.length;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   711
        int off = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   712
        while (off + 4 <= len) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   713
            int tag = get16(extra, off);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   714
            int sz = get16(extra, off + 2);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   715
            if (sz < 0 || (off + 4 + sz) > len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   716
                break;
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   717
            }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   718
            if (tag == EXTID_EXTT || tag == EXTID_ZIP64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   719
                skipped += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   720
            }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   721
            off += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   722
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   723
        return len - skipped;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   724
    }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   725
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   726
    /*
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   727
     * Writes extra data without EXTT and ZIP64.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   728
     *
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   729
     * Extra timestamp and ZIP64 data is handled/output separately
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   730
     * in writeLOC and writeCEN.
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   731
     */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   732
    private void writeExtra(byte[] extra) throws IOException {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   733
        if (extra != null) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   734
            int len = extra.length;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   735
            int off = 0;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   736
            while (off + 4 <= len) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   737
                int tag = get16(extra, off);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   738
                int sz = get16(extra, off + 2);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   739
                if (sz < 0 || (off + 4 + sz) > len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   740
                    writeBytes(extra, off, len - off);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   741
                    return;
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   742
                }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   743
                if (tag != EXTID_EXTT && tag != EXTID_ZIP64) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   744
                    writeBytes(extra, off, sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   745
                }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   746
                off += (sz + 4);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   747
            }
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   748
            if (off < len) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   749
                writeBytes(extra, off, len - off);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   750
            }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   751
        }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   752
    }
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   753
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   754
    /*
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   755
     * Writes a 8-bit byte to the output stream.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   756
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   757
    private void writeByte(int v) throws IOException {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   758
        OutputStream out = this.out;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   759
        out.write(v & 0xff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   760
        written += 1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   761
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   762
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16713
diff changeset
   763
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Writes a 16-bit short to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        out.write((v >>> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        out.write((v >>> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * Writes a 32-bit int to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private void writeInt(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        out.write((int)((v >>>  0) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        out.write((int)((v >>>  8) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        out.write((int)((v >>> 16) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        out.write((int)((v >>> 24) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        written += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   786
     * 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
   787
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   788
    private void writeLong(long v) throws IOException {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   789
        OutputStream out = this.out;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   790
        out.write((int)((v >>>  0) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   791
        out.write((int)((v >>>  8) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   792
        out.write((int)((v >>> 16) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   793
        out.write((int)((v >>> 24) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   794
        out.write((int)((v >>> 32) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   795
        out.write((int)((v >>> 40) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   796
        out.write((int)((v >>> 48) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   797
        out.write((int)((v >>> 56) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   798
        written += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   799
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   800
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   801
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Writes an array of bytes to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    private void writeBytes(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        super.out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
}