使用第三方procyon-decompiler-0.5.30.jar对class文件进行反编译

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;


import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.IOUtils;


import com.strobel.decompiler.Decompiler;
import com.strobel.decompiler.DecompilerSettings;
import com.strobel.decompiler.PlainTextOutput;


public class DecompilerUtils {
public static void main(String[] args) throws IOException {
// javaDecompiler("E:\\aaa\\RuleAction.class");
//解压jar
uncompressZip("E:\\aaa\\aaaaaa\\jd-core-0.7.1.jar");
//遍历目录,对所有class文件反编译
File rootFile = new File("E:\\aaa\\aaaaaa\\jd-core-0.7.1");
if (rootFile.isDirectory()) {
listDirectoryDecompiler(rootFile);
}

}
/**
* 遍历目录,对class进行反编译
*/
public static void listDirectoryDecompiler(File rootFile){
File[] files = rootFile.listFiles();
if (files != null && files.length > 0) {
for(File file:files){
                if(file.isDirectory())
                    //递归
                listDirectoryDecompiler(file);
                else{
                if (file.getName().endsWith(".class")) {
                //对class文件进行反编译
                javaDecompiler(file.getAbsolutePath());
}
                }
            }
}
}
/**
* 对class文件进行反编译
* @param classFilePath  class文件完整路径
*/
public static void javaDecompiler(String classFilePath){
File file = new File(classFilePath);
String fileName = null;
if (file.isFile() && file.getName().endsWith(".class")) {
fileName = classFilePath.substring(0,classFilePath.lastIndexOf("."));
final DecompilerSettings settings = DecompilerSettings.javaDefaults();
try (final FileOutputStream stream = new FileOutputStream(fileName + ".java");
final OutputStreamWriter writer = new OutputStreamWriter(stream)) {
// Decompiler.decompile("E:\\aaa\\RuleAction.class", new PlainTextOutput(writer), settings);
Decompiler.decompile(classFilePath , new PlainTextOutput(writer), settings); 
} catch (final IOException e) {
// handle error 
}
}
}

/**
* 解压zip,解压jar,解压到当前文件夹下
* @param zipFilePath zip或jar文件完整路径
* @throws IOException 
*/
public static void uncompressZip(String zipFilePath) throws IOException{
File file = new File(zipFilePath);
String fileName = null;
if (file.isFile() && (file.getName().endsWith(".zip") || file.getName().endsWith(".jar"))) {
fileName = zipFilePath.substring(0,zipFilePath.lastIndexOf("."));
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<ZipArchiveEntry> en = zipFile.getEntries();
ZipArchiveEntry ze;
while (en.hasMoreElements()) {
ze = en.nextElement();
File f = new File(fileName, ze.getName());
// 创建完整路径
if (ze.isDirectory()) {
f.mkdirs();
continue;
} else
f.getParentFile().mkdirs();


InputStream is = zipFile.getInputStream(ze);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os, 4096);
is.close();
os.close();
}
zipFile.close();
}


}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值