jdk/src/share/classes/sun/tools/jar/Main.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5627 e636ac7a63a4
child 8158 77d9c0f1c19f
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5627
diff changeset
     2
 * Copyright (c) 1996, 2010, 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: 3288
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: 3288
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: 3288
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
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 sun.tools.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    29
import java.nio.file.Path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.misc.JarIndex;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    36
import static sun.misc.JarIndex.INDEX_NAME;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    37
import static java.util.jar.JarFile.MANIFEST_NAME;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
    38
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class implements a simple utility for creating files in the JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (Java Archive) file format. The JAR format is based on the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * format, with optional meta-information stored in a MANIFEST entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class Main {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    String program;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    PrintStream out, err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    String fname, mname, ename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    String zname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    String[] files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    String rootjar = null;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    53
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    54
    // An entryName(path)->File map generated during "expand", it helps to
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    55
    // decide whether or not an existing entry in a jar file needs to be
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    56
    // replaced, during the "update" operation.
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    57
    Map<String, File> entryMap = new HashMap<String, File>();
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    58
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    59
    // All files need to be added/updated.
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    60
    Set<File> entries = new LinkedHashSet<File>();
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    61
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    62
    // Directories specified by "-C" operation.
3056
0864d307b376 6834805: Improve jar -C performance
martin
parents: 1816
diff changeset
    63
    Set<String> paths = new HashSet<String>();
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * cflag: create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * uflag: update
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * xflag: xtract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * tflag: table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * vflag: verbose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * flag0: no zip compression (store only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Mflag: DO NOT generate a manifest file (just ZIP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * iflag: generate jar index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final String MANIFEST_DIR = "META-INF/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final String VERSION = "1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static ResourceBundle rsrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If true, maintain compatibility with JDK releases prior to 6.0 by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * timestamping extracted files with the time at which they are extracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Default is to use the time given in the archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final boolean useExtractionTime =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Boolean.getBoolean("sun.tools.jar.useExtractionTime");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Initialize ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new Error("Fatal: Resource for jar is missing");
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
    private String getMsg(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return (rsrc.getString(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new Error("Error in message file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private String formatMsg(String key, String arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String msg = getMsg(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String[] args = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        args[0] = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return MessageFormat.format(msg, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private String formatMsg2(String key, String arg, String arg1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String msg = getMsg(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String[] args = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        args[0] = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        args[1] = arg1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return MessageFormat.format(msg, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Main(PrintStream out, PrintStream err, String program) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.err = err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.program = program;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   130
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   131
     * Creates a new empty temporary file in the same directory as the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   132
     * specified file.  A variant of File.createTempFile.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   133
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   134
    private static File createTempFileInSameDirectoryAs(File file)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   135
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   136
        File dir = file.getParentFile();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   137
        if (dir == null)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   138
            dir = new File(".");
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   139
        return File.createTempFile("jartmp", null, dir);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   140
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   144
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Starts main program with the specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public synchronized boolean run(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (!parseArgs(args)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (cflag || uflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    // The name of the zip file as it would appear as its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // zip file entry. We use this to make sure that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // add the zip file to itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    zname = fname.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (zname.startsWith("./")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        zname = zname.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (cflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                Manifest manifest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (!Mflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (mname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        in = new FileInputStream(mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        manifest = new Manifest(new BufferedInputStream(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        manifest = new Manifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    addVersion(manifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    addCreatedBy(manifest);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   177
                    if (isAmbiguousMainClass(manifest)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        addMainClass(manifest, ename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    out = new FileOutputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    out = new FileOutputStream(FileDescriptor.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        // Disable verbose output so that it does not appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        // on stdout along with file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        // error("Warning: -v option ignored");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        vflag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   199
                expand(null, files, false);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   200
                create(new BufferedOutputStream(out, 4096), manifest);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } else if (uflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                File inputFile = null, tmpFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                FileInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                FileOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    inputFile = new File(fname);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   211
                    tmpFile = createTempFileInSameDirectoryAs(inputFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    in = new FileInputStream(inputFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    out = new FileOutputStream(tmpFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    in = new FileInputStream(FileDescriptor.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    out = new FileOutputStream(FileDescriptor.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    vflag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                InputStream manifest = (!Mflag && (mname != null)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    (new FileInputStream(mname)) : null;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   221
                expand(null, files, true);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   222
                boolean updateOk = update(in, new BufferedOutputStream(out),
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   223
                                          manifest, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    ok = updateOk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (manifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    manifest.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (fname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    // on Win32, we need this delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    inputFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (!tmpFile.renameTo(inputFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        tmpFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        throw new IOException(getMsg("error.write.file"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    tmpFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   241
            } else if (tflag) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   242
                replaceFSC(files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (fname != null) {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   244
                    list(fname, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                } else {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   246
                    InputStream in = new FileInputStream(FileDescriptor.in);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   247
                    try{
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   248
                        list(new BufferedInputStream(in), files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   249
                    } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   250
                        in.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   251
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   253
            } else if (xflag) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   254
                replaceFSC(files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   255
                if (fname != null && files != null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   256
                    extract(fname, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                } else {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   258
                    InputStream in = (fname == null)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   259
                        ? new FileInputStream(FileDescriptor.in)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   260
                        : new FileInputStream(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   261
                    try {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   262
                        extract(new BufferedInputStream(in), files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   263
                    } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   264
                        in.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   265
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else if (iflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                genIndex(rootjar, files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            fatalError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } catch (Error ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   285
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   286
     * Parses command line arguments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    boolean parseArgs(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /* Preprocess and expand @file arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            args = CommandLine.parse(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            fatalError(formatMsg("error.cant.open", e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            fatalError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /* parse flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            String flags = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (flags.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                flags = flags.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (int i = 0; i < flags.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                switch (flags.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                case 'c':
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   309
                    if (xflag || tflag || uflag || iflag) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    cflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                case 'u':
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   316
                    if (cflag || xflag || tflag || iflag) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    uflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                case 'x':
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   323
                    if (cflag || uflag || tflag || iflag) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    xflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                case 't':
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   330
                    if (cflag || uflag || xflag || iflag) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    tflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                case 'M':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    Mflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                case 'v':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    vflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    fname = args[count++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    mname = args[count++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    flag0 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                case 'i':
5620
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   352
                    if (cflag || uflag || xflag || tflag) {
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   353
                        usageError();
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   354
                        return false;
ed56433bf5d6 4690407: JAR tool: option -i can't be combined with other options
sherman
parents: 3288
diff changeset
   355
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    // do not increase the counter, files will contain rootjar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    rootjar = args[count++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    iflag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                case 'e':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                     ename = args[count++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                     break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    error(formatMsg("error.illegal.option",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                String.valueOf(flags.charAt(i))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (!cflag && !tflag && !xflag && !uflag && !iflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            error(getMsg("error.bad.option"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        /* parse file arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int n = args.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            String[] nameBuf = new String[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                for (int i = count; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    if (args[i].equals("-C")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        /* change the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        String dir = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        dir = (dir.endsWith(File.separator) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                               dir : (dir + File.separator));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        dir = dir.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        while (dir.indexOf("//") > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            dir = dir.replace("//", "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        }
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   395
                        paths.add(dir.replace(File.separatorChar, '/'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        nameBuf[k++] = dir + args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        nameBuf[k++] = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            files = new String[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            System.arraycopy(nameBuf, 0, files, 0, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } else if (cflag && (mname == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            error(getMsg("error.bad.cflag"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else if (uflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if ((mname != null) || (ename != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                /* just want to update the manifest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                error(getMsg("error.bad.uflag"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   424
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Expands list of files to process into full list of all files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * can be found by recursively descending directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   428
    void expand(File dir, String[] files, boolean isUpdate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            File f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                f = new File(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                f = new File(dir, files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (f.isFile()) {
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   440
                if (entries.add(f)) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   441
                    if (isUpdate)
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   442
                        entryMap.put(entryName(f.getPath()), f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else if (f.isDirectory()) {
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   445
                if (entries.add(f)) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   446
                    if (isUpdate) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   447
                        String dirPath = f.getPath();
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   448
                        dirPath = (dirPath.endsWith(File.separator)) ? dirPath :
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   449
                            (dirPath + File.separator);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   450
                        entryMap.put(entryName(dirPath), f);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   451
                    }
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   452
                    expand(f, f.list(), isUpdate);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   453
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   461
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Creates a new JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   464
    void create(OutputStream out, Manifest manifest)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        ZipOutputStream zos = new JarOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            zos.setMethod(ZipOutputStream.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (manifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                output(getMsg("out.added.manifest"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            ZipEntry e = new ZipEntry(MANIFEST_DIR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            e.setSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            e.setCrc(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            zos.putNextEntry(e);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   480
            e = new ZipEntry(MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                crc32Manifest(e, manifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            manifest.write(zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   489
        for (File file: entries) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   490
            addFile(zos, file);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   495
    private char toUpperCaseASCII(char c) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   496
        return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   497
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   498
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   499
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   500
     * Compares two strings for equality, ignoring case.  The second
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   501
     * argument must contain only upper-case ASCII characters.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   502
     * We don't want case comparison to be locale-dependent (else we
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   503
     * have the notorious "turkish i bug").
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   504
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   505
    private boolean equalsIgnoreCase(String s, String upper) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   506
        assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   507
        int len;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   508
        if ((len = s.length()) != upper.length())
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   509
            return false;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   510
        for (int i = 0; i < len; i++) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   511
            char c1 = s.charAt(i);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   512
            char c2 = upper.charAt(i);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   513
            if (c1 != c2 && toUpperCaseASCII(c1) != c2)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   514
                return false;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   515
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   516
        return true;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   517
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   518
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   519
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   520
     * Updates an existing jar file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    boolean update(InputStream in, OutputStream out,
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   523
                   InputStream newManifest,
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   524
                   JarIndex jarIndex) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        ZipOutputStream zos = new JarOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        ZipEntry e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        boolean foundManifest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        boolean updateOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   532
        if (jarIndex != null) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   533
            addIndex(jarIndex, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // put the old entries first, replace if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        while ((e = zis.getNextEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            String name = e.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   540
            boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   541
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   542
            if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   543
                || (Mflag && isManifestEntry)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else if (isManifestEntry && ((newManifest != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        (ename != null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                foundManifest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    // Don't read from the newManifest InputStream, as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    // might need it below, and we can't re-read the same data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    // twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    FileInputStream fis = new FileInputStream(mname);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   553
                    boolean ambiguous = isAmbiguousMainClass(new Manifest(fis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    fis.close();
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   555
                    if (ambiguous) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // Update the manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                Manifest old = new Manifest(zis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    old.read(newManifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                updateManifest(old, zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            } else {
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   567
                if (!entryMap.containsKey(name)) { // copy the old stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    // do our own compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    ZipEntry e2 = new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    e2.setMethod(e.getMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    e2.setTime(e.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    e2.setComment(e.getComment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    e2.setExtra(e.getExtra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    if (e.getMethod() == ZipEntry.STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        e2.setSize(e.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        e2.setCrc(e.getCrc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    zos.putNextEntry(e2);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   579
                    copy(zis, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                } else { // replace with the new files
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   581
                    File f = entryMap.get(name);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   582
                    addFile(zos, f);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   583
                    entryMap.remove(name);
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   584
                    entries.remove(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // add the remaining new files
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   590
        for (File f: entries) {
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   591
            addFile(zos, f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (!foundManifest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (newManifest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                Manifest m = new Manifest(newManifest);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   596
                updateOk = !isAmbiguousMainClass(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                if (updateOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    updateManifest(m, zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            } else if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                updateManifest(new Manifest(), zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        zis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return updateOk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private void addIndex(JarIndex index, ZipOutputStream zos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   613
        ZipEntry e = new ZipEntry(INDEX_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (flag0) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   616
            CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   617
            index.write(os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   618
            os.updateEntry(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        index.write(zos);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   622
        zos.closeEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private void updateManifest(Manifest m, ZipOutputStream zos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        addVersion(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        addCreatedBy(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            addMainClass(m, ename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   633
        ZipEntry e = new ZipEntry(MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            crc32Manifest(e, m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        m.write(zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            output(getMsg("out.update.manifest"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
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
    private String entryName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        name = name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        String matchPath = "";
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   649
        for (String path : paths) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   650
            if (name.startsWith(path)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   651
                && (path.length() > matchPath.length())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                matchPath = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        name = name.substring(matchPath.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } else if (name.startsWith("./")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            name = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    private void addVersion(Manifest m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            global.put(Attributes.Name.MANIFEST_VERSION, VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private void addCreatedBy(Manifest m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (global.getValue(new Attributes.Name("Created-By")) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            String javaVendor = System.getProperty("java.vendor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            String jdkVersion = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        javaVendor + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    private void addMainClass(Manifest m, String mainApp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        // overrides any existing Main-Class attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        global.put(Attributes.Name.MAIN_CLASS, mainApp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   689
    private boolean isAmbiguousMainClass(Manifest m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (ename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            Attributes global = m.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                error(getMsg("error.bad.eflag"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                usageError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   701
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Adds a new file entry to the ZIP output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    void addFile(ZipOutputStream zos, File file) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        boolean isDir = file.isDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (isDir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            name = name.endsWith(File.separator) ? name :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                (name + File.separator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        name = entryName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (name.equals("") || name.equals(".") || name.equals(zname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   715
        } else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                   && !Mflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                output(formatMsg("out.ignore.entry", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        long size = isDir ? 0 : file.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            out.print(formatMsg("out.adding", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        ZipEntry e = new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        e.setTime(file.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            e.setMethod(ZipEntry.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            e.setSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            e.setCrc(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        } else if (flag0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            crc32File(e, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (!isDir) {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   739
            copy(file, zos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        /* report how much compression occurred. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            size = e.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            long csize = e.getCompressedSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            out.print(formatMsg2("out.size", String.valueOf(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        String.valueOf(csize)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (e.getMethod() == ZipEntry.DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                long ratio = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                if (size != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    ratio = ((size - csize) * 100) / size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                output(formatMsg("out.deflated", String.valueOf(ratio)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                output(getMsg("out.stored"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   760
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   761
     * A buffer for use only by copy(InputStream, OutputStream).
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   762
     * Not as clean as allocating a new buffer as needed by copy,
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   763
     * but significantly more efficient.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   765
    private byte[] copyBuf = new byte[8192];
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   766
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   767
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   768
     * Copies all bytes from the input stream to the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   769
     * Does not close or flush either stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   770
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   771
     * @param from the input stream to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   772
     * @param to the output stream to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   773
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   774
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   775
    private void copy(InputStream from, OutputStream to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   776
        int n;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   777
        while ((n = from.read(copyBuf)) != -1)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   778
            to.write(copyBuf, 0, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   781
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   782
     * Copies all bytes from the input file to the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   783
     * Does not close or flush the output stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   784
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   785
     * @param from the input file to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   786
     * @param to the output stream to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   787
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   788
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   789
    private void copy(File from, OutputStream to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   790
        InputStream in = new FileInputStream(from);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   791
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   792
            copy(in, to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   793
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   794
            in.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   795
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   796
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   797
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   798
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   799
     * Copies all bytes from the input stream to the output file.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   800
     * Does not close the input stream.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   801
     *
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   802
     * @param from the input stream to read from
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   803
     * @param to the output file to write to
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   804
     * @throws IOException if an I/O error occurs
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   805
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   806
    private void copy(InputStream from, File to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   807
        OutputStream out = new FileOutputStream(to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   808
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   809
            copy(from, out);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   810
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   811
            out.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   812
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   813
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   814
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   815
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   816
     * Computes the crc32 of a Manifest.  This is necessary when the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   817
     * ZipOutputStream is in STORED mode.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   818
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   819
    private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   820
        CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   821
        m.write(os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   822
        os.updateEntry(e);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   823
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   824
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   825
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   826
     * Computes the crc32 of a File.  This is necessary when the
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   827
     * ZipOutputStream is in STORED mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private void crc32File(ZipEntry e, File f) throws IOException {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   830
        CRC32OutputStream os = new CRC32OutputStream();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   831
        copy(f, os);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   832
        if (os.n != f.length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            throw new JarException(formatMsg(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        "error.incorrect.length", f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   836
        os.updateEntry(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   839
    void replaceFSC(String files[]) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   840
        if (files != null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   841
            for (String file : files) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   842
                file = file.replace(File.separatorChar, '/');
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   843
            }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   844
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   845
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   846
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   847
    @SuppressWarnings("serial")
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   848
    Set<ZipEntry> newDirSet() {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   849
        return new HashSet<ZipEntry>() {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   850
            public boolean add(ZipEntry e) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   851
                return ((e == null || useExtractionTime) ? false : super.add(e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   852
            }};
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   853
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   854
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   855
    void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   856
        for (ZipEntry ze : zes) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   857
            long lastModified = ze.getTime();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   858
            if (lastModified != -1) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   859
                File f = new File(ze.getName().replace('/', File.separatorChar));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   860
                f.setLastModified(lastModified);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   861
            }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   862
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   863
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   864
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   865
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Extracts specified entries from JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    void extract(InputStream in, String files[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        ZipEntry e;
1762
4659af392d07 6496274: jar seems to use more CPU than it should
sherman
parents: 2
diff changeset
   871
        // Set of all directory entries specified in archive.  Disallows
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        // null entries.  Disallows all entries if using pre-6.0 behavior.
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   873
        Set<ZipEntry> dirs = newDirSet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        while ((e = zis.getNextEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                dirs.add(extractFile(zis, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                String name = e.getName();
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   879
                for (String file : files) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    if (name.startsWith(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                        dirs.add(extractFile(zis, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // Update timestamps of directories specified in archive with their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // timestamps as given in the archive.  We do this after extraction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        // instead of during, because creating a file in a directory changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        // that directory's timestamp.
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   892
        updateLastModifiedTime(dirs);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   893
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   894
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   895
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   896
     * Extracts specified entries from JAR file, via ZipFile.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   897
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   898
    void extract(String fname, String files[]) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   899
        ZipFile zf = new ZipFile(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   900
        Set<ZipEntry> dirs = newDirSet();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   901
        Enumeration<? extends ZipEntry> zes = zf.entries();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   902
        while (zes.hasMoreElements()) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   903
            ZipEntry e = zes.nextElement();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   904
            InputStream is;
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   905
            if (files == null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   906
                dirs.add(extractFile(zf.getInputStream(e), e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   907
            } else {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   908
                String name = e.getName();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   909
                for (String file : files) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   910
                    if (name.startsWith(file)) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   911
                        dirs.add(extractFile(zf.getInputStream(e), e));
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   912
                        break;
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   913
                    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   914
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   917
        zf.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   918
        updateLastModifiedTime(dirs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   921
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Extracts next entry from JAR file, creating directories as needed.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * the entry is for a directory which doesn't exist prior to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * invocation, returns that entry, otherwise returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   926
    ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        ZipEntry rc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        String name = e.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        File f = new File(e.getName().replace('/', File.separatorChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (e.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (!f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    throw new IOException(formatMsg("error.create.dir",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                if (!f.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    throw new IOException(formatMsg("error.create.dir",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                        f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    rc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                output(formatMsg("out.create", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (f.getParent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                File d = new File(f.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    throw new IOException(formatMsg(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        "error.create.dir", d.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            }
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   956
            try {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   957
                copy(is, f);
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   958
            } finally {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   959
                if (is instanceof ZipInputStream)
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   960
                    ((ZipInputStream)is).closeEntry();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   961
                else
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   962
                    is.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (vflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                if (e.getMethod() == ZipEntry.DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    output(formatMsg("out.inflated", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    output(formatMsg("out.extracted", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if (!useExtractionTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            long lastModified = e.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if (lastModified != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                f.setLastModified(lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   981
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * Lists contents of JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    void list(InputStream in, String files[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        ZipInputStream zis = new ZipInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        ZipEntry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        while ((e = zis.getNextEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
             * In the case of a compressed (deflated) entry, the entry size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
             * is stored immediately following the entry data and cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
             * determined until the entry is fully read. Therefore, we close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
             * the entry first before printing out its attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            zis.closeEntry();
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
   995
            printEntry(e, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
   999
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1000
     * Lists contents of JAR file, via ZipFile.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1001
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1002
    void list(String fname, String files[]) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1003
        ZipFile zf = new ZipFile(fname);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1004
        Enumeration<? extends ZipEntry> zes = zf.entries();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1005
        while (zes.hasMoreElements()) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1006
            printEntry(zes.nextElement(), files);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1007
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1008
        zf.close();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1009
    }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1010
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /**
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1012
     * Outputs the class index table to the INDEX.LIST file of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * root jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    void dumpIndex(String rootjar, JarIndex index) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        File jarFile = new File(rootjar);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1017
        Path jarPath = jarFile.toPath();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1018
        Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1019
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1020
            if (update(jarPath.newInputStream(),
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1021
                       tmpPath.newOutputStream(),
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1022
                       null, index)) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1023
                try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1024
                    tmpPath.moveTo(jarPath, REPLACE_EXISTING);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1025
                } catch (IOException e) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1026
                    throw new IOException(getMsg("error.write.file"), e);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1027
                }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1028
            }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1029
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1030
            tmpPath.deleteIfExists();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1034
    private HashSet<String> jarPaths = new HashSet<String>();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1035
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1036
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1037
     * Generates the transitive closure of the Class-Path attribute for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * the specified jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1040
    List<String> getJarPath(String jar) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1041
        List<String> files = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        files.add(jar);
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1043
        jarPaths.add(jar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // take out the current path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        // class path attribute will give us jar file name with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // '/' as separators, so we need to change them to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        // appropriate one before we open the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        JarFile rf = new JarFile(jar.replace('/', File.separatorChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (rf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            Manifest man = rf.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    String value = attr.getValue(Attributes.Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                        StringTokenizer st = new StringTokenizer(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                            String ajar = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                            if (!ajar.endsWith("/")) {  // it is a jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                                ajar = path.concat(ajar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                /* check on cyclic dependency */
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1066
                                if (! jarPaths.contains(ajar)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                                    files.addAll(getJarPath(ajar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        rf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1080
     * Generates class index file for the specified root jar file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    void genIndex(String rootjar, String[] files) throws IOException {
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1083
        List<String> jars = getJarPath(rootjar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        int njars = jars.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        String[] jarfiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (njars == 1 && files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // no class-path attribute defined in rootjar, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // use command line specified list of jars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                jars.addAll(getJarPath(files[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            njars = jars.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1095
        jarfiles = jars.toArray(new String[njars]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        JarIndex index = new JarIndex(jarfiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        dumpIndex(rootjar, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1100
    /**
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1101
     * Prints entry information, if requested.
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1102
     */
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1103
    void printEntry(ZipEntry e, String[] files) throws IOException {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1104
        if (files == null) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1105
            printEntry(e);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1106
        } else {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1107
            String name = e.getName();
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1108
            for (String file : files) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1109
                if (name.startsWith(file)) {
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1110
                    printEntry(e);
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1111
                    return;
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1112
                }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1113
            }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1114
        }
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1115
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1117
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Prints entry information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    void printEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (vflag) {
1816
f310e059bd83 6332094: "jar t" and "jar x" should use ZipFile, not ZipInputStream
sherman
parents: 1762
diff changeset
  1122
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            String s = Long.toString(e.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            for (int i = 6 - s.length(); i > 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            sb.append(s).append(' ').append(new Date(e.getTime()).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            sb.append(' ').append(e.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            output(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            output(e.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1135
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1136
     * Prints usage message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    void usageError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        error(getMsg("usage"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1142
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * A fatal exception has been caught.  No recovery possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    void fatalError(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1149
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * A fatal condition has been detected; message is "s".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * No recovery possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    void fatalError(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        error(program + ": " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Print an output message; like verbose output and the like
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    protected void output(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        out.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * Print an error mesage; like something is broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    protected void error(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1171
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * Main routine to start program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        Main jartool = new Main(System.out, System.err, "jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        System.exit(jartool.run(args) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1179
    /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1180
     * An OutputStream that doesn't send its output anywhere, (but could).
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1181
     * It's here to find the CRC32 of an input file, necessary for STORED
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1182
     * mode in ZIP.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1183
     */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1184
    private static class CRC32OutputStream extends java.io.OutputStream {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1185
        final CRC32 crc = new CRC32();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1186
        long n = 0;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1187
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1188
        CRC32OutputStream() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1190
        public void write(int r) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1191
            crc.update(r);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1192
            n++;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1193
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1194
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1195
        public void write(byte[] b, int off, int len) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1196
            crc.update(b, off, len);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1197
            n += len;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1198
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1200
        /**
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1201
         * Updates a ZipEntry which describes the data read by this
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1202
         * output stream, in STORED mode.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1203
         */
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1204
        public void updateEntry(ZipEntry e) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1205
            e.setMethod(ZipEntry.STORED);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1206
            e.setSize(n);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1207
            e.setCrc(crc.getValue());
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 3056
diff changeset
  1208
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
}