package zip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class ziptest {
private static File apktool = null;
private static File signapk = null;
private static File testkey_pk8 = null;
private static File testkey_x509_pem = null;
private static File dirFile;// 根目录
public static void main(String args[]) throws IOException{
Process process = null;
apktool = new File(dirFile.getAbsolutePath(), "apktool.bat");
dirFile = new File(System.getProperty("user.dir"));
signapk = new File(dirFile.getAbsolutePath(), "signapk.jar");
testkey_pk8 = new File(dirFile.getAbsolutePath(), "testkey.pk8");
testkey_x509_pem = new File(dirFile.getAbsolutePath(), "testkey.x509.pem");
try {
//1----解压
//apktool路径
//String path = "E:\apktool\apktool.bat";
//保存路径
String appPath = "E:\newapk\";
//process = Runtime.getRuntime().exec("cmd.exe /c"+path+" apktool d "+appPath+"lvping.apk -o"+appPath+"apk");
process = Runtime.getRuntime().exec("cmd.exe /c"+apktool.getPath() + " d –f "+appPath+"lvping.apk -o"+appPath+"apk");
if(process.waitFor()!=0)System.out.println("解压失败!");
//2----添加json文件
String filePath ="E:\newapk\apk";
File f = new File(filePath);
File[] fis = f.listFiles();
for(File ff:fis) {
String paths = ff.getAbsolutePath();
if(paths.contains("assets")) {
String fPath = "E:\newapk\apk\assets";
CreateJson(fPath);
}else {
String fPath =filePath+"\assets";
File fs = new File(fPath);
CreateJson(fPath);
fs.mkdirs();
}
}
//3----打包
// process = Runtime.getRuntime().exec("cmd.exe /c"+path+" b "+appPath+"apk -o"+appPath+"app.apk");
process = Runtime.getRuntime().exec("cmd.exe /c"+apktool.getPath() +" b "+appPath+"apk -o"+appPath+"app.apk");
if(process.waitFor()!=0)System.out.println("打包失败!");
//4----签名 (文件名称中不能包含空格)
//String cmd = "cmd.exe /c E:\apktool\signapk.jar E:\apktool\testkey.x509.pem E:\apktool\testkey.pk8 "+appPath+"app.apk"+ appPath;
String cmd = "cmd.exe /c java -jar" + signapk.getPath() + " " + testkey_x509_pem.getPath() + " "
+ testkey_pk8.getPath() + " " +appPath+"app.apk " + appPath+ "appt.apk";
process = Runtime.getRuntime().exec(cmd);
System.out.println("111213");
if(process.waitFor()!=0) {
System.out.println("签名失败。。。");
}
}catch (Exception e)
{
e.printStackTrace();
}finally{
/* br.close();
osw.close();*/
}
}
public static void CreateJson(String fPath/*,String json*/) {
// 标记文件生成是否成功
boolean flag = true;
// 拼接文件完整路径
String fullPath =fPath+ File.separator + "config.json";
// 生成json格式文件
try {
// 保证创建一个新文件
File file = new File(fullPath);
if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
file.getParentFile().mkdirs();
}
if (file.exists()) { // 如果已存在,删除旧文件
file.delete();
}
file.createNewFile();
// 格式化json字符串
// 将格式化后的字符串写入文件
Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
write.write("{\"user\":\"test1\",\"direction\":\"3.24\"}");
write.flush();
write.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
}
}
package zip;
import java.io.File;
import java.