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();
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-1125472.html