Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jar/Main.java
38918 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jar;2627import java.io.*;28import java.nio.file.Path;29import java.nio.file.Files;30import java.util.*;31import java.util.zip.*;32import java.util.jar.*;33import java.util.jar.Pack200.*;34import java.util.jar.Manifest;35import java.text.MessageFormat;36import sun.misc.JarIndex;37import static sun.misc.JarIndex.INDEX_NAME;38import static java.util.jar.JarFile.MANIFEST_NAME;39import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;4041/**42* This class implements a simple utility for creating files in the JAR43* (Java Archive) file format. The JAR format is based on the ZIP file44* format, with optional meta-information stored in a MANIFEST entry.45*/46public47class Main {48String program;49PrintStream out, err;50String fname, mname, ename;51String zname = "";52String[] files;53String rootjar = null;5455// An entryName(path)->File map generated during "expand", it helps to56// decide whether or not an existing entry in a jar file needs to be57// replaced, during the "update" operation.58Map<String, File> entryMap = new HashMap<String, File>();5960// All files need to be added/updated.61Set<File> entries = new LinkedHashSet<File>();6263// Directories specified by "-C" operation.64Set<String> paths = new HashSet<String>();6566/*67* cflag: create68* uflag: update69* xflag: xtract70* tflag: table71* vflag: verbose72* flag0: no zip compression (store only)73* Mflag: DO NOT generate a manifest file (just ZIP)74* iflag: generate jar index75* nflag: Perform jar normalization at the end76* pflag: preserve/don't strip leading slash and .. component from file name77*78*/79boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, nflag, pflag;8081static final String MANIFEST_DIR = "META-INF/";82static final String VERSION = "1.0";8384private static ResourceBundle rsrc;8586/**87* If true, maintain compatibility with JDK releases prior to 6.0 by88* timestamping extracted files with the time at which they are extracted.89* Default is to use the time given in the archive.90*/91private static final boolean useExtractionTime =92Boolean.getBoolean("sun.tools.jar.useExtractionTime");9394/**95* Initialize ResourceBundle96*/97static {98try {99rsrc = ResourceBundle.getBundle("sun.tools.jar.resources.jar");100} catch (MissingResourceException e) {101throw new Error("Fatal: Resource for jar is missing");102}103}104105private String getMsg(String key) {106try {107return (rsrc.getString(key));108} catch (MissingResourceException e) {109throw new Error("Error in message file");110}111}112113private String formatMsg(String key, String arg) {114String msg = getMsg(key);115String[] args = new String[1];116args[0] = arg;117return MessageFormat.format(msg, (Object[]) args);118}119120private String formatMsg2(String key, String arg, String arg1) {121String msg = getMsg(key);122String[] args = new String[2];123args[0] = arg;124args[1] = arg1;125return MessageFormat.format(msg, (Object[]) args);126}127128public Main(PrintStream out, PrintStream err, String program) {129this.out = out;130this.err = err;131this.program = program;132}133134/**135* Creates a new empty temporary file in the same directory as the136* specified file. A variant of File.createTempFile.137*/138private static File createTempFileInSameDirectoryAs(File file)139throws IOException {140File dir = file.getParentFile();141if (dir == null)142dir = new File(".");143return File.createTempFile("jartmp", null, dir);144}145146private boolean ok;147148/**149* Starts main program with the specified arguments.150*/151public synchronized boolean run(String args[]) {152ok = true;153if (!parseArgs(args)) {154return false;155}156try {157if (cflag || uflag) {158if (fname != null) {159// The name of the zip file as it would appear as its own160// zip file entry. We use this to make sure that we don't161// add the zip file to itself.162zname = fname.replace(File.separatorChar, '/');163if (zname.startsWith("./")) {164zname = zname.substring(2);165}166}167}168if (cflag) {169Manifest manifest = null;170InputStream in = null;171172if (!Mflag) {173if (mname != null) {174in = new FileInputStream(mname);175manifest = new Manifest(new BufferedInputStream(in));176} else {177manifest = new Manifest();178}179addVersion(manifest);180addCreatedBy(manifest);181if (isAmbiguousMainClass(manifest)) {182if (in != null) {183in.close();184}185return false;186}187if (ename != null) {188addMainClass(manifest, ename);189}190}191expand(null, files, false);192OutputStream out;193if (fname != null) {194out = new FileOutputStream(fname);195} else {196out = new FileOutputStream(FileDescriptor.out);197if (vflag) {198// Disable verbose output so that it does not appear199// on stdout along with file data200// error("Warning: -v option ignored");201vflag = false;202}203}204File tmpfile = null;205final OutputStream finalout = out;206final String tmpbase = (fname == null)207? "tmpjar"208: fname.substring(fname.indexOf(File.separatorChar) + 1);209if (nflag) {210tmpfile = createTemporaryFile(tmpbase, ".jar");211out = new FileOutputStream(tmpfile);212}213create(new BufferedOutputStream(out, 4096), manifest);214if (in != null) {215in.close();216}217out.close();218if (nflag) {219JarFile jarFile = null;220File packFile = null;221JarOutputStream jos = null;222try {223Packer packer = Pack200.newPacker();224Map<String, String> p = packer.properties();225p.put(Packer.EFFORT, "1"); // Minimal effort to conserve CPU226jarFile = new JarFile(tmpfile.getCanonicalPath());227packFile = createTemporaryFile(tmpbase, ".pack");228out = new FileOutputStream(packFile);229packer.pack(jarFile, out);230jos = new JarOutputStream(finalout);231Unpacker unpacker = Pack200.newUnpacker();232unpacker.unpack(packFile, jos);233} catch (IOException ioe) {234fatalError(ioe);235} finally {236if (jarFile != null) {237jarFile.close();238}239if (out != null) {240out.close();241}242if (jos != null) {243jos.close();244}245if (tmpfile != null && tmpfile.exists()) {246tmpfile.delete();247}248if (packFile != null && packFile.exists()) {249packFile.delete();250}251}252}253} else if (uflag) {254File inputFile = null, tmpFile = null;255FileInputStream in;256FileOutputStream out;257if (fname != null) {258inputFile = new File(fname);259tmpFile = createTempFileInSameDirectoryAs(inputFile);260in = new FileInputStream(inputFile);261out = new FileOutputStream(tmpFile);262} else {263in = new FileInputStream(FileDescriptor.in);264out = new FileOutputStream(FileDescriptor.out);265vflag = false;266}267InputStream manifest = (!Mflag && (mname != null)) ?268(new FileInputStream(mname)) : null;269expand(null, files, true);270boolean updateOk = update(in, new BufferedOutputStream(out),271manifest, null);272if (ok) {273ok = updateOk;274}275in.close();276out.close();277if (manifest != null) {278manifest.close();279}280if (ok && fname != null) {281// on Win32, we need this delete282inputFile.delete();283if (!tmpFile.renameTo(inputFile)) {284tmpFile.delete();285throw new IOException(getMsg("error.write.file"));286}287tmpFile.delete();288}289} else if (tflag) {290replaceFSC(files);291if (fname != null) {292list(fname, files);293} else {294InputStream in = new FileInputStream(FileDescriptor.in);295try {296list(new BufferedInputStream(in), files);297} finally {298in.close();299}300}301} else if (xflag) {302replaceFSC(files);303if (fname != null && files != null) {304extract(fname, files);305} else {306InputStream in = (fname == null)307? new FileInputStream(FileDescriptor.in)308: new FileInputStream(fname);309try {310extract(new BufferedInputStream(in), files);311} finally {312in.close();313}314}315} else if (iflag) {316genIndex(rootjar, files);317}318} catch (IOException e) {319fatalError(e);320ok = false;321} catch (Error ee) {322ee.printStackTrace();323ok = false;324} catch (Throwable t) {325t.printStackTrace();326ok = false;327}328out.flush();329err.flush();330return ok;331}332333/**334* Parses command line arguments.335*/336boolean parseArgs(String args[]) {337/* Preprocess and expand @file arguments */338try {339args = CommandLine.parse(args);340} catch (FileNotFoundException e) {341fatalError(formatMsg("error.cant.open", e.getMessage()));342return false;343} catch (IOException e) {344fatalError(e);345return false;346}347/* parse flags */348int count = 1;349try {350String flags = args[0];351if (flags.startsWith("-")) {352flags = flags.substring(1);353}354for (int i = 0; i < flags.length(); i++) {355switch (flags.charAt(i)) {356case 'c':357if (xflag || tflag || uflag || iflag) {358usageError();359return false;360}361cflag = true;362break;363case 'u':364if (cflag || xflag || tflag || iflag) {365usageError();366return false;367}368uflag = true;369break;370case 'x':371if (cflag || uflag || tflag || iflag) {372usageError();373return false;374}375xflag = true;376break;377case 't':378if (cflag || uflag || xflag || iflag) {379usageError();380return false;381}382tflag = true;383break;384case 'M':385Mflag = true;386break;387case 'v':388vflag = true;389break;390case 'f':391fname = args[count++];392break;393case 'm':394mname = args[count++];395break;396case '0':397flag0 = true;398break;399case 'i':400if (cflag || uflag || xflag || tflag) {401usageError();402return false;403}404// do not increase the counter, files will contain rootjar405rootjar = args[count++];406iflag = true;407break;408case 'n':409nflag = true;410break;411case 'e':412ename = args[count++];413break;414case 'P':415pflag = true;416break;417default:418error(formatMsg("error.illegal.option",419String.valueOf(flags.charAt(i))));420usageError();421return false;422}423}424} catch (ArrayIndexOutOfBoundsException e) {425usageError();426return false;427}428if (!cflag && !tflag && !xflag && !uflag && !iflag) {429error(getMsg("error.bad.option"));430usageError();431return false;432}433/* parse file arguments */434int n = args.length - count;435if (n > 0) {436int k = 0;437String[] nameBuf = new String[n];438try {439for (int i = count; i < args.length; i++) {440if (args[i].equals("-C")) {441/* change the directory */442String dir = args[++i];443dir = (dir.endsWith(File.separator) ?444dir : (dir + File.separator));445dir = dir.replace(File.separatorChar, '/');446while (dir.indexOf("//") > -1) {447dir = dir.replace("//", "/");448}449paths.add(dir.replace(File.separatorChar, '/'));450nameBuf[k++] = dir + args[++i];451} else {452nameBuf[k++] = args[i];453}454}455} catch (ArrayIndexOutOfBoundsException e) {456usageError();457return false;458}459files = new String[k];460System.arraycopy(nameBuf, 0, files, 0, k);461} else if (cflag && (mname == null)) {462error(getMsg("error.bad.cflag"));463usageError();464return false;465} else if (uflag) {466if ((mname != null) || (ename != null)) {467/* just want to update the manifest */468return true;469} else {470error(getMsg("error.bad.uflag"));471usageError();472return false;473}474}475return true;476}477478/**479* Expands list of files to process into full list of all files that480* can be found by recursively descending directories.481*/482void expand(File dir, String[] files, boolean isUpdate) {483if (files == null) {484return;485}486for (int i = 0; i < files.length; i++) {487File f;488if (dir == null) {489f = new File(files[i]);490} else {491f = new File(dir, files[i]);492}493if (f.isFile()) {494if (entries.add(f)) {495if (isUpdate)496entryMap.put(entryName(f.getPath()), f);497}498} else if (f.isDirectory()) {499if (entries.add(f)) {500if (isUpdate) {501String dirPath = f.getPath();502dirPath = (dirPath.endsWith(File.separator)) ? dirPath :503(dirPath + File.separator);504entryMap.put(entryName(dirPath), f);505}506expand(f, f.list(), isUpdate);507}508} else {509error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));510ok = false;511}512}513}514515/**516* Creates a new JAR file.517*/518void create(OutputStream out, Manifest manifest)519throws IOException520{521ZipOutputStream zos = new JarOutputStream(out);522if (flag0) {523zos.setMethod(ZipOutputStream.STORED);524}525if (manifest != null) {526if (vflag) {527output(getMsg("out.added.manifest"));528}529ZipEntry e = new ZipEntry(MANIFEST_DIR);530e.setTime(System.currentTimeMillis());531e.setSize(0);532e.setCrc(0);533zos.putNextEntry(e);534e = new ZipEntry(MANIFEST_NAME);535e.setTime(System.currentTimeMillis());536if (flag0) {537crc32Manifest(e, manifest);538}539zos.putNextEntry(e);540manifest.write(zos);541zos.closeEntry();542}543for (File file: entries) {544addFile(zos, file);545}546zos.close();547}548549private char toUpperCaseASCII(char c) {550return (c < 'a' || c > 'z') ? c : (char) (c + 'A' - 'a');551}552553/**554* Compares two strings for equality, ignoring case. The second555* argument must contain only upper-case ASCII characters.556* We don't want case comparison to be locale-dependent (else we557* have the notorious "turkish i bug").558*/559private boolean equalsIgnoreCase(String s, String upper) {560assert upper.toUpperCase(java.util.Locale.ENGLISH).equals(upper);561int len;562if ((len = s.length()) != upper.length())563return false;564for (int i = 0; i < len; i++) {565char c1 = s.charAt(i);566char c2 = upper.charAt(i);567if (c1 != c2 && toUpperCaseASCII(c1) != c2)568return false;569}570return true;571}572573/**574* Updates an existing jar file.575*/576boolean update(InputStream in, OutputStream out,577InputStream newManifest,578JarIndex jarIndex) throws IOException579{580ZipInputStream zis = new ZipInputStream(in);581ZipOutputStream zos = new JarOutputStream(out);582ZipEntry e = null;583boolean foundManifest = false;584boolean updateOk = true;585586if (jarIndex != null) {587addIndex(jarIndex, zos);588}589590// put the old entries first, replace if necessary591while ((e = zis.getNextEntry()) != null) {592String name = e.getName();593594boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);595596if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME))597|| (Mflag && isManifestEntry)) {598continue;599} else if (isManifestEntry && ((newManifest != null) ||600(ename != null))) {601foundManifest = true;602if (newManifest != null) {603// Don't read from the newManifest InputStream, as we604// might need it below, and we can't re-read the same data605// twice.606FileInputStream fis = new FileInputStream(mname);607boolean ambiguous = isAmbiguousMainClass(new Manifest(fis));608fis.close();609if (ambiguous) {610return false;611}612}613614// Update the manifest.615Manifest old = new Manifest(zis);616if (newManifest != null) {617old.read(newManifest);618}619if (!updateManifest(old, zos)) {620return false;621}622} else {623if (!entryMap.containsKey(name)) { // copy the old stuff624// do our own compression625ZipEntry e2 = new ZipEntry(name);626e2.setMethod(e.getMethod());627e2.setTime(e.getTime());628e2.setComment(e.getComment());629e2.setExtra(e.getExtra());630if (e.getMethod() == ZipEntry.STORED) {631e2.setSize(e.getSize());632e2.setCrc(e.getCrc());633}634zos.putNextEntry(e2);635copy(zis, zos);636} else { // replace with the new files637File f = entryMap.get(name);638addFile(zos, f);639entryMap.remove(name);640entries.remove(f);641}642}643}644645// add the remaining new files646for (File f: entries) {647addFile(zos, f);648}649if (!foundManifest) {650if (newManifest != null) {651Manifest m = new Manifest(newManifest);652updateOk = !isAmbiguousMainClass(m);653if (updateOk) {654if (!updateManifest(m, zos)) {655updateOk = false;656}657}658} else if (ename != null) {659if (!updateManifest(new Manifest(), zos)) {660updateOk = false;661}662}663}664zis.close();665zos.close();666return updateOk;667}668669private void addIndex(JarIndex index, ZipOutputStream zos)670throws IOException671{672ZipEntry e = new ZipEntry(INDEX_NAME);673e.setTime(System.currentTimeMillis());674if (flag0) {675CRC32OutputStream os = new CRC32OutputStream();676index.write(os);677os.updateEntry(e);678}679zos.putNextEntry(e);680index.write(zos);681zos.closeEntry();682}683684private boolean updateManifest(Manifest m, ZipOutputStream zos)685throws IOException686{687addVersion(m);688addCreatedBy(m);689if (ename != null) {690addMainClass(m, ename);691}692ZipEntry e = new ZipEntry(MANIFEST_NAME);693e.setTime(System.currentTimeMillis());694if (flag0) {695crc32Manifest(e, m);696}697zos.putNextEntry(e);698m.write(zos);699if (vflag) {700output(getMsg("out.update.manifest"));701}702return true;703}704705private static final boolean isWinDriveLetter(char c) {706return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));707}708709private String safeName(String name) {710if (!pflag) {711int len = name.length();712int i = name.lastIndexOf("../");713if (i == -1) {714i = 0;715} else {716i += 3; // strip any dot-dot components717}718if (File.separatorChar == '\\') {719// the spec requests no drive letter. skip if720// the entry name has one.721while (i < len) {722int off = i;723if (i + 1 < len &&724name.charAt(i + 1) == ':' &&725isWinDriveLetter(name.charAt(i))) {726i += 2;727}728while (i < len && name.charAt(i) == '/') {729i++;730}731if (i == off) {732break;733}734}735} else {736while (i < len && name.charAt(i) == '/') {737i++;738}739}740if (i != 0) {741name = name.substring(i);742}743}744return name;745}746747private String entryName(String name) {748name = name.replace(File.separatorChar, '/');749String matchPath = "";750for (String path : paths) {751if (name.startsWith(path)752&& (path.length() > matchPath.length())) {753matchPath = path;754}755}756name = name.substring(matchPath.length());757name = safeName(name);758// the old implementaton doesn't remove759// "./" if it was led by "/" (?)760if (name.startsWith("./")) {761name = name.substring(2);762}763return name;764}765766private void addVersion(Manifest m) {767Attributes global = m.getMainAttributes();768if (global.getValue(Attributes.Name.MANIFEST_VERSION) == null) {769global.put(Attributes.Name.MANIFEST_VERSION, VERSION);770}771}772773private void addCreatedBy(Manifest m) {774Attributes global = m.getMainAttributes();775if (global.getValue(new Attributes.Name("Created-By")) == null) {776String javaVendor = System.getProperty("java.vendor");777String jdkVersion = System.getProperty("java.version");778global.put(new Attributes.Name("Created-By"), jdkVersion + " (" +779javaVendor + ")");780}781}782783private void addMainClass(Manifest m, String mainApp) {784Attributes global = m.getMainAttributes();785786// overrides any existing Main-Class attribute787global.put(Attributes.Name.MAIN_CLASS, mainApp);788}789790private boolean isAmbiguousMainClass(Manifest m) {791if (ename != null) {792Attributes global = m.getMainAttributes();793if ((global.get(Attributes.Name.MAIN_CLASS) != null)) {794error(getMsg("error.bad.eflag"));795usageError();796return true;797}798}799return false;800}801802/**803* Adds a new file entry to the ZIP output stream.804*/805void addFile(ZipOutputStream zos, File file) throws IOException {806String name = file.getPath();807boolean isDir = file.isDirectory();808if (isDir) {809name = name.endsWith(File.separator) ? name :810(name + File.separator);811}812name = entryName(name);813814if (name.equals("") || name.equals(".") || name.equals(zname)) {815return;816} else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME))817&& !Mflag) {818if (vflag) {819output(formatMsg("out.ignore.entry", name));820}821return;822}823824long size = isDir ? 0 : file.length();825826if (vflag) {827out.print(formatMsg("out.adding", name));828}829ZipEntry e = new ZipEntry(name);830e.setTime(file.lastModified());831if (size == 0) {832e.setMethod(ZipEntry.STORED);833e.setSize(0);834e.setCrc(0);835} else if (flag0) {836crc32File(e, file);837}838zos.putNextEntry(e);839if (!isDir) {840copy(file, zos);841}842zos.closeEntry();843/* report how much compression occurred. */844if (vflag) {845size = e.getSize();846long csize = e.getCompressedSize();847out.print(formatMsg2("out.size", String.valueOf(size),848String.valueOf(csize)));849if (e.getMethod() == ZipEntry.DEFLATED) {850long ratio = 0;851if (size != 0) {852ratio = ((size - csize) * 100) / size;853}854output(formatMsg("out.deflated", String.valueOf(ratio)));855} else {856output(getMsg("out.stored"));857}858}859}860861/**862* A buffer for use only by copy(InputStream, OutputStream).863* Not as clean as allocating a new buffer as needed by copy,864* but significantly more efficient.865*/866private byte[] copyBuf = new byte[8192];867868/**869* Copies all bytes from the input stream to the output stream.870* Does not close or flush either stream.871*872* @param from the input stream to read from873* @param to the output stream to write to874* @throws IOException if an I/O error occurs875*/876private void copy(InputStream from, OutputStream to) throws IOException {877int n;878while ((n = from.read(copyBuf)) != -1)879to.write(copyBuf, 0, n);880}881882/**883* Copies all bytes from the input file to the output stream.884* Does not close or flush the output stream.885*886* @param from the input file to read from887* @param to the output stream to write to888* @throws IOException if an I/O error occurs889*/890private void copy(File from, OutputStream to) throws IOException {891InputStream in = new FileInputStream(from);892try {893copy(in, to);894} finally {895in.close();896}897}898899/**900* Copies all bytes from the input stream to the output file.901* Does not close the input stream.902*903* @param from the input stream to read from904* @param to the output file to write to905* @throws IOException if an I/O error occurs906*/907private void copy(InputStream from, File to) throws IOException {908OutputStream out = new FileOutputStream(to);909try {910copy(from, out);911} finally {912out.close();913}914}915916/**917* Computes the crc32 of a Manifest. This is necessary when the918* ZipOutputStream is in STORED mode.919*/920private void crc32Manifest(ZipEntry e, Manifest m) throws IOException {921CRC32OutputStream os = new CRC32OutputStream();922m.write(os);923os.updateEntry(e);924}925926/**927* Computes the crc32 of a File. This is necessary when the928* ZipOutputStream is in STORED mode.929*/930private void crc32File(ZipEntry e, File f) throws IOException {931CRC32OutputStream os = new CRC32OutputStream();932copy(f, os);933if (os.n != f.length()) {934throw new JarException(formatMsg(935"error.incorrect.length", f.getPath()));936}937os.updateEntry(e);938}939940void replaceFSC(String files[]) {941if (files != null) {942for (int i = 0; i < files.length; i++) {943files[i] = files[i].replace(File.separatorChar, '/');944}945}946}947948@SuppressWarnings("serial")949Set<ZipEntry> newDirSet() {950return new HashSet<ZipEntry>() {951public boolean add(ZipEntry e) {952return ((e == null || useExtractionTime) ? false : super.add(e));953}};954}955956void updateLastModifiedTime(Set<ZipEntry> zes) throws IOException {957for (ZipEntry ze : zes) {958long lastModified = ze.getTime();959if (lastModified != -1) {960String name = safeName(ze.getName().replace(File.separatorChar, '/'));961if (name.length() != 0) {962File f = new File(name.replace('/', File.separatorChar));963f.setLastModified(lastModified);964}965}966}967}968969/**970* Extracts specified entries from JAR file.971*/972void extract(InputStream in, String files[]) throws IOException {973ZipInputStream zis = new ZipInputStream(in);974ZipEntry e;975// Set of all directory entries specified in archive. Disallows976// null entries. Disallows all entries if using pre-6.0 behavior.977Set<ZipEntry> dirs = newDirSet();978while ((e = zis.getNextEntry()) != null) {979if (files == null) {980dirs.add(extractFile(zis, e));981} else {982String name = e.getName();983for (String file : files) {984if (name.startsWith(file)) {985dirs.add(extractFile(zis, e));986break;987}988}989}990}991992// Update timestamps of directories specified in archive with their993// timestamps as given in the archive. We do this after extraction,994// instead of during, because creating a file in a directory changes995// that directory's timestamp.996updateLastModifiedTime(dirs);997}998999/**1000* Extracts specified entries from JAR file, via ZipFile.1001*/1002void extract(String fname, String files[]) throws IOException {1003ZipFile zf = new ZipFile(fname);1004Set<ZipEntry> dirs = newDirSet();1005Enumeration<? extends ZipEntry> zes = zf.entries();1006while (zes.hasMoreElements()) {1007ZipEntry e = zes.nextElement();1008if (files == null) {1009dirs.add(extractFile(zf.getInputStream(e), e));1010} else {1011String name = e.getName();1012for (String file : files) {1013if (name.startsWith(file)) {1014dirs.add(extractFile(zf.getInputStream(e), e));1015break;1016}1017}1018}1019}1020zf.close();1021updateLastModifiedTime(dirs);1022}10231024/**1025* Extracts next entry from JAR file, creating directories as needed. If1026* the entry is for a directory which doesn't exist prior to this1027* invocation, returns that entry, otherwise returns null.1028*/1029ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {1030ZipEntry rc = null;1031// The spec requres all slashes MUST be forward '/', it is possible1032// an offending zip/jar entry may uses the backwards slash in its1033// name. It might cause problem on Windows platform as it skips1034// our "safe" check for leading slahs and dot-dot. So replace them1035// with '/'.1036String name = safeName(e.getName().replace(File.separatorChar, '/'));1037if (name.length() == 0) {1038return rc; // leading '/' or 'dot-dot' only path1039}1040File f = new File(name.replace('/', File.separatorChar));1041if (e.isDirectory()) {1042if (f.exists()) {1043if (!f.isDirectory()) {1044throw new IOException(formatMsg("error.create.dir",1045f.getPath()));1046}1047} else {1048if (!f.mkdirs()) {1049throw new IOException(formatMsg("error.create.dir",1050f.getPath()));1051} else {1052rc = e;1053}1054}10551056if (vflag) {1057output(formatMsg("out.create", name));1058}1059} else {1060if (f.getParent() != null) {1061File d = new File(f.getParent());1062if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {1063throw new IOException(formatMsg(1064"error.create.dir", d.getPath()));1065}1066}1067try {1068copy(is, f);1069} finally {1070if (is instanceof ZipInputStream)1071((ZipInputStream)is).closeEntry();1072else1073is.close();1074}1075if (vflag) {1076if (e.getMethod() == ZipEntry.DEFLATED) {1077output(formatMsg("out.inflated", name));1078} else {1079output(formatMsg("out.extracted", name));1080}1081}1082}1083if (!useExtractionTime) {1084long lastModified = e.getTime();1085if (lastModified != -1) {1086f.setLastModified(lastModified);1087}1088}1089return rc;1090}10911092/**1093* Lists contents of JAR file.1094*/1095void list(InputStream in, String files[]) throws IOException {1096ZipInputStream zis = new ZipInputStream(in);1097ZipEntry e;1098while ((e = zis.getNextEntry()) != null) {1099/*1100* In the case of a compressed (deflated) entry, the entry size1101* is stored immediately following the entry data and cannot be1102* determined until the entry is fully read. Therefore, we close1103* the entry first before printing out its attributes.1104*/1105zis.closeEntry();1106printEntry(e, files);1107}1108}11091110/**1111* Lists contents of JAR file, via ZipFile.1112*/1113void list(String fname, String files[]) throws IOException {1114ZipFile zf = new ZipFile(fname);1115Enumeration<? extends ZipEntry> zes = zf.entries();1116while (zes.hasMoreElements()) {1117printEntry(zes.nextElement(), files);1118}1119zf.close();1120}11211122/**1123* Outputs the class index table to the INDEX.LIST file of the1124* root jar file.1125*/1126void dumpIndex(String rootjar, JarIndex index) throws IOException {1127File jarFile = new File(rootjar);1128Path jarPath = jarFile.toPath();1129Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();1130try {1131if (update(Files.newInputStream(jarPath),1132Files.newOutputStream(tmpPath),1133null, index)) {1134try {1135Files.move(tmpPath, jarPath, REPLACE_EXISTING);1136} catch (IOException e) {1137throw new IOException(getMsg("error.write.file"), e);1138}1139}1140} finally {1141Files.deleteIfExists(tmpPath);1142}1143}11441145private HashSet<String> jarPaths = new HashSet<String>();11461147/**1148* Generates the transitive closure of the Class-Path attribute for1149* the specified jar file.1150*/1151List<String> getJarPath(String jar) throws IOException {1152List<String> files = new ArrayList<String>();1153files.add(jar);1154jarPaths.add(jar);11551156// take out the current path1157String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1));11581159// class path attribute will give us jar file name with1160// '/' as separators, so we need to change them to the1161// appropriate one before we open the jar file.1162JarFile rf = new JarFile(jar.replace('/', File.separatorChar));11631164if (rf != null) {1165Manifest man = rf.getManifest();1166if (man != null) {1167Attributes attr = man.getMainAttributes();1168if (attr != null) {1169String value = attr.getValue(Attributes.Name.CLASS_PATH);1170if (value != null) {1171StringTokenizer st = new StringTokenizer(value);1172while (st.hasMoreTokens()) {1173String ajar = st.nextToken();1174if (!ajar.endsWith("/")) { // it is a jar file1175ajar = path.concat(ajar);1176/* check on cyclic dependency */1177if (! jarPaths.contains(ajar)) {1178files.addAll(getJarPath(ajar));1179}1180}1181}1182}1183}1184}1185}1186rf.close();1187return files;1188}11891190/**1191* Generates class index file for the specified root jar file.1192*/1193void genIndex(String rootjar, String[] files) throws IOException {1194List<String> jars = getJarPath(rootjar);1195int njars = jars.size();1196String[] jarfiles;11971198if (njars == 1 && files != null) {1199// no class-path attribute defined in rootjar, will1200// use command line specified list of jars1201for (int i = 0; i < files.length; i++) {1202jars.addAll(getJarPath(files[i]));1203}1204njars = jars.size();1205}1206jarfiles = jars.toArray(new String[njars]);1207JarIndex index = new JarIndex(jarfiles);1208dumpIndex(rootjar, index);1209}12101211/**1212* Prints entry information, if requested.1213*/1214void printEntry(ZipEntry e, String[] files) throws IOException {1215if (files == null) {1216printEntry(e);1217} else {1218String name = e.getName();1219for (String file : files) {1220if (name.startsWith(file)) {1221printEntry(e);1222return;1223}1224}1225}1226}12271228/**1229* Prints entry information.1230*/1231void printEntry(ZipEntry e) throws IOException {1232if (vflag) {1233StringBuilder sb = new StringBuilder();1234String s = Long.toString(e.getSize());1235for (int i = 6 - s.length(); i > 0; --i) {1236sb.append(' ');1237}1238sb.append(s).append(' ').append(new Date(e.getTime()).toString());1239sb.append(' ').append(e.getName());1240output(sb.toString());1241} else {1242output(e.getName());1243}1244}12451246/**1247* Prints usage message.1248*/1249void usageError() {1250error(getMsg("usage"));1251}12521253/**1254* A fatal exception has been caught. No recovery possible1255*/1256void fatalError(Exception e) {1257e.printStackTrace();1258}12591260/**1261* A fatal condition has been detected; message is "s".1262* No recovery possible1263*/1264void fatalError(String s) {1265error(program + ": " + s);1266}12671268/**1269* Print an output message; like verbose output and the like1270*/1271protected void output(String s) {1272out.println(s);1273}12741275/**1276* Print an error message; like something is broken1277*/1278protected void error(String s) {1279err.println(s);1280}12811282/**1283* Main routine to start program.1284*/1285public static void main(String args[]) {1286Main jartool = new Main(System.out, System.err, "jar");1287System.exit(jartool.run(args) ? 0 : 1);1288}12891290/**1291* An OutputStream that doesn't send its output anywhere, (but could).1292* It's here to find the CRC32 of an input file, necessary for STORED1293* mode in ZIP.1294*/1295private static class CRC32OutputStream extends java.io.OutputStream {1296final CRC32 crc = new CRC32();1297long n = 0;12981299CRC32OutputStream() {}13001301public void write(int r) throws IOException {1302crc.update(r);1303n++;1304}13051306public void write(byte[] b, int off, int len) throws IOException {1307crc.update(b, off, len);1308n += len;1309}13101311/**1312* Updates a ZipEntry which describes the data read by this1313* output stream, in STORED mode.1314*/1315public void updateEntry(ZipEntry e) {1316e.setMethod(ZipEntry.STORED);1317e.setSize(n);1318e.setCrc(crc.getValue());1319}1320}13211322/**1323* Attempt to create temporary file in the system-provided temporary folder, if failed attempts1324* to create it in the same folder as the file in parameter (if any)1325*/1326private File createTemporaryFile(String tmpbase, String suffix) {1327File tmpfile = null;13281329try {1330tmpfile = File.createTempFile(tmpbase, suffix);1331} catch (IOException | SecurityException e) {1332// Unable to create file due to permission violation or security exception1333}1334if (tmpfile == null) {1335// Were unable to create temporary file, fall back to temporary file in the same folder1336if (fname != null) {1337try {1338File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();1339tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);1340} catch (IOException ioe) {1341// Last option failed - fall gracefully1342fatalError(ioe);1343}1344} else {1345// No options left - we can not compress to stdout without access to the temporary folder1346fatalError(new IOException(getMsg("error.create.tempfile")));1347}1348}1349return tmpfile;1350}1351}135213531354