Skip to content

Commit 773674b

Browse files
author
WindySha
committed
fix bug: some app compressed res file name, the names may the same in case insensitive mode, so fix it
1 parent 25e0bb6 commit 773674b

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

xpatch/src/main/java/com/storm/wind/xpatch/task/BuildAndSignApkTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ public void run() {
8787
if (keyStoreFile.exists()) {
8888
keyStoreFile.delete();
8989
}
90+
System.out.println(" out put apk :" + signedApkPath);
9091
}
9192

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

95-
System.out.println(" apkParentPath :" + apkParentPath);
9696
ShellCmdUtil.chmodNoException(apkParentPath, ShellCmdUtil.FileMode.MODE_755);
9797
if (signApkUsingAndroidApksigner(apkPath, keyStorePath, signedApkPath, "123456")) {
9898
return true;

xpatch/src/main/java/com/storm/wind/xpatch/util/FileUtils.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.nio.charset.Charset;
1818
import java.util.ArrayList;
1919
import java.util.Enumeration;
20+
import java.util.HashSet;
2021
import java.util.zip.CRC32;
2122
import java.util.zip.CheckedInputStream;
2223
import java.util.zip.CheckedOutputStream;
@@ -42,6 +43,12 @@ public class FileUtils {
4243
".amr", ".awb", ".wma", ".wmv",
4344
".tflite", ".lite"
4445
};
46+
private static final String APPEND_PREFIX_FORMAT = "#$&(";
47+
private static final String APPEND_SUBFIX_FORMAT = ")&$#";
48+
49+
private static final HashSet<String> ResFileNameSet = new HashSet<>();
50+
private static final String RES_PATH_CONST = "res" + File.separator;
51+
4552

4653
/**
4754
* 解压文件
@@ -52,6 +59,7 @@ public class FileUtils {
5259
*/
5360
@SuppressWarnings("rawtypes")
5461
public static boolean decompressZip(String zipPath, String descDir) {
62+
ResFileNameSet.clear();
5563
File zipFile = new File(zipPath);
5664
boolean flag = false;
5765
if (!descDir.endsWith(File.separator)) {
@@ -88,6 +96,17 @@ public static boolean decompressZip(String zipPath, String descDir) {
8896
if (new File(outPath).isDirectory()) {
8997
continue;
9098
}
99+
100+
// 处理res/aaa.xml资源名称忽略大小写出现重复的问题, 比如 youtube app, 漫画人app等等
101+
if (zipEntryName.startsWith(RES_PATH_CONST) && zipEntryName.split(File.separator).length == 2) {
102+
String fileName = zipEntryName.split(File.separator)[1];
103+
fileName = getNotContainedFileName(fileName, ResFileNameSet);
104+
ResFileNameSet.add(fileName.toLowerCase());
105+
106+
zipEntryName = RES_PATH_CONST + fileName;
107+
outPath = (descDir + zipEntryName).replace("/", File.separator);
108+
}
109+
91110
//保存文件路径信息(可利用md5.zip名称的唯一性,来判断是否已经解压)
92111
// System.err.println("当前zip解压之后的路径为:" + outPath);
93112
OutputStream out = new FileOutputStream(outPath);
@@ -107,6 +126,33 @@ public static boolean decompressZip(String zipPath, String descDir) {
107126
return flag;
108127
}
109128

129+
private static String getNotContainedFileName(String fileName, HashSet<String> set) {
130+
StringBuilder name = new StringBuilder(fileName);
131+
if (set.contains(name.toString().toLowerCase())) {
132+
for (int i = 1; i < 100; i++) {
133+
name.insert(0, APPEND_SUBFIX_FORMAT);
134+
name.insert(0, i);
135+
name.insert(0, APPEND_PREFIX_FORMAT);
136+
if (!set.contains(name.toString().toLowerCase())) {
137+
return name.toString();
138+
}
139+
}
140+
} else {
141+
return name.toString();
142+
}
143+
return name.toString();
144+
}
145+
146+
private static String removeFileNamePrefix(String fileName) {
147+
String lastPrefix = APPEND_PREFIX_FORMAT + "1" + APPEND_SUBFIX_FORMAT;
148+
int index = fileName.lastIndexOf(lastPrefix);
149+
if (index >= 0) {
150+
return fileName.substring(index + lastPrefix.length());
151+
} else {
152+
return fileName;
153+
}
154+
}
155+
110156
private static InputStream getInputStreamFromFile(String filePath) {
111157
return FileUtils.class.getClassLoader().getResourceAsStream(filePath);
112158
}
@@ -248,8 +294,11 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
248294
BufferedInputStream bis = null;
249295
try {
250296
bis = new BufferedInputStream(new FileInputStream(file));
251-
ZipEntry entry = new ZipEntry(baseDir + file.getName());
252297
String fileName = file.getName();
298+
if (baseDir.equals(RES_PATH_CONST)) { // 处理res/目录下,文件名称忽略大小写出现重复的问题
299+
fileName = removeFileNamePrefix(fileName);
300+
}
301+
ZipEntry entry = new ZipEntry(baseDir + fileName);
253302
boolean isNoCompressFileFormat = false;
254303
int index = fileName.lastIndexOf(".");
255304
if (index >= 0) {
@@ -357,5 +406,4 @@ private static void close(Closeable closeable) {
357406
io.printStackTrace();
358407
}
359408
}
360-
361409
}

0 commit comments

Comments
 (0)