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

@ -6,9 +6,3 @@ I use Java 8 features, which requires Jack compiler and that's only available 2.
### libbusybox.so ### libbusybox.so
Static BusyBox binary by @yashdsaraf Static BusyBox binary by @yashdsaraf
Link and source: http://forum.xda-developers.com/showthread.php?t=3348543 Link and source: http://forum.xda-developers.com/showthread.php?t=3348543
### libzip.so
Static ndk-built info-zip
NDK makefiles: https://github.com/cloudchou/ndkzip
Info-Zip source: https://sourceforge.net/projects/infozip/
bzip2 source: http://www.bzip.org/

View File

@ -23,11 +23,13 @@ import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.Repo; import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Async; import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebWindow; import com.topjohnwu.magisk.utils.WebWindow;
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
@ -102,30 +104,15 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
new DownloadReceiver() { new DownloadReceiver() {
@Override @Override
public void task(Uri uri) { public void task(Uri uri) {
new Async.FlashZIP(context, uri, mFilename) { try {
/* ByteArrayOutputStream buffer = new ByteArrayOutputStream();
* !!! This method is now depreciated, will be replaced with new method !!! InputStream in = mContext.getContentResolver().openInputStream(uri);
*/ ZipUtils.removeTopFolder(in, buffer);
@Override buffer.writeTo(mContext.getContentResolver().openOutputStream(uri));
protected void preProcessing() throws Throwable { } catch (IOException e) {
File file = new File(mUri.getPath()); return;
Shell.sh(
"PATH=" + context.getApplicationInfo().dataDir + "/tools:$PATH",
"cd " + file.getParent(),
"mkdir git",
"unzip -o " + file + " -d git",
"mv git/* install",
"cd install",
"rm -rf system/placeholder",
"chmod 644 $(find . -type f)",
"chmod 755 $(find . -type d)",
"rm -rf ../install.zip ../git",
"zip -r ../install.zip *",
"rm -rf ../install"
);
mUri = Uri.fromFile(new File(file.getParent() + "/install.zip"));
} }
}.exec(); new Async.FlashZIP(mContext, uri, mFilename).exec();
} }
}, },
repo.getZipUrl(), repo.getZipUrl(),

View File

@ -40,6 +40,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver {
} }
context.unregisterReceiver(this); context.unregisterReceiver(this);
} }
c.close();
} }
} }

View File

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

Binary file not shown.