📄 jar.java
字号:
} // if the file is already absolute then do not // combine it with the -C argument tmp = new XPFile(files[i]); if (! tmp.isAbsolute()) { tmp = new XPFile(parent,files[i]); } if (tmp.isAbsolute()) { // Find and remove the first '/' in the short name String name = files[i]; int index = name.indexOf('/'); if (index == -1) { // Something really strange happended, the file // is absolute so something is really screwed up throw new RuntimeException("absolute file " + name + " had no '/' chars in it"); } // gaurd against the case where / is at the end of the string if ((index + 1) < name.length()) { files[i] = name.substring(index + 1); } } String absolute_name = tmp.getAbsolutePath(); String canon = tmp.getCanonicalPath(); if (debug) { System.out.println("absolute was \"" + absolute_name + "\""); System.out.println("canon was \"" + canon + "\""); } if (canon.length() < absolute_name.length()) { // Take care of the really nasty case where // a dir path included . or .. which can // really mess up the entry names that get // put into the archive. To fix this we // need to get the last path element in // the canonical path and use that as our // short file name if (debug) { System.out.println("name got smaller"); } int index = canon.lastIndexOf('/'); if (index == -1) { // Something really strange happended, the file // name is absolute so something is really screwed up throw new RuntimeException("absolute file " + canon + " had no '/' chars in it"); } // gaurd against the case where / is at the end of the string if ((index + 1) < canon.length()) { files[i] = canon.substring(index + 1); } } if (debug) { System.out.println("file name is \"" + files[i] + "\""); System.out.println("absolute_name is \"" + absolute_name + "\""); System.out.println("exists() is \"" + tmp.exists() + "\""); System.out.println("isDirectory() is \"" + tmp.isDirectory() + "\""); } if (requireExistence && tmp.exists() == false) { existenceError = true; // Non existant file error message System.out.println(tmp.getPath() + ": no such file or directory"); } absolute_files[i] = absolute_name; } if (existenceError) { // The JDK's jar will print each bad file name before // exiting so we do that too if (debug) { System.out.println("calling exit() because of existence error"); } exit(2); } } // List the file arguments that exist in this jar file // if files argument is null then list them all void listFilesInJar(String[] shortnames) throws IOException { if (debug) { System.out.println("entered Jar.listFilesInJar()"); System.out.flush(); } ZipInputStream zin; ZipEntry entry; //final boolean debug = true; if (debug) { System.out.println("opening archive \"" + archive + "\""); } if (archive == null) { zin = new ZipInputStream(System.in); } else { zin = new ZipInputStream(new XPFileInputStream(archive)); } try { while ((entry = zin.getNextEntry()) != null) { // close entry right after we open it so that // all the data is read in and we can read the entry. zin.closeEntry(); // see if the current ZipEntry's name equals // the file we want to extract. If equal then // print the name of the file to stdout. String entry_name = entry.getName(); boolean match = (shortnames == null); if (! match) { if (debug) { System.out.println("looking for match for \"" + entry_name + "\""); } for (int i = 0; i < shortnames.length ; i++) { if (entry_name.equals(shortnames[i])) { match = true; } } } if (match) { if (debug) { System.out.println("found match for \"" + entry_name + "\""); } if (verbose) { // FORMAT for verbose jar output. // 10 Wed Feb 10 01:42:40 CST 1999 data.txt // print size in at least 6 char wide field // with right justification. int width = 6; String info = String.valueOf(entry.getSize()); if (info.length() > width) { vout.print(info); } else { while (width > info.length()) { vout.print(' '); width--; } vout.print(info); } vout.print(' '); Date date = new Date(entry.getTime()); vout.print(date); vout.print(' '); vout.println(entry_name); } else { vout.println(entry_name); } } } } finally { zin.close(); } } void extractFilesInJar(String[] shortnames, String[] longnames) throws IOException { ZipInputStream zin; ZipEntry entry; //final boolean debug = true; if (debug) { System.out.println("opening archive \"" + archive + "\""); } if (archive == null) { zin = new ZipInputStream(System.in); } else { zin = new ZipInputStream(new XPFileInputStream(archive)); } try { while ((entry = zin.getNextEntry()) != null) { // see if the current ZipEntry's name equals // the file we want to extract. If equal then // print the name of the file to stdout. String entry_name = entry.getName(); boolean match = (shortnames == null); String longname = entry_name; if (! match) { if (debug) { System.out.println("looking for match for \"" + entry_name + "\""); } for (int i = 0; i < shortnames.length ; i++) { if (entry_name.equals(shortnames[i])) { match = true; longname = longnames[i]; } } } if (match) { if (debug) { System.out.println("found match for \"" + entry_name + "\""); } if (verbose) { // FORMAT for verbose jar output. // Actually, there is a bug in the JDK jar // implementation in that the output is // exactly the opposite of what is shown here. // The JDK output makes not sense so we do not // duplicate the bug in this implementation. // for DEFLATED entries // created: META-INF/ // inflated: META-INF/MANIFEST.MF // for STORED entries // created: META-INF/ // extracted: META-INF/MANIFEST.MF // print in at least 10 char wide field // with right justification. int width = 10; String info; if (entry.isDirectory()) { info = "created"; } else { if (entry.getMethod() == ZipEntry.STORED) { info = "extracted"; } else { info = "inflated"; } } if (info.length() > width) { vout.print(info); } else { while (width > info.length()) { vout.print(' '); width--; } vout.print(info); } vout.print(':'); vout.print(' '); vout.println(entry_name); } // Now Extract the entry. if (entry.isDirectory()) { // If the entry is a directory then // we can just create that directory // and go on to the next entry XPFile dir = new XPFile(longname); if (! dir.exists()) { dir.mkdirs(); } continue; } // If it is in a directory that does // not exist we will need to create it. ensureParentsExist(longname); if (debug) { System.out.println("opening output file \"" + longname + "\""); } XPFileOutputStream fos = new XPFileOutputStream(longname); try { readwriteStreams(zin, fos); } finally { fos.close(); } } } } finally { zin.close(); } } void updateFilesInJar(String[] shortnames, String[] longnames) throws IOException { // How can an existing file be updated without just reading // and writing a new file ??? throw new RuntimeException("not implemented yet"); } void createJar(String[] shortnames, String[] longnames) throws IOException { //final boolean debug = true; OutputStream out; if (archive == null) { out = System.out; vout = System.err; } else { // FIXME : test buffering (do we need it, would it help?) //out = new BufferedOutputStream(new XPFileOutputStream(archive)); out = new XPFileOutputStream(archive); } JarOutputStream jos = null; try { // If there is a manifest file that needs to be added to the jar // then we need to create it if (create_manifest) { // generic type so that it will work in JDK1.1 with -M option Manifest jar_manifest; if (manifest == null) { // Use default manifest jar_manifest = new Manifest(); } else { // Use manifest from file InputStream manifest_in = null; try { manifest_in = new BufferedInputStream( new XPFileInputStream(manifest) ); jar_manifest = new Manifest(manifest_in); } finally { if (manifest_in != null) manifest_in.close(); } } // Set manifest version Attributes mainAttr = jar_manifest.getMainAttributes(); mainAttr.put(Attributes.Name.MANIFEST_VERSION, "1.0"); // Checksum all files for (int i=0; i < longnames.length ; i++) { String[] algs = new String[] { "MD5", "SHA" }; XPFile infile = new XPFile(longnames[i]); String name = shortnames[i]; // Skip directories if (!infile.isFile()) { continue; } // Set up attributes for this file Attributes attr = jar_manifest.getAttributes(name); if (attr == null) { attr = new Attributes(); jar_manifest.getEntries().put(name, attr); } attr.putValue("Name", name); // Compute hash values StringBuffer alglist = new StringBuffer(); for (int j = 0; j < algs.length; j++) { DigestInputStream dis; try { dis = new DigestInputStream( new BufferedInputStream( new XPFileInputStream(infile)), MessageDigest.getInstance(algs[j])); } catch (NoSuchAlgorithmException e) { continue; } byte[] buf = new byte[1024]; while (dis.read(buf, 0, buf.length) != -1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -