Skip to content

Commit

Permalink
Fix processing some xapk files
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdurazaaqMohammed committed Jun 8, 2024
1 parent 231a062 commit 82007a7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void process(Uri outputUri) {
final File cacheDir = getExternalCacheDir();
deleteDir(cacheDir);
try {
Merger.run(getContentResolver().openInputStream(splitAPKUri), cacheDir, getContentResolver().openOutputStream(outputUri));
Merger.run(getContentResolver().openInputStream(splitAPKUri), cacheDir, getContentResolver().openOutputStream(outputUri), splitAPKUri.getPath().endsWith("xapk"));
} catch (IOException e) {
showError(e.toString());
}
Expand Down
86 changes: 62 additions & 24 deletions app/src/main/java/com/reandroid/apkeditor/merge/Merger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.reandroid.apkeditor.merge;

import android.os.Environment;

import com.reandroid.apk.ApkBundle;
import com.reandroid.apk.ApkModule;
import com.reandroid.apkeditor.common.AndroidManifestHelper;
Expand All @@ -37,41 +35,80 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Merger {

private static File newFile(File outputDir, ZipEntry zipEntry) throws IOException {
File file = new File(outputDir, zipEntry.getName());
private static File newFile(File outputDir, String name) throws IOException {
File file = new File(outputDir, name);
String canonicalizedPath = file.getCanonicalPath();
if (!canonicalizedPath.startsWith(outputDir.getCanonicalPath() + File.separator)) {
throw new IOException("Zip entry is outside of the target dir: " + zipEntry.getName());
throw new IOException("Zip entry is outside of the target dir: " + name);
}
return file;
}
private static void extractZip(InputStream zi, File outputDir) throws IOException {
private static void extractZip(InputStream zi, File outputDir, boolean isXAPK) throws IOException {
byte[] buffer = new byte[1024];

try (ZipInputStream zis = new ZipInputStream(zi)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
final String name = zipEntry.getName();
if(name.endsWith(".apk")) {
FileOutputStream fos = new FileOutputStream(newFile(outputDir, zipEntry));
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
LogUtil.logMessage("Extracted " + name);
} else LogUtil.logMessage("Skipping " + name + ": Not an APK file");
zipEntry = zis.getNextEntry();
if(isXAPK) {
final File bruh = new File(outputDir + File.separator + "bruh.zip");
OutputStream os = new FileOutputStream(bruh);
int length;
while ((length = zi.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
zis.closeEntry();
}
zi.close();
os.close();
try (ZipFile zipFile = new ZipFile(bruh)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();

while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String fileName = entry.getName();

if (fileName.endsWith(".apk")) {
File outFile = new File(outputDir, fileName);
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}

try (InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(outFile)) {
byte[] buffy = new byte[1024];
int len;
while ((len = is.read(buffy)) > 0) {
fos.write(buffy, 0, len);
}
}
} else LogUtil.logMessage("Skipping " + fileName + ": Not an APK file");
}
}
bruh.delete();
}
else {
try (ZipInputStream zis = new ZipInputStream(zi)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
final String name = zipEntry.getName();
if(name.endsWith(".apk")) {
FileOutputStream fos = new FileOutputStream(newFile(outputDir, name));
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
LogUtil.logMessage("Extracted " + name);
} else LogUtil.logMessage("Skipping " + name + ": Not an APK file");
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
}
}
}
private static void sanitizeManifest(ApkModule apkModule) {
if(!apkModule.hasAndroidManifest()){
Expand Down Expand Up @@ -156,9 +193,10 @@ public interface LogListener {
void onLog(String log);
}

public static void run(InputStream ins, File cacheDir, OutputStream out) throws IOException {
public static void run(InputStream ins, File cacheDir, OutputStream out, boolean isXAPK) throws IOException {
LogUtil.logMessage("Searching apk files ...");
extractZip(ins, cacheDir);

extractZip(ins, cacheDir, isXAPK);
ApkBundle bundle=new ApkBundle();
bundle.loadApkDirectory(cacheDir, false);
LogUtil.logMessage("Found modules: "+bundle.getApkModuleList().size());
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/starry/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ of this software and associated documentation files (the "Software"), to deal
https://github.com/starry-shivam/FileUtils/blob/main/file-utils/src/main/java/com/starry/file_utils/FileUtils.kt
*/

private static final String TAG = "FileUtils";
private static String FALLBACK_COPY_FOLDER = "upload_part";
private Context context;

Expand Down

0 comments on commit 82007a7

Please sign in to comment.