Beispiel #1
0
  public static boolean extractJarDir(String jarfile, String outfile) throws Exception {
    long filesize = new File(jarfile).length();
    JarInputStream jar = new JarInputStream(new FileInputStream(jarfile));
    // while (true) {
    //	JarEntry jarentry = (JarEntry)jar.getNextJarEntry();
    //	if (jarentry==null) break;
    //	String maindir = jarentry.getName();
    //	// get rid of "./" and "/" prefix
    //	if (maindir.startsWith("."))
    //		maindir = maindir.substring(1);
    //	if (maindir.startsWith(File.separator))
    //		maindir = maindir.substring(1);
    //	// Find manifest
    //	System.out.println(maindir);
    //	if (maindir.equals("META-INF/MANIFEST.MF")) {
    //		java.io.InputStream ins = jar;

    java.io.FileOutputStream outs = new java.io.FileOutputStream(outfile);
    Manifest man = jar.getManifest();
    if (man == null) return false;
    man.write(outs);
    byte[] buf = ("MIDlet-Jar-Size: " + filesize).getBytes();
    outs.write(buf, 0, buf.length);
    outs.close();
    //		// copy buffered (is a lot faster than one byte at a time)
    //		byte[] buf = new byte[8192];
    //		int total_bytes=0;
    //		while (true) {
    //			//outs.write(ins.read());
    //			int len = ins.read(buf);
    //			if (len < 0) break;
    //			outs.write(buf,0,len);
    //			total_bytes += len;
    //		}
    //		buf = ("MIDlet-Jar-Size: "+total_bytes).getBytes();
    //		outs.write(buf,0,buf.length);
    //		outs.close();
    //		//ins.close();
    //	}
    // }
    // return false;
    // remove empty lines from the generated jad which may have been
    // introduced by manifest write.
    // some systems have trouble with empty lines
    List<String> lines = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader(outfile));
    while (true) {
      String line = in.readLine();
      if (line == null) break;
      lines.add(line);
    }
    in.close();
    PrintWriter outp = new PrintWriter(outfile);
    for (String s : lines) {
      if (s.trim().equals("")) continue;
      outp.println(s);
    }
    outp.close();
    return true;
  }
  public void unpack(String artifact, File destination) {
    File archiveFile = getAsFile(artifact);

    if (archiveFile == null || !archiveFile.exists()) return;
    if (archiveFile.isDirectory()) {
      try {
        Files.walkFileTree(archiveFile.toPath(), new CopyDirVisitor(archiveFile, destination));
      } catch (Exception e) {
        OutputBouble.reportError(e);
      }

    } else if (archiveFile.getName().endsWith("jar")) {
      destination.mkdirs();
      try {
        java.util.jar.JarFile jar = new java.util.jar.JarFile(archiveFile);
        java.util.Enumeration jarEnum = jar.entries();
        while (jarEnum.hasMoreElements()) {
          java.util.jar.JarEntry file = (java.util.jar.JarEntry) jarEnum.nextElement();
          java.io.File f = new java.io.File(destination + java.io.File.separator + file.getName());
          if (file.isDirectory()) { // if its a directory, create it
            f.mkdir();
            continue;
          }
          java.io.InputStream is = jar.getInputStream(file); // get the input stream
          java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
          while (is.available() > 0) { // write contents of 'is' to 'fos'
            fos.write(is.read());
          }
          fos.close();
          is.close();
        }
        jar.close();
      } catch (Exception e) {

      }
    }
  }