84 lines
2.9 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
2017-10-30 03:45:22 +08:00
import com.topjohnwu.crypto.JarMap;
import com.topjohnwu.crypto.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;
2018-01-23 05:04:59 +08:00
import java.io.BufferedOutputStream;
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;
2018-01-23 05:04:59 +08:00
import java.io.OutputStream;
2018-01-02 00:25:08 +08:00
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
2016-11-21 01:33:12 +08:00
public class ZipUtils {
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 {
try {
2018-01-02 00:25:08 +08:00
ZipInputStream zipfile = new ZipInputStream(zip);
ZipEntry entry;
while ((entry = zipfile.getNextEntry()) != null) {
2017-08-31 03:07:33 +08:00
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();
2017-10-31 16:31:58 +08:00
try (FileOutputStream out = new FileOutputStream(dest)) {
Utils.inToOut(zipfile, out);
}
2016-11-29 13:24:48 +08:00
}
} 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 {
2018-01-23 05:04:59 +08:00
try (JarMap map = new JarMap(is, false)) {
signZip(map, 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 {
2018-01-23 05:04:59 +08:00
try (JarMap map = new JarMap(input, false)) {
signZip(map, 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 {
2018-01-23 05:04:59 +08:00
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
signZip(input, out, minSign);
}
}
public static void signZip(JarMap input, OutputStream output, boolean minSign) throws Exception {
2017-10-21 22:54:47 +08:00
AssetManager assets = MagiskManager.get().getAssets();
SignAPK.signZip(
2017-11-06 04:41:23 +08:00
assets.open(Const.PUBLIC_KEY_NAME), assets.open(Const.PRIVATE_KEY_NAME),
input, output, minSign);
2016-11-22 13:45:26 +08:00
}
2017-06-16 03:07:46 +08:00
}