Process zip with Java

This commit is contained in:
topjohnwu
2016-11-21 01:33:12 +08:00
parent f5bee7b691
commit 2a8477cbda
7 changed files with 55 additions and 35 deletions

View File

@@ -60,7 +60,6 @@ public class Async {
protected Void doInBackground(Void... voids) {
String toolPath = mInfo.dataDir + "/tools";
String busybox = mInfo.dataDir + "/lib/libbusybox.so";
String zip = mInfo.dataDir + "/lib/libzip.so";
if (!Utils.itemExist(false, toolPath)) {
Shell.sh(
"mkdir " + toolPath,
@@ -70,8 +69,7 @@ public class Async {
"for tool in $(./busybox --list); do",
"ln -s " + busybox + " $tool",
"done",
"rm -f su sh",
"ln -s " + zip + " zip"
"rm -f su sh"
);
}
return null;

View File

@@ -0,0 +1,40 @@
package com.topjohnwu.magisk.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static void removeTopFolder(InputStream in, OutputStream out) {
try {
ZipInputStream source = new ZipInputStream(in);
ZipOutputStream dest = new ZipOutputStream(out);
ZipEntry entry;
String path;
int size;
byte buffer[] = new byte[2048];
while ((entry = source.getNextEntry()) != null) {
// Remove the top directory from the path
path = entry.toString().substring(entry.toString().indexOf("/") + 1);
// If it's the top folder, ignore it
if (path.isEmpty())
continue;
// Don't include placeholder
if (path.contains("system/placeholder"))
continue;
dest.putNextEntry(new ZipEntry(path));
while((size = source.read(buffer, 0, 2048)) != -1)
dest.write(buffer, 0, size);
}
source.close();
dest.close();
} catch (IOException e) {
e.printStackTrace();
Logger.dev("ZipUtils: removeTopFolder IO error!");
}
}
}