Manually generate cd and eocd before sign

This commit is contained in:
Nullptr 2021-08-24 18:48:49 +08:00
parent b44b06f268
commit 8c0fe5646b
2 changed files with 25 additions and 16 deletions

View File

@ -258,6 +258,25 @@ public class LSPatch {
dstZFile.realign();
// create zip link
if (verbose)
System.out.println("Creating nested apk link...");
NestedZipLink nestedZipLink = new NestedZipLink(dstZFile);
StoredEntry originalZipEntry = dstZFile.get(ORIGINAL_APK_ASSET_PATH);
NestedZip nestedZip = new NestedZip(srcZFile, originalZipEntry);
for (StoredEntry entry : srcZFile.entries()) {
String name = entry.getCentralDirectoryHeader().getName();
if (name.startsWith("classes") && name.endsWith(".dex")) continue;
if (dstZFile.get(name) != null) continue;
if (name.equals("AndroidManifest.xml")) continue;
if (name.startsWith("META-INF/CERT")) continue;
if (name.equals("META-INF/MANIFEST.MF")) continue;
nestedZip.addFileLink(name);
}
nestedZipLink.nestedZips.add(nestedZip);
dstZFile.addZFileExtension(nestedZipLink);
// sign apk
System.out.println("Signing apk...");
try {
@ -278,22 +297,6 @@ public class LSPatch {
throw new PatchError("Failed to sign apk: " + e.getMessage());
}
// create zip link
if (verbose)
System.out.println("Creating nested apk link...");
NestedZipLink nestedZipLink = new NestedZipLink(dstZFile);
StoredEntry originalZipEntry = dstZFile.get(ORIGINAL_APK_ASSET_PATH);
NestedZip nestedZip = new NestedZip(srcZFile, originalZipEntry);
for (StoredEntry entry : srcZFile.entries()) {
String name = entry.getCentralDirectoryHeader().getName();
if (name.startsWith("classes") && name.endsWith(".dex")) continue;
if (name.equals("AndroidManifest.xml")) continue;
nestedZip.addFileLink(name);
}
nestedZipLink.nestedZips.add(nestedZip);
dstZFile.addZFileExtension(nestedZipLink);
dstZFile.update();
FileUtils.copyFile(tmpApk, outputFile);

View File

@ -54,6 +54,12 @@ public class NestedZipLink extends ZFileExtension {
deleteDirectoryAndEocd.setAccessible(true);
deleteDirectoryAndEocd.invoke(zFile);
appendEntries();
Method computeCentralDirectory = ZFile.class.getDeclaredMethod("computeCentralDirectory");
computeCentralDirectory.setAccessible(true);
computeCentralDirectory.invoke(zFile);
Method computeEocd = ZFile.class.getDeclaredMethod("computeEocd");
computeEocd.setAccessible(true);
computeEocd.invoke(zFile);
} catch (Exception e) {
e.printStackTrace();
var ex = new IOException("Error when writing link entries");