Sitemap

Thursday, June 25, 2015

Java: Decompiling using Procyon

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import java.util.ArrayList;
import java.util.List;

public class ProcyonDecompiler {

    private static String JAR_DIRECTORY = "C:\\ucm";
    private static String DECOMPILER_FILEPATH = "lib/procyon-decompiler-0.5.29.jar";
    private static List<Path> fileList = new ArrayList<Path>();

    public static void main(String[] args) throws IOException, InterruptedException {
        walkDirectory();
        for (int i = 0; i < fileList.size(); i++) {
            Path absolutePath = fileList.get(i).toAbsolutePath();
            Path parentPath = fileList.get(i).getParent();
            Path fileName = fileList.get(i).getFileName();
            executeJarFile(absolutePath.toString(), parentPath.toString(), trimFileExtension(fileName.toString()));
        }
    }

    private static void executeJarFile(String absolutePath, String parentPath, String filename) throws IOException,
                                                                                                       InterruptedException {
        ProcessBuilder pb =
            new ProcessBuilder("java", "-jar", DECOMPILER_FILEPATH, "-jar", absolutePath, "-o",
                               parentPath + File.separator + filename);
        Process p = pb.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = "";
        while ((s = in.readLine()) != null) {
            System.out.println(s);
        }
        int status = p.waitFor();
        System.out.println("Exited with status: " + status);
    }

    private static String trimFileExtension(String filename) {
        int pos = filename.lastIndexOf(".");
        if (pos > 0) {
            filename = filename.substring(0, pos);
        }
        return filename;
    }

    public static void walkDirectory() throws IOException {
        Path start = FileSystems.getDefault().getPath(JAR_DIRECTORY);
        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(".jar")) {
                    //System.out.println(file);
                    fileList.add(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

No comments:

Post a Comment