jarx.java

来自「dump3 morpheus 0.2.9 src」· Java 代码 · 共 136 行

JAVA
136
字号
/**
 * DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
 * Copyright 2005 Alexander Gr&auml;sser<BR>
 * All Rights Reserved, http://dump3.sourceforge.net/<BR>
 * <BR>
 * This file is part of DuMP3.<BR>
 * <BR>
 * DuMP3 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later version.<BR>
 * <BR>
 * DuMP3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.<BR>
 * <BR>
 * You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.za.grasser.duplicate;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * This class does the equivalent of jar -xf, but sice jar.exe is not shipped with the JRE, I had to write my own!
 * 
 * @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
 * @version $Revision: 1.5 $
 */
public class Jarx {
  /**
   * private constructor for utility class
   */
  private Jarx() {
    super();
  }

  /**
   * @param pArgs
   * @return a regular expression for the wildcards
   */
  private static Pattern makeRegex(final String[] pArgs) {
    final Set<String> s = new HashSet<String>();
    for (int i = 1; i < pArgs.length; i++) {
      s.add(pArgs[i]);
    }
    if (s.isEmpty()) {
      s.add("*.so");
      s.add("*.dll");
      s.add("*.jnilib");
    }
    final StringBuffer reg = new StringBuffer(10);
    boolean more = false;
    for (final String t : s) {
      if (more) {
        reg.append("|");
      }
      if (t.startsWith("-r") || t.startsWith("/r")) {
        // already a regex
        reg.append("(");
        reg.append(t.substring(2));
        reg.append(")");
      } else {
        // a normal wildcard
        reg.append("(");
        reg.append(t.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*").replaceAll("\\?", "."));
        reg.append("$)");
      }
      more = true;
    }
    System.out.println("regex:" + reg);
    return Pattern.compile(reg.toString());
  }

  /**
   * Extracts all dll, so and jnilib files from pJar
   * 
   * @param pArgs
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  private static void extract(final String[] pArgs) throws Exception {
    final File currentArchive = new File(pArgs[0]);
    final File outputDir = new File("./");
    final ZipFile zf = new ZipFile(currentArchive);
    final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zf.entries();
    final Pattern regex = makeRegex(pArgs);
    while (entries.hasMoreElements()) {
      final ZipEntry entry = entries.nextElement();
      final String pathname = entry.getName();
      if (regex.matcher(pathname).matches()) {
        System.out.println("Extracting: " + pathname);
        final File outFile = new File(outputDir, pathname);
        if (!outFile.getParentFile().exists()) {
          outFile.getParentFile().mkdirs();
        }
        final InputStream in = zf.getInputStream(entry);
        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
        final byte[] buf = new byte[1024];
        while (true) {
          final int nRead = in.read(buf, 0, buf.length);
          if (nRead <= 0) {
            break;
          }
          out.write(buf, 0, nRead);
        }
        in.close();
        out.close();
      }
    }
    zf.close();
  }

  /**
   * @param args
   */
  public static void main(final String[] args) {
    if (args.length < 1) {
      System.out.println("Usage: Jarx path_to_jar [wildcard] [wildcard2] ...");
      System.exit(1);
    }
    try {
      Jarx.extract(args);
    } catch (final Exception e) {
      System.err.println("Could not extract " + args[0] + " because: " + e.getMessage());
      e.printStackTrace(System.err);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?