home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / tools / jar / Main.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  10.2 KB  |  487 lines

  1. package sun.tools.jar;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileDescriptor;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.io.PrintStream;
  13. import java.util.Date;
  14. import java.util.Hashtable;
  15. import java.util.Vector;
  16. import java.util.zip.CRC32;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipInputStream;
  19. import java.util.zip.ZipOutputStream;
  20.  
  21. public class Main {
  22.    String program;
  23.    PrintStream out;
  24.    PrintStream err;
  25.    String fname;
  26.    String mname;
  27.    String zname = "";
  28.    String[] files;
  29.    CRC32 crc32 = new CRC32();
  30.    boolean cflag;
  31.    boolean xflag;
  32.    boolean tflag;
  33.    boolean vflag;
  34.    boolean flag0;
  35.    boolean Mflag;
  36.    static final String MANIFEST = "META-INF/MANIFEST.MF";
  37.    static final char SEPARATOR;
  38.  
  39.    public Main(PrintStream var1, PrintStream var2, String var3) {
  40.       this.out = var1;
  41.       this.err = var2;
  42.       this.program = var3;
  43.    }
  44.  
  45.    public synchronized boolean run(String[] var1) {
  46.       boolean var2 = true;
  47.       if (!this.parseArgs(var1)) {
  48.          return false;
  49.       } else {
  50.          try {
  51.             if (this.cflag) {
  52.                FileOutputStream var3;
  53.                if (this.fname != null) {
  54.                   var3 = new FileOutputStream(this.fname);
  55.                   this.zname = this.fname.replace(File.separatorChar, '/');
  56.                   if (this.zname.startsWith("./")) {
  57.                      this.zname = this.zname.substring(2);
  58.                   }
  59.                } else {
  60.                   var3 = new FileOutputStream(FileDescriptor.out);
  61.                }
  62.  
  63.                Manifest var4 = null;
  64.                if (!this.Mflag) {
  65.                   if (this.mname != null) {
  66.                      FileInputStream var5 = new FileInputStream(this.mname);
  67.                      var4 = new Manifest(new BufferedInputStream(var5));
  68.                   } else {
  69.                      var4 = new Manifest((String[])null);
  70.                   }
  71.                }
  72.  
  73.                this.create(new BufferedOutputStream(var3), this.expand(this.files), var4);
  74.             } else if (this.xflag || this.tflag) {
  75.                FileInputStream var9;
  76.                if (this.fname != null) {
  77.                   var9 = new FileInputStream(this.fname);
  78.                } else {
  79.                   var9 = new FileInputStream(FileDescriptor.in);
  80.                }
  81.  
  82.                if (this.xflag) {
  83.                   this.extract(new BufferedInputStream(var9), this.files);
  84.                } else {
  85.                   this.list(new BufferedInputStream(var9), this.files);
  86.                }
  87.             }
  88.          } catch (IOException var6) {
  89.             this.fatalError((Exception)var6);
  90.             var2 = false;
  91.          } catch (Error var7) {
  92.             ((Throwable)var7).printStackTrace();
  93.             var2 = false;
  94.          } catch (Throwable var8) {
  95.             var8.printStackTrace();
  96.             var2 = false;
  97.          }
  98.  
  99.          this.out.flush();
  100.          this.err.flush();
  101.          return var2;
  102.       }
  103.    }
  104.  
  105.    boolean parseArgs(String[] var1) {
  106.       int var2 = 1;
  107.  
  108.       try {
  109.          String var3 = var1[0];
  110.          if (var3.startsWith("-")) {
  111.             var3 = var3.substring(1);
  112.          }
  113.  
  114.          for(int var4 = 0; var4 < var3.length(); ++var4) {
  115.             switch (var3.charAt(var4)) {
  116.                case '0':
  117.                   this.flag0 = true;
  118.                   break;
  119.                case 'M':
  120.                   this.Mflag = true;
  121.                   break;
  122.                case 'c':
  123.                   if (this.xflag || this.tflag) {
  124.                      this.usageError();
  125.                      return false;
  126.                   }
  127.  
  128.                   this.cflag = true;
  129.                   break;
  130.                case 'f':
  131.                   this.fname = var1[var2++];
  132.                   break;
  133.                case 'm':
  134.                   this.mname = var1[var2++];
  135.                   break;
  136.                case 't':
  137.                   if (this.cflag || this.xflag) {
  138.                      this.usageError();
  139.                      return false;
  140.                   }
  141.  
  142.                   this.tflag = true;
  143.                   break;
  144.                case 'v':
  145.                   this.vflag = true;
  146.                   break;
  147.                case 'x':
  148.                   if (this.cflag || this.tflag) {
  149.                      this.usageError();
  150.                      return false;
  151.                   }
  152.  
  153.                   this.xflag = true;
  154.                   break;
  155.                default:
  156.                   this.error("Illegal option: " + var3.charAt(var4));
  157.                   this.usageError();
  158.                   return false;
  159.             }
  160.          }
  161.       } catch (ArrayIndexOutOfBoundsException var5) {
  162.          this.usageError();
  163.          return false;
  164.       }
  165.  
  166.       if (!this.cflag && !this.tflag && !this.xflag) {
  167.          this.error("One of options -{ctx} must be specified.");
  168.          this.usageError();
  169.          return false;
  170.       } else {
  171.          if (var1.length - var2 > 0) {
  172.             this.files = new String[var1.length - var2];
  173.             System.arraycopy(var1, var2, this.files, 0, this.files.length);
  174.          } else if (this.cflag) {
  175.             this.error("'c' flag requires that input files specified!");
  176.             this.usageError();
  177.             return false;
  178.          }
  179.  
  180.          return true;
  181.       }
  182.    }
  183.  
  184.    String[] expand(String[] var1) {
  185.       Vector var2 = new Vector();
  186.       this.expand((File)null, var1, var2, new Hashtable());
  187.       var1 = new String[var2.size()];
  188.  
  189.       for(int var3 = 0; var3 < var1.length; ++var3) {
  190.          var1[var3] = ((File)var2.elementAt(var3)).getPath();
  191.       }
  192.  
  193.       return var1;
  194.    }
  195.  
  196.    void expand(File var1, String[] var2, Vector var3, Hashtable var4) {
  197.       if (var2 != null) {
  198.          for(int var5 = 0; var5 < var2.length; ++var5) {
  199.             File var6;
  200.             if (var1 == null) {
  201.                var6 = new File(var2[var5]);
  202.             } else {
  203.                var6 = new File(var1, var2[var5]);
  204.             }
  205.  
  206.             if (var6.isFile()) {
  207.                if (!var4.contains(var6)) {
  208.                   var4.put(var6, var6);
  209.                   var3.addElement(var6);
  210.                }
  211.             } else if (var6.isDirectory()) {
  212.                File var7 = var6.getPath().endsWith(File.separator) ? var6 : new File(var6.getPath() + File.separator);
  213.                var4.put(var7, var7);
  214.                var3.addElement(var7);
  215.                this.expand(var6, var6.list(), var3, var4);
  216.             } else {
  217.                this.error(var6 + ": no such file or directory");
  218.             }
  219.          }
  220.  
  221.       }
  222.    }
  223.  
  224.    void create(OutputStream var1, String[] var2, Manifest var3) throws IOException {
  225.       ZipOutputStream var4 = new ZipOutputStream(var1);
  226.       if (this.flag0) {
  227.          var4.setMethod(0);
  228.       }
  229.  
  230.       if (var3 != null) {
  231.          var3.addFiles((File)null, var2);
  232.          ZipEntry var5 = new ZipEntry("META-INF/MANIFEST.MF");
  233.          var5.setTime(System.currentTimeMillis());
  234.          if (this.flag0) {
  235.             this.crc32Manifest(var5, var3);
  236.          }
  237.  
  238.          var4.putNextEntry(var5);
  239.          var3.stream(var4);
  240.          var4.closeEntry();
  241.       }
  242.  
  243.       for(int var6 = 0; var6 < var2.length; ++var6) {
  244.          this.addFile(var4, new File(var2[var6]));
  245.       }
  246.  
  247.       var4.close();
  248.    }
  249.  
  250.    private String entryName(String var1) {
  251.       var1 = var1.replace(File.separatorChar, '/');
  252.       if (var1.startsWith("/")) {
  253.          var1 = var1.substring(1);
  254.       } else if (var1.startsWith("./")) {
  255.          var1 = var1.substring(2);
  256.       }
  257.  
  258.       return var1;
  259.    }
  260.  
  261.    void addFile(ZipOutputStream var1, File var2) throws IOException {
  262.       String var3 = this.entryName(var2.getPath());
  263.       if (!var3.equals("") && !var3.equals(".") && !var3.equals(this.zname)) {
  264.          boolean var4 = var2.isDirectory();
  265.          long var5 = var4 ? 0L : var2.length();
  266.          if (this.vflag) {
  267.             this.err.print("adding: " + var3 + " ");
  268.          }
  269.  
  270.          ZipEntry var7 = new ZipEntry(var3);
  271.          var7.setTime(var2.lastModified());
  272.          if (var5 == 0L) {
  273.             var7.setMethod(0);
  274.             var7.setSize(0L);
  275.             var7.setCrc(0L);
  276.          } else if (this.flag0) {
  277.             var7.setSize(var5);
  278.             this.crc32File(var7, var2);
  279.          }
  280.  
  281.          var1.putNextEntry(var7);
  282.          if (!var4) {
  283.             byte[] var8 = new byte[1024];
  284.             BufferedInputStream var10 = new BufferedInputStream(new FileInputStream(var2));
  285.  
  286.             int var9;
  287.             while((var9 = ((InputStream)var10).read(var8, 0, var8.length)) != -1) {
  288.                var1.write(var8, 0, var9);
  289.             }
  290.  
  291.             ((InputStream)var10).close();
  292.          }
  293.  
  294.          var1.closeEntry();
  295.          if (this.vflag) {
  296.             var5 = var7.getSize();
  297.             long var13 = var7.getCompressedSize();
  298.             this.err.print("(in=" + var5 + ") (out=" + var13 + ") ");
  299.             if (var7.getMethod() == 8) {
  300.                long var14 = 0L;
  301.                if (var5 != 0L) {
  302.                   var14 = (var5 - var13) * 100L / var5;
  303.                }
  304.  
  305.                this.output("(deflated " + var14 + "%)");
  306.                return;
  307.             }
  308.  
  309.             this.output("(stored 0%)");
  310.          }
  311.  
  312.       }
  313.    }
  314.  
  315.    private void crc32Manifest(ZipEntry var1, Manifest var2) throws IOException {
  316.       this.crc32.reset();
  317.       CRC32OutputStream var3 = new CRC32OutputStream(this.crc32);
  318.       PrintStream var4 = new PrintStream(var3);
  319.       var2.stream(var4);
  320.       var1.setSize((long)var3.n);
  321.       var1.setCrc(this.crc32.getValue());
  322.    }
  323.  
  324.    private void crc32File(ZipEntry var1, File var2) throws IOException {
  325.       BufferedInputStream var3 = new BufferedInputStream(new FileInputStream(var2));
  326.       byte[] var4 = new byte[1024];
  327.       this.crc32.reset();
  328.       int var5 = 0;
  329.       int var6 = 0;
  330.       long var7 = var2.length();
  331.  
  332.       while((var5 = ((InputStream)var3).read(var4)) != -1) {
  333.          var6 += var5;
  334.          this.crc32.update(var4, 0, var5);
  335.       }
  336.  
  337.       ((InputStream)var3).close();
  338.       if (var6 != (int)var7) {
  339.          throw new JarException("incorrect length while processing: " + var2.getPath());
  340.       } else {
  341.          var1.setCrc(this.crc32.getValue());
  342.       }
  343.    }
  344.  
  345.    void extract(InputStream var1, String[] var2) throws IOException {
  346.       ZipInputStream var3 = new ZipInputStream(var1);
  347.  
  348.       ZipEntry var4;
  349.       while((var4 = var3.getNextEntry()) != null) {
  350.          if (var2 == null) {
  351.             this.extractFile(var3, var4);
  352.          } else {
  353.             String var5 = var4.getName().replace('/', File.separatorChar);
  354.  
  355.             for(int var6 = 0; var6 < var2.length; ++var6) {
  356.                if (var5.startsWith(var2[var6])) {
  357.                   this.extractFile(var3, var4);
  358.                   break;
  359.                }
  360.             }
  361.          }
  362.       }
  363.  
  364.    }
  365.  
  366.    void extractFile(ZipInputStream var1, ZipEntry var2) throws IOException {
  367.       File var3 = new File(var2.getName().replace('/', File.separatorChar));
  368.       if (var2.isDirectory()) {
  369.          if (!var3.exists() && !var3.mkdirs() || !var3.isDirectory()) {
  370.             throw new IOException(var3 + ": could not create directory");
  371.          }
  372.  
  373.          if (this.vflag) {
  374.             this.output("   created: " + var3.getPath());
  375.             return;
  376.          }
  377.       } else {
  378.          if (var3.getParent() != null) {
  379.             File var4 = new File(var3.getParent());
  380.             if (!var4.exists() && !var4.mkdirs() || !var4.isDirectory()) {
  381.                throw new IOException(var4 + ": could not create directory");
  382.             }
  383.          }
  384.  
  385.          FileOutputStream var7 = new FileOutputStream(var3);
  386.          byte[] var5 = new byte[512];
  387.  
  388.          int var6;
  389.          while((var6 = var1.read(var5, 0, var5.length)) != -1) {
  390.             ((OutputStream)var7).write(var5, 0, var6);
  391.          }
  392.  
  393.          var1.closeEntry();
  394.          ((OutputStream)var7).close();
  395.          if (this.vflag) {
  396.             if (var2.getMethod() == 8) {
  397.                this.output(" extracted: " + var3.getPath());
  398.                return;
  399.             }
  400.  
  401.             this.output("  inflated: " + var3.getPath());
  402.          }
  403.       }
  404.  
  405.    }
  406.  
  407.    void list(InputStream var1, String[] var2) throws IOException {
  408.       ZipInputStream var3 = new ZipInputStream(var1);
  409.  
  410.       ZipEntry var4;
  411.       while((var4 = var3.getNextEntry()) != null) {
  412.          String var5 = var4.getName().replace('/', File.separatorChar);
  413.          var3.closeEntry();
  414.          if (var2 == null) {
  415.             this.printEntry(var4);
  416.          } else {
  417.             for(int var6 = 0; var6 < var2.length; ++var6) {
  418.                if (var5.startsWith(var2[var6])) {
  419.                   this.printEntry(var4);
  420.                   break;
  421.                }
  422.             }
  423.          }
  424.       }
  425.  
  426.    }
  427.  
  428.    void printEntry(ZipEntry var1) throws IOException {
  429.       if (!this.vflag) {
  430.          this.output(var1.getName());
  431.       } else {
  432.          StringBuffer var2 = new StringBuffer();
  433.          String var3 = Long.toString(var1.getSize());
  434.  
  435.          for(int var4 = 6 - var3.length(); var4 > 0; --var4) {
  436.             var2.append(' ');
  437.          }
  438.  
  439.          var2.append(var3).append(' ').append((new Date(var1.getTime())).toString());
  440.          var2.append(' ').append(var1.getName());
  441.          this.output(var2.toString());
  442.       }
  443.    }
  444.  
  445.    void usageError() {
  446.       this.error("Usage: jar {ctx}[vfm0M] [jar-file] [manifest-file] files ...");
  447.       this.error("Options:");
  448.       this.error("  -c  create new archive");
  449.       this.error("  -t  list table of contents for archive");
  450.       this.error("  -x  extract named (or all) files from archive");
  451.       this.error("  -v  generate verbose output on standard error");
  452.       this.error("  -f  specify archive file name");
  453.       this.error("  -m  include manifest information from specified manifest file");
  454.       this.error("  -0  store only; use no ZIP compression");
  455.       this.error("  -M  Do not create a manifest file for the entries\n");
  456.       this.error("If any file is a directory then it is processed recursively.");
  457.       this.error("Example: to archive two class files into an archive called classes.jar: ");
  458.       this.error("  jar cvf classes.jar Foo.class Bar.class ");
  459.       this.error("Note: use the '0' option to create a jar file that can be put in your CLASSPATH");
  460.    }
  461.  
  462.    void fatalError(Exception var1) {
  463.       ((Throwable)var1).printStackTrace();
  464.    }
  465.  
  466.    void fatalError(String var1) {
  467.       this.error(this.program + ": " + var1);
  468.    }
  469.  
  470.    protected void output(String var1) {
  471.       this.err.println(var1);
  472.    }
  473.  
  474.    protected void error(String var1) {
  475.       this.err.println(var1);
  476.    }
  477.  
  478.    public static void main(String[] var0) {
  479.       Main var1 = new Main(System.out, System.err, "jar");
  480.       System.exit(var1.run(var0) ? 0 : 1);
  481.    }
  482.  
  483.    static {
  484.       SEPARATOR = File.separatorChar;
  485.    }
  486. }
  487.