80 lines
2.8 KiB
Java
Raw Normal View History

2016-11-21 01:33:12 +08:00
package com.topjohnwu.magisk.utils;
import android.content.res.AssetManager;
2017-10-04 22:09:59 +08:00
import com.topjohnwu.jarsigner.JarMap;
import com.topjohnwu.jarsigner.SignAPK;
2017-10-21 22:54:47 +08:00
import com.topjohnwu.magisk.MagiskManager;
2016-11-22 13:45:26 +08:00
import java.io.BufferedInputStream;
2016-11-22 13:45:26 +08:00
import java.io.File;
2017-06-16 03:07:46 +08:00
import java.io.FileInputStream;
2016-11-29 13:24:48 +08:00
import java.io.FileOutputStream;
2016-11-21 01:33:12 +08:00
import java.io.InputStream;
2016-11-22 13:45:26 +08:00
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
2016-11-21 01:33:12 +08:00
public class ZipUtils {
2017-06-16 03:07:46 +08:00
// File name in assets
2016-11-22 13:45:26 +08:00
private static final String PUBLIC_KEY_NAME = "public.certificate.x509.pem";
private static final String PRIVATE_KEY_NAME = "private.key.pk8";
2017-06-16 03:07:46 +08:00
2016-11-23 13:46:52 +08:00
static {
2017-06-16 03:07:46 +08:00
System.loadLibrary("zipadjust");
2016-11-23 13:46:52 +08:00
}
2017-06-16 03:07:46 +08:00
public native static void zipAdjust(String filenameIn, String filenameOut);
2017-08-31 03:07:33 +08:00
public static void unzip(File zip, File folder, String path, boolean junkPath) throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(zip));
2017-08-31 03:07:33 +08:00
unzip(in, folder, path, junkPath);
in.close();
}
public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws Exception {
2017-01-12 02:02:52 +01:00
byte data[] = new byte[4096];
2017-08-31 03:07:33 +08:00
try {
JarInputStream zipfile = new JarInputStream(zip);
JarEntry entry;
while ((entry = zipfile.getNextJarEntry()) != null) {
if (!entry.getName().startsWith(path) || entry.isDirectory()){
2016-12-08 23:03:50 +08:00
// Ignore directories, only create files
2016-11-29 13:24:48 +08:00
continue;
}
2017-08-31 03:07:33 +08:00
String name;
if (junkPath) {
name = entry.getName().substring(entry.getName().lastIndexOf('/') + 1);
} else {
name = entry.getName();
}
2017-08-31 03:07:33 +08:00
File dest = new File(folder, name);
dest.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(dest);
int count;
while ((count = zipfile.read(data)) != -1) {
2016-11-29 13:24:48 +08:00
out.write(data, 0, count);
}
2016-11-29 13:24:48 +08:00
out.flush();
out.close();
}
} catch(Exception e) {
e.printStackTrace();
2017-02-15 05:24:02 +08:00
throw e;
}
}
2017-10-21 22:54:47 +08:00
public static void signZip(InputStream is, File output, boolean minSign) throws Exception {
signZip(new JarMap(is, false), output, minSign);
2017-10-04 22:09:59 +08:00
}
2017-10-21 22:54:47 +08:00
public static void signZip(File input, File output, boolean minSign) throws Exception {
signZip(new JarMap(input, false), output, minSign);
2017-10-04 22:09:59 +08:00
}
2017-10-21 22:54:47 +08:00
public static void signZip(JarMap input, File output, boolean minSign) throws Exception {
AssetManager assets = MagiskManager.get().getAssets();
SignAPK.signZip(
assets.open(PUBLIC_KEY_NAME), assets.open(PRIVATE_KEY_NAME),
input, output, minSign);
2016-11-22 13:45:26 +08:00
}
2017-06-16 03:07:46 +08:00
}