import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class Client {
JLabel consoleLabel;
static File chooseFile(){
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showSaveDialog(null);
File path = null;
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
path = selectedFile;
}
return path;
}
static File chooseDir(){
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("选择文件目录保存文件 ");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = jfc.showSaveDialog(null);
File path = null;
if (returnValue == JFileChooser.APPROVE_OPTION) {
if (jfc.getSelectedFile().isDirectory()) {
path = jfc.getSelectedFile();
}
}
return path;
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel srcLabel = new JLabel("源文件:");
srcLabel.setBounds(10,20,80,25);
panel.add(srcLabel);
JTextField srcText = new JTextField(20);
srcText.setBounds(100,20,500,25);
panel.add(srcText);
JButton srcButton = new JButton("选择");
srcButton.setBounds(650,20,80,25);
srcButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File srcFile = chooseFile();
srcText.setText(srcFile.getPath());
}
});
panel.add(srcButton);
JLabel destLabel = new JLabel("保存路径:");
destLabel.setBounds(10,60,80,25);
panel.add(destLabel);
JTextField destText = new JTextField(20);
destText.setBounds(100,60,500,25);
panel.add(destText);
JButton destButton = new JButton("选择");
destButton.setBounds(650,60,80,25);
destButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File destFile = chooseDir();
destText.setText(destFile.getPath());
}
});
panel.add(destButton);
JLabel packageLabel = new JLabel("package:");
packageLabel.setBounds(10,100,80,25);
panel.add(packageLabel);
JTextField packageText = new JTextField(20);
packageText.setBounds(100,100,500,25);
panel.add(packageText);
JTextArea consoleArea = new JTextArea();
StringBuilder info = new StringBuilder();
JButton jadButton = new JButton("反编译");
jadButton.setBounds(100,140,80,25);
jadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
info.append("开始解析....\n");
consoleArea.setText(info.toString());
File srcFile = new File(srcText.getText());
File destDir= new File(destText.getText()+"/"+srcFile.getName().
replace(".war", "").replace(".jar", ""));
System.out.println("srcFile: " + srcFile);
System.out.println("destDir: " + destDir);
System.out.println("package: " + packageText.getText());
DecompileClass.unJar(srcFile, destDir.getPath(), packageText.getText());
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
DecompileClass.traverseFolder(new File(destDir.getParent()));
info.append("解析结束!\n");
consoleArea.setText(info.toString());
}
});
panel.add(jadButton);
JLabel consoleLabel = new JLabel("控制台:");
consoleLabel.setBounds(10,180,80,25);
panel.add(consoleLabel);
consoleArea.setBounds(100,180,600,360);
panel.add(consoleArea);
}
public static void main(String[] args) {
JFrame frame = new JFrame("JadPlus, write by 仙草蜜");
frame.setSize(800, 600);
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
}
import java.io.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class DecompileClass {
public static void unJar(File srcFile, String destDirPath, String packagePath) {
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
JarFile jarFile = null;
try {
jarFile = new JarFile(srcFile);
Enumeration<?> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
System.out.println("解压:" + entry.getName());
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
String entryName = entry.getName();
File targetFile = new File(destDirPath + "/" + entryName);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
InputStream is = jarFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
is.close();
if (null!=packagePath && !packagePath.equals("") && entryName.endsWith(".jar") && entryName.contains(packagePath)) {
String destDir2 = new File(destDirPath).getParent() +"/"+ targetFile.getName().replace(".jar", "");
System.out.println("srcFile2: " + targetFile);
System.out.println("destDir2: " + destDir2);
System.out.println("package2: " + packagePath);
unJar(targetFile, destDir2, packagePath);
}
}
}
} catch (Exception e) {
throw new RuntimeException("文件解压发生错误", e);
} finally {
if(jarFile != null){
try {
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void execCmd(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
p.waitFor();
if (p.exitValue() != 0) {
System.out.println("命令执行错误");
}
String s = null;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void traverseFolder(File file) {
File[] files = file.listFiles();
if (null != files && files.length > 0) {
for (File file2 : files) {
if (file2.isDirectory()) {
traverseFolder(file2);
} else {
String fileName = file2.getAbsolutePath();
System.out.println("文件:" + fileName);
if (fileName.endsWith("class")) {
System.out.println("jad.exe -d "+file2.getParent()+" -s java "+fileName);
execCmd("jad.exe -d "+file2.getParent()+" -s java "+fileName);
}
}
}
}
}
}