import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.TextField;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DecompAllForm
{
private static Label statusLabel = new Label("no start");
private static List logList = new List();
private static Thread processThread = null;
private static ArrayList clzs = new ArrayList();
private static String src=null;
private static String destFolder=null;
private static boolean running=true;
private static SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");
private static ArrayList supportList = new ArrayList();
public static void main(String[] args)
{
final Frame window = new Frame();
window.setSize(400, 400);
window.setLayout(null);
window.setLocation(480, 320);
window.setResizable(false);
//window.setBackground(Color.LIGHT_GRAY);
window.setTitle("Decompile All - By Wafly");
window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
{
window.dispose();
System.exit(0);
}});
Label srcPathLabel = new Label("Source path:");
srcPathLabel.setSize(100, 20);
srcPathLabel.setLocation(20, 50);
window.add(srcPathLabel);
final TextField srcPathInput = new TextField("input the package path or folder path");
srcPathInput.setSize(260, 20);
srcPathInput.setLocation(120, 50);
srcPathInput.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0)
{
srcPathInput.selectAll();
}
public void focusLost(FocusEvent arg0) { }
});
window.add(srcPathInput);
Label distPathLabel = new Label("Destination path:");
distPathLabel.setSize(100, 20);
distPathLabel.setLocation(20, 100);
window.add(distPathLabel);
final TextField distPathInput = new TextField("input the folder path");
distPathInput.setSize(260, 20);
distPathInput.setLocation(120, 100);
distPathInput.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0)
{
distPathInput.selectAll();
}
public void focusLost(FocusEvent arg0) { }
});
window.add(distPathInput);
Label statusTitleLabel = new Label("Current status:");
statusTitleLabel.setSize(100, 20);
statusTitleLabel.setLocation(20, 150);
window.add(statusTitleLabel);
statusLabel.setSize(250, 20);
statusLabel.setLocation(120, 150);
window.add(statusLabel);
final Button startBtn = new Button("START");
startBtn.setSize(100, 20);
startBtn.setLocation(70, 200);
startBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
processThread = new Thread()
{
public void run()
{
try {
src = srcPathInput.getText();
destFolder = distPathInput.getText();
if(!new File(src).exists())
{
statusLabel.setText("Source path/package not exists!");
return;
}
File folder = new File(destFolder);
if(destFolder.replace("\\", "/").indexOf("/")<0)
{
statusLabel.setText("Destination path format not correct!");
return;
}
if(!folder.exists() && !folder.mkdirs())
{
statusLabel.setText("Fail to create destination folder!");
return;
}
if(!folder.isDirectory())
{
statusLabel.setText("Destination is not a folder!");
return;
}
process();
statusLabel.setText("Task completed.");
} catch (Exception e) {
statusLabel.setText("Error: "+e.getMessage());
addLog(e.getMessage());
e.printStackTrace();
} finally {
processThread=null;
startBtn.setEnabled(true);
}
}
};
startBtn.setEnabled(false);
logList.removeAll();
clzs.clear();
running=true;
processThread.start();
statusLabel.setText("Thread started.");
}});
window.add(startBtn);
Button stopBtn = new Button("STOP");
stopBtn.setSize(100, 20);
stopBtn.setLocation(230, 200);
stopBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
if(processThread==null)
return;
try {
processThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
processThread=null;
running=false;
statusLabel.setText("Thread force stopped !!");
startBtn.setEnabled(true);
}});
window.add(stopBtn);
logList.setSize(360, 120);
logList.setLocation(20, 250);
window.add(logList);
window.setVisible(true);
stopBtn.requestFocus();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("SupportPkg.list")));
String fileType = null;
while((fileType=reader.readLine())!=null)
{
supportList.add(fileType);
addLog("Add support package type: *"+fileType);
}
reader.close();
} catch (Exception e) {
statusLabel.setText("Error: Fail to load package type.");
addLog(e.getMessage());
e.printStackTrace();
}
new DropTarget(window, DnDConstants.ACTION_COPY, new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent event) {
event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors();
try {
if (flavors[0].equals(DataFlavor.javaFileListFlavor)) {
File f = (File)((java.util.List) transferable.getTransferData(flavors[0])).get(0);
srcPathInput.setText(f.getAbsolutePath());
}
} catch (Exception e) {
statusLabel.setText("Fail to locate the source path.");
}
event.dropComplete(true);
}
});
}
private static void process() throws Exception
{
statusLabel.setText("Unzip package...");
unzipEARnWARnJARnZIP(src);
if(new File(src).isFile())
{
if(src.substring(src.lastIndexOf(".")).equalsIgnoreCase(".class"))
clzs.add(src);
else
src=src.substring(0, src.lastIndexOf("."));
}
statusLabel.setText("Calculating...");
listFiles(src);
if(clzs.size()==0)
{
throw new Exception("Class file not found.");
}
for(int i=0; i<clzs.size() && running; i++)
{
statusLabel.setText("Decompiling..."+((i+1)*100/clzs.size())+"% ("+(i+1)+" / "+clzs.size()+")");
addLog("Decompile "+clzs.get(i));
try {
Runtime.getRuntime().exec("jad -o -r -lnc -sjava -d "+destFolder+" "+clzs.get(i));
} catch (Exception e) {
statusLabel.setText("Fail to locate the source path.");
}
}
}
public static void unzipEARnWARnJARnZIP(String path) throws Exception
{
if(!running)
return;
File parents = new File(path);
if(parents.isFile())
{
String filePath = parents.getAbsolutePath();
filePath.replace("\\", "/");
int extIndex = filePath.lastIndexOf(".");
if(extIndex<1)
return;
String fileExt = filePath.substring(extIndex);
if(supportList.contains(fileExt.toLowerCase()))
{
String targetUnzipPath=filePath.substring(0, extIndex);
File targetUnzipPathFile = new File(targetUnzipPath);
if(targetUnzipPathFile.exists()&&!targetUnzipPathFile.isDirectory())
throw new Exception("Fail to create target folder ["+targetUnzipPath+"].");
targetUnzipPathFile.mkdirs();
addLog("Unzip "+parents.getName());
try {
Runtime.getRuntime().exec("winrar x -o+ -inul "+filePath+" "+targetUnzipPath).waitFor();
} catch (Exception e) {
e.printStackTrace();
}
unzipEARnWARnJARnZIP(targetUnzipPath);
}
}
else if(parents.isDirectory())
{
File[] children = parents.listFiles();
for(int i=0; i<children.length; i++)
unzipEARnWARnJARnZIP(children[i].getAbsolutePath());
}
}
private static void listFiles(String path)
{
if(!running)
return;
File[] fds = new File(path).listFiles();
if(fds==null)
return;
for(int i=0; i<fds.length; i++)
{
if(fds[i].isFile() && fds[i].getName().toLowerCase().endsWith(".class") && fds[i].getName().indexOf("$")<0)
{
clzs.add(fds[i].getAbsolutePath());
}
else if(fds[i].isDirectory())
listFiles(fds[i].getAbsolutePath());
}
}
private static void addLog(String log)
{
format.format(new Date());
logList.add("["+format.format(new Date())+"] "+log);
logList.select(logList.getItemCount()-1);
}
}