gistfile1.txt /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package cn.huawei.com.CompressedSeacher.util;i
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cn.huawei.com.CompressedSeacher.util; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author l00358914 */ public class DecompressJar { private static final DecompressJar instance = new DecompressJar(); private DecompressJar() { } public static DecompressJar getInstance() { return instance; } // private static String removeBlankCharForJarFileName(String inputJarFileName){ // return inputJarFileName.replaceAll("\\s","-"); // } /** * 解压缩JAR包 * * @param fileName 文件名 * @param outputPath 解压输出路径 * @throws IOException IO异常 */ //jad -f -ff -o -r -sjava -dD:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox D:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox.jar\**/*.class //jad -f -ff -o -r -safe -stat -sjava -dD:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox D:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox.jar\**/*.class //jad -f -ff -o -r -safe -stat -sjava -dD:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox D:\decompileTest\lukeall-0.9.9Lucene-Index-Toolbox.jar\**/*.class public static String decompressAndDecompile(JarFile jf, String jarpath, String outputPath) throws IOException { //创建解压包路径 if (!outputPath.endsWith(File.separator)) {//判断是否以文件分隔符结尾 outputPath += File.separator; } outputPath = outputPath + CommonUtil.getInstance().removeBlankCharForJarFileName(jarpath.substring(jarpath.lastIndexOf(File.separator) + 1)) + File.separator; System.out.println("outputPath:" + outputPath); //枚举jar文件中的每个元素 for (Enumeration e = jf.entries(); e.hasMoreElements();) { JarEntry je = (JarEntry) e.nextElement(); //jar中元素条目 String outFileName = outputPath + je.getName(); //解压文件的名称 File f = new File(outFileName); //创建解压文件对象 // System.out.println(f.getAbsolutePath()); //打印绝对路径 // 创建该路径的目录和所有父目录 createFatherDir(outFileName); // 如果是目录,则直接进入下一个循环 if (f.isDirectory()) { continue; } System.out.println("文件创建成功:" + outFileName); InputStream in = null; OutputStream out = null; try { in = jf.getInputStream(je); FileOutputStream fos = new FileOutputStream(f); out = new BufferedOutputStream(fos); byte[] buffer = new byte[2048]; int nBytes = 0; while ((nBytes = in.read(buffer)) > 0) { out.write(buffer, 0, nBytes); } } catch (IOException ioe) { throw ioe; } finally { try { if (out != null) { out.flush(); //立刻写 out.close(); } } catch (IOException ioe) { throw ioe; } finally { if (in != null) { in.close(); } } } } //这里预先创建src文件夹targetSrc File src = new File(outputPath.replaceAll(".jar", ".src")); src.mkdir(); CommonUtil.getInstance().decompileClassFolder(outputPath.replaceAll(".jar", ".src"), outputPath);//反编译class return outputPath.replaceAll(".jar", ".src"); } /** * 循环创建父目录,正则表达式方式 * * @param outFileName */ private static void createFatherDir(String outFileName) { // 匹配分隔符 Pattern p = Pattern.compile("[/\\" + File.separator + "]"); Matcher m = p.matcher(outFileName); // 每找到一个匹配的分隔符,则创建一个该分隔符以前的目录 while (m.find()) { int index = m.start(); String subDir = outFileName.substring(0, index); File subDirFile = new File(subDir); if (!subDirFile.exists()) { subDirFile.mkdir(); System.out.println("目录创建完成:" + subDir); } } } }