新增自动打包程序

修复穿山甲打资源包问题
This commit is contained in:
2020-03-13 17:30:40 +08:00
parent 561177d0f4
commit c43093b119
10 changed files with 647 additions and 198 deletions

View File

@@ -3,9 +3,15 @@ package com.qy.utils;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.json.JSONObject;
import static java.util.zip.ZipEntry.STORED;
public class Tools {
public static boolean stringIsNull(String string) {
@@ -48,6 +54,7 @@ public class Tools {
File srcFile = new File(srcFileName);
// 判断源文件是否存在
if (!srcFile.exists()) {
return false;
} else if (!srcFile.isFile()) {
return false;
@@ -58,11 +65,19 @@ public class Tools {
// 如果目标文件所在目录不存在,则创建目录
if (!destFile.exists()) {
// 目标文件所在目录不存在
boolean t=destFile.mkdirs();
if(srcFileName.equals("Activity.smali")){
System.out.println("创建文件夹:"+destFileName+" "+t);
}
if (!destFile.mkdirs()) {
// 复制文件失败:创建目标文件所在目录失败
return false;
}
}else{
if(srcFileName.equals("Activity.smali")){
System.out.println("文件夹已存在:"+destFileName);
}
}
// 复制文件
@@ -109,7 +124,57 @@ public class Tools {
*/
public static boolean copyFile(String srcFileName, String destFileName,
boolean overlay) {
return copyFileToName(srcFileName, destFileName, null, overlay);
if(new File(srcFileName).isDirectory()){
return copyDir(srcFileName,destFileName);
}else {
return copyFileToName(srcFileName, destFileName, null, overlay);
}
}
private static boolean copyDir(String dir,String dest){
try{
//System.out.println("复制文件"+dir+"到"+dest);
File src=new File(dir);
File destFile=new File(dest);
String path="";
int index;
String ta,tb;
if(dir.length()>dest.length()){
index=dir.length();
ta=dir;
tb=dest;
}else{
index=dest.length();
ta=dest;
tb=dir;
}
for (int i = 0; i < index; i++) {
if(tb.length()>(i+1)&&ta.substring(i,i+1).equals(tb.substring(i,i+1))){
path+=ta.substring(i,i+1);
}else {
break;
}
}
if(!destFile.exists()){
destFile.mkdirs();
}
for (File file : src.listFiles()) {
if(file.isDirectory()){
copyDir(file.getAbsolutePath(),dest);
}else{
String tmp=file.getAbsolutePath().replace(file.getName(),"").replace(new File("input"+File.separator+"Documents"+File.separator+"copy"+File.separator).getAbsolutePath(),"");
System.out.println(file.getAbsolutePath()+" "+tmp);
File out=new File(dest+tmp);
if(!out.exists()){
out.mkdirs();
}
copyFile(file.getAbsolutePath(),out.getAbsolutePath(),true);
}
}
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
public static JSONObject loadConfig(File file) {
@@ -138,4 +203,112 @@ public class Tools {
}
return json;
}
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure,boolean zipRootName) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
zos.setMethod(ZipOutputStream.STORED);
zos.setLevel(0);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure,zipRootName);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure,boolean zipRootName)
throws Exception {
byte[] buf = new byte[ 2 * 1024];
if (sourceFile.isFile()) {
// 向zip输出流中添加一个zip实体构造器中name为zip实体的文件的名字
ZipEntry entry=new ZipEntry(name);
entry.setMethod(STORED);
entry.setSize(sourceFile.length());
long crc=0;
crc=calFileCRC32(sourceFile);
entry.setCrc(crc);
zos.putNextEntry(entry);
// copy文件到zip输出流中
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if (KeepDirStructure) {
// 空文件夹的处理
ZipEntry entry=new ZipEntry(name + File.separator);
entry.setMethod(STORED);
zos.putNextEntry(entry);
// 没有文件不需要文件的copy
zos.closeEntry();
}
} else {
for (File file : listFiles) {
// 判断是否需要保留原来的文件结构
if (KeepDirStructure) {
// 注意file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
if(zipRootName) {
compress(file, zos, name + File.separator + file.getName(), KeepDirStructure,zipRootName);
}else{
if(name.startsWith("\\")){
compress(file, zos, name + File.separator + file.getName(), KeepDirStructure,zipRootName);
}else {
compress(file, zos, File.separator + file.getName(), KeepDirStructure, zipRootName);
}
}
} else {
compress(file, zos, file.getName(), KeepDirStructure,zipRootName);
}
}
}
}
}
public static long calFileCRC32(File file) throws IOException {
FileInputStream fi = new FileInputStream(file);
CheckedInputStream checksum = new CheckedInputStream(fi, new CRC32());
while (checksum.read() != -1) { }
long temp = checksum.getChecksum().getValue();
fi.close();
checksum.close();
return temp;
}
public static void toAJar(String dx,String inName,String outName) {
try {
File file=new File(outName);
if(file.exists()) {
file.delete();
}
String exec="java -jar "+dx+" --dex --output="+outName+" "+inName+"";
System.out.println("java to dex:"+exec);
Process process=Runtime.getRuntime().exec(exec);
process.waitFor();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}