home *** CD-ROM | disk | FTP | other *** search
- package sun.tools.jar;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileDescriptor;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.util.Date;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.zip.CRC32;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
-
- public class Main {
- String program;
- PrintStream out;
- PrintStream err;
- String fname;
- String mname;
- String zname = "";
- String[] files;
- CRC32 crc32 = new CRC32();
- boolean cflag;
- boolean xflag;
- boolean tflag;
- boolean vflag;
- boolean flag0;
- boolean Mflag;
- static final String MANIFEST = "META-INF/MANIFEST.MF";
- static final char SEPARATOR;
-
- public Main(PrintStream var1, PrintStream var2, String var3) {
- this.out = var1;
- this.err = var2;
- this.program = var3;
- }
-
- public synchronized boolean run(String[] var1) {
- boolean var2 = true;
- if (!this.parseArgs(var1)) {
- return false;
- } else {
- try {
- if (this.cflag) {
- FileOutputStream var3;
- if (this.fname != null) {
- var3 = new FileOutputStream(this.fname);
- this.zname = this.fname.replace(File.separatorChar, '/');
- if (this.zname.startsWith("./")) {
- this.zname = this.zname.substring(2);
- }
- } else {
- var3 = new FileOutputStream(FileDescriptor.out);
- }
-
- Manifest var4 = null;
- if (!this.Mflag) {
- if (this.mname != null) {
- FileInputStream var5 = new FileInputStream(this.mname);
- var4 = new Manifest(new BufferedInputStream(var5));
- } else {
- var4 = new Manifest((String[])null);
- }
- }
-
- this.create(new BufferedOutputStream(var3), this.expand(this.files), var4);
- } else if (this.xflag || this.tflag) {
- FileInputStream var9;
- if (this.fname != null) {
- var9 = new FileInputStream(this.fname);
- } else {
- var9 = new FileInputStream(FileDescriptor.in);
- }
-
- if (this.xflag) {
- this.extract(new BufferedInputStream(var9), this.files);
- } else {
- this.list(new BufferedInputStream(var9), this.files);
- }
- }
- } catch (IOException var6) {
- this.fatalError((Exception)var6);
- var2 = false;
- } catch (Error var7) {
- ((Throwable)var7).printStackTrace();
- var2 = false;
- } catch (Throwable var8) {
- var8.printStackTrace();
- var2 = false;
- }
-
- this.out.flush();
- this.err.flush();
- return var2;
- }
- }
-
- boolean parseArgs(String[] var1) {
- int var2 = 1;
-
- try {
- String var3 = var1[0];
- if (var3.startsWith("-")) {
- var3 = var3.substring(1);
- }
-
- for(int var4 = 0; var4 < var3.length(); ++var4) {
- switch (var3.charAt(var4)) {
- case '0':
- this.flag0 = true;
- break;
- case 'M':
- this.Mflag = true;
- break;
- case 'c':
- if (this.xflag || this.tflag) {
- this.usageError();
- return false;
- }
-
- this.cflag = true;
- break;
- case 'f':
- this.fname = var1[var2++];
- break;
- case 'm':
- this.mname = var1[var2++];
- break;
- case 't':
- if (this.cflag || this.xflag) {
- this.usageError();
- return false;
- }
-
- this.tflag = true;
- break;
- case 'v':
- this.vflag = true;
- break;
- case 'x':
- if (this.cflag || this.tflag) {
- this.usageError();
- return false;
- }
-
- this.xflag = true;
- break;
- default:
- this.error("Illegal option: " + var3.charAt(var4));
- this.usageError();
- return false;
- }
- }
- } catch (ArrayIndexOutOfBoundsException var5) {
- this.usageError();
- return false;
- }
-
- if (!this.cflag && !this.tflag && !this.xflag) {
- this.error("One of options -{ctx} must be specified.");
- this.usageError();
- return false;
- } else {
- if (var1.length - var2 > 0) {
- this.files = new String[var1.length - var2];
- System.arraycopy(var1, var2, this.files, 0, this.files.length);
- } else if (this.cflag) {
- this.error("'c' flag requires that input files specified!");
- this.usageError();
- return false;
- }
-
- return true;
- }
- }
-
- String[] expand(String[] var1) {
- Vector var2 = new Vector();
- this.expand((File)null, var1, var2, new Hashtable());
- var1 = new String[var2.size()];
-
- for(int var3 = 0; var3 < var1.length; ++var3) {
- var1[var3] = ((File)var2.elementAt(var3)).getPath();
- }
-
- return var1;
- }
-
- void expand(File var1, String[] var2, Vector var3, Hashtable var4) {
- if (var2 != null) {
- for(int var5 = 0; var5 < var2.length; ++var5) {
- File var6;
- if (var1 == null) {
- var6 = new File(var2[var5]);
- } else {
- var6 = new File(var1, var2[var5]);
- }
-
- if (var6.isFile()) {
- if (!var4.contains(var6)) {
- var4.put(var6, var6);
- var3.addElement(var6);
- }
- } else if (var6.isDirectory()) {
- File var7 = var6.getPath().endsWith(File.separator) ? var6 : new File(var6.getPath() + File.separator);
- var4.put(var7, var7);
- var3.addElement(var7);
- this.expand(var6, var6.list(), var3, var4);
- } else {
- this.error(var6 + ": no such file or directory");
- }
- }
-
- }
- }
-
- void create(OutputStream var1, String[] var2, Manifest var3) throws IOException {
- ZipOutputStream var4 = new ZipOutputStream(var1);
- if (this.flag0) {
- var4.setMethod(0);
- }
-
- if (var3 != null) {
- var3.addFiles((File)null, var2);
- ZipEntry var5 = new ZipEntry("META-INF/MANIFEST.MF");
- var5.setTime(System.currentTimeMillis());
- if (this.flag0) {
- this.crc32Manifest(var5, var3);
- }
-
- var4.putNextEntry(var5);
- var3.stream(var4);
- var4.closeEntry();
- }
-
- for(int var6 = 0; var6 < var2.length; ++var6) {
- this.addFile(var4, new File(var2[var6]));
- }
-
- var4.close();
- }
-
- private String entryName(String var1) {
- var1 = var1.replace(File.separatorChar, '/');
- if (var1.startsWith("/")) {
- var1 = var1.substring(1);
- } else if (var1.startsWith("./")) {
- var1 = var1.substring(2);
- }
-
- return var1;
- }
-
- void addFile(ZipOutputStream var1, File var2) throws IOException {
- String var3 = this.entryName(var2.getPath());
- if (!var3.equals("") && !var3.equals(".") && !var3.equals(this.zname)) {
- boolean var4 = var2.isDirectory();
- long var5 = var4 ? 0L : var2.length();
- if (this.vflag) {
- this.err.print("adding: " + var3 + " ");
- }
-
- ZipEntry var7 = new ZipEntry(var3);
- var7.setTime(var2.lastModified());
- if (var5 == 0L) {
- var7.setMethod(0);
- var7.setSize(0L);
- var7.setCrc(0L);
- } else if (this.flag0) {
- var7.setSize(var5);
- this.crc32File(var7, var2);
- }
-
- var1.putNextEntry(var7);
- if (!var4) {
- byte[] var8 = new byte[1024];
- BufferedInputStream var10 = new BufferedInputStream(new FileInputStream(var2));
-
- int var9;
- while((var9 = ((InputStream)var10).read(var8, 0, var8.length)) != -1) {
- var1.write(var8, 0, var9);
- }
-
- ((InputStream)var10).close();
- }
-
- var1.closeEntry();
- if (this.vflag) {
- var5 = var7.getSize();
- long var13 = var7.getCompressedSize();
- this.err.print("(in=" + var5 + ") (out=" + var13 + ") ");
- if (var7.getMethod() == 8) {
- long var14 = 0L;
- if (var5 != 0L) {
- var14 = (var5 - var13) * 100L / var5;
- }
-
- this.output("(deflated " + var14 + "%)");
- return;
- }
-
- this.output("(stored 0%)");
- }
-
- }
- }
-
- private void crc32Manifest(ZipEntry var1, Manifest var2) throws IOException {
- this.crc32.reset();
- CRC32OutputStream var3 = new CRC32OutputStream(this.crc32);
- PrintStream var4 = new PrintStream(var3);
- var2.stream(var4);
- var1.setSize((long)var3.n);
- var1.setCrc(this.crc32.getValue());
- }
-
- private void crc32File(ZipEntry var1, File var2) throws IOException {
- BufferedInputStream var3 = new BufferedInputStream(new FileInputStream(var2));
- byte[] var4 = new byte[1024];
- this.crc32.reset();
- int var5 = 0;
- int var6 = 0;
- long var7 = var2.length();
-
- while((var5 = ((InputStream)var3).read(var4)) != -1) {
- var6 += var5;
- this.crc32.update(var4, 0, var5);
- }
-
- ((InputStream)var3).close();
- if (var6 != (int)var7) {
- throw new JarException("incorrect length while processing: " + var2.getPath());
- } else {
- var1.setCrc(this.crc32.getValue());
- }
- }
-
- void extract(InputStream var1, String[] var2) throws IOException {
- ZipInputStream var3 = new ZipInputStream(var1);
-
- ZipEntry var4;
- while((var4 = var3.getNextEntry()) != null) {
- if (var2 == null) {
- this.extractFile(var3, var4);
- } else {
- String var5 = var4.getName().replace('/', File.separatorChar);
-
- for(int var6 = 0; var6 < var2.length; ++var6) {
- if (var5.startsWith(var2[var6])) {
- this.extractFile(var3, var4);
- break;
- }
- }
- }
- }
-
- }
-
- void extractFile(ZipInputStream var1, ZipEntry var2) throws IOException {
- File var3 = new File(var2.getName().replace('/', File.separatorChar));
- if (var2.isDirectory()) {
- if (!var3.exists() && !var3.mkdirs() || !var3.isDirectory()) {
- throw new IOException(var3 + ": could not create directory");
- }
-
- if (this.vflag) {
- this.output(" created: " + var3.getPath());
- return;
- }
- } else {
- if (var3.getParent() != null) {
- File var4 = new File(var3.getParent());
- if (!var4.exists() && !var4.mkdirs() || !var4.isDirectory()) {
- throw new IOException(var4 + ": could not create directory");
- }
- }
-
- FileOutputStream var7 = new FileOutputStream(var3);
- byte[] var5 = new byte[512];
-
- int var6;
- while((var6 = var1.read(var5, 0, var5.length)) != -1) {
- ((OutputStream)var7).write(var5, 0, var6);
- }
-
- var1.closeEntry();
- ((OutputStream)var7).close();
- if (this.vflag) {
- if (var2.getMethod() == 8) {
- this.output(" extracted: " + var3.getPath());
- return;
- }
-
- this.output(" inflated: " + var3.getPath());
- }
- }
-
- }
-
- void list(InputStream var1, String[] var2) throws IOException {
- ZipInputStream var3 = new ZipInputStream(var1);
-
- ZipEntry var4;
- while((var4 = var3.getNextEntry()) != null) {
- String var5 = var4.getName().replace('/', File.separatorChar);
- var3.closeEntry();
- if (var2 == null) {
- this.printEntry(var4);
- } else {
- for(int var6 = 0; var6 < var2.length; ++var6) {
- if (var5.startsWith(var2[var6])) {
- this.printEntry(var4);
- break;
- }
- }
- }
- }
-
- }
-
- void printEntry(ZipEntry var1) throws IOException {
- if (!this.vflag) {
- this.output(var1.getName());
- } else {
- StringBuffer var2 = new StringBuffer();
- String var3 = Long.toString(var1.getSize());
-
- for(int var4 = 6 - var3.length(); var4 > 0; --var4) {
- var2.append(' ');
- }
-
- var2.append(var3).append(' ').append((new Date(var1.getTime())).toString());
- var2.append(' ').append(var1.getName());
- this.output(var2.toString());
- }
- }
-
- void usageError() {
- this.error("Usage: jar {ctx}[vfm0M] [jar-file] [manifest-file] files ...");
- this.error("Options:");
- this.error(" -c create new archive");
- this.error(" -t list table of contents for archive");
- this.error(" -x extract named (or all) files from archive");
- this.error(" -v generate verbose output on standard error");
- this.error(" -f specify archive file name");
- this.error(" -m include manifest information from specified manifest file");
- this.error(" -0 store only; use no ZIP compression");
- this.error(" -M Do not create a manifest file for the entries\n");
- this.error("If any file is a directory then it is processed recursively.");
- this.error("Example: to archive two class files into an archive called classes.jar: ");
- this.error(" jar cvf classes.jar Foo.class Bar.class ");
- this.error("Note: use the '0' option to create a jar file that can be put in your CLASSPATH");
- }
-
- void fatalError(Exception var1) {
- ((Throwable)var1).printStackTrace();
- }
-
- void fatalError(String var1) {
- this.error(this.program + ": " + var1);
- }
-
- protected void output(String var1) {
- this.err.println(var1);
- }
-
- protected void error(String var1) {
- this.err.println(var1);
- }
-
- public static void main(String[] var0) {
- Main var1 = new Main(System.out, System.err, "jar");
- System.exit(var1.run(var0) ? 0 : 1);
- }
-
- static {
- SEPARATOR = File.separatorChar;
- }
- }
-