Skip to content

Commit

Permalink
fix bug: some app compressed res file name, the names may the same in…
Browse files Browse the repository at this point in the history
… case insensitive mode, so fix it
  • Loading branch information
WindySha committed Sep 17, 2021
1 parent 25e0bb6 commit 773674b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public void run() {
if (keyStoreFile.exists()) {
keyStoreFile.delete();
}
System.out.println(" out put apk :" + signedApkPath);
}

private boolean signApk(String apkPath, String keyStorePath, String signedApkPath) {
String apkParentPath = (new File(apkPath)).getParent();

System.out.println(" apkParentPath :" + apkParentPath);
ShellCmdUtil.chmodNoException(apkParentPath, ShellCmdUtil.FileMode.MODE_755);
if (signApkUsingAndroidApksigner(apkPath, keyStorePath, signedApkPath, "123456")) {
return true;
Expand Down
52 changes: 50 additions & 2 deletions xpatch/src/main/java/com/storm/wind/xpatch/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
Expand All @@ -42,6 +43,12 @@ public class FileUtils {
".amr", ".awb", ".wma", ".wmv",
".tflite", ".lite"
};
private static final String APPEND_PREFIX_FORMAT = "#$&(";
private static final String APPEND_SUBFIX_FORMAT = ")&$#";

private static final HashSet<String> ResFileNameSet = new HashSet<>();
private static final String RES_PATH_CONST = "res" + File.separator;


/**
* 解压文件
Expand All @@ -52,6 +59,7 @@ public class FileUtils {
*/
@SuppressWarnings("rawtypes")
public static boolean decompressZip(String zipPath, String descDir) {
ResFileNameSet.clear();
File zipFile = new File(zipPath);
boolean flag = false;
if (!descDir.endsWith(File.separator)) {
Expand Down Expand Up @@ -88,6 +96,17 @@ public static boolean decompressZip(String zipPath, String descDir) {
if (new File(outPath).isDirectory()) {
continue;
}

// 处理res/aaa.xml资源名称忽略大小写出现重复的问题, 比如 youtube app, 漫画人app等等
if (zipEntryName.startsWith(RES_PATH_CONST) && zipEntryName.split(File.separator).length == 2) {
String fileName = zipEntryName.split(File.separator)[1];
fileName = getNotContainedFileName(fileName, ResFileNameSet);
ResFileNameSet.add(fileName.toLowerCase());

zipEntryName = RES_PATH_CONST + fileName;
outPath = (descDir + zipEntryName).replace("/", File.separator);
}

//保存文件路径信息(可利用md5.zip名称的唯一性,来判断是否已经解压)
// System.err.println("当前zip解压之后的路径为:" + outPath);
OutputStream out = new FileOutputStream(outPath);
Expand All @@ -107,6 +126,33 @@ public static boolean decompressZip(String zipPath, String descDir) {
return flag;
}

private static String getNotContainedFileName(String fileName, HashSet<String> set) {
StringBuilder name = new StringBuilder(fileName);
if (set.contains(name.toString().toLowerCase())) {
for (int i = 1; i < 100; i++) {
name.insert(0, APPEND_SUBFIX_FORMAT);
name.insert(0, i);
name.insert(0, APPEND_PREFIX_FORMAT);
if (!set.contains(name.toString().toLowerCase())) {
return name.toString();
}
}
} else {
return name.toString();
}
return name.toString();
}

private static String removeFileNamePrefix(String fileName) {
String lastPrefix = APPEND_PREFIX_FORMAT + "1" + APPEND_SUBFIX_FORMAT;
int index = fileName.lastIndexOf(lastPrefix);
if (index >= 0) {
return fileName.substring(index + lastPrefix.length());
} else {
return fileName;
}
}

private static InputStream getInputStreamFromFile(String filePath) {
return FileUtils.class.getClassLoader().getResourceAsStream(filePath);
}
Expand Down Expand Up @@ -248,8 +294,11 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
String fileName = file.getName();
if (baseDir.equals(RES_PATH_CONST)) { // 处理res/目录下,文件名称忽略大小写出现重复的问题
fileName = removeFileNamePrefix(fileName);
}
ZipEntry entry = new ZipEntry(baseDir + fileName);
boolean isNoCompressFileFormat = false;
int index = fileName.lastIndexOf(".");
if (index >= 0) {
Expand Down Expand Up @@ -357,5 +406,4 @@ private static void close(Closeable closeable) {
io.printStackTrace();
}
}

}

0 comments on commit 773674b

Please sign in to comment.