2019-01-30 09:10:12 +01:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2016-11-20 18:33:12 +01:00
|
|
|
|
2019-01-30 09:10:12 +01:00
|
|
|
import com.topjohnwu.signing.JarMap;
|
|
|
|
import com.topjohnwu.signing.SignAPK;
|
2018-02-12 16:07:35 +01:00
|
|
|
import com.topjohnwu.superuser.ShellUtils;
|
2018-05-13 12:14:10 +02:00
|
|
|
import com.topjohnwu.superuser.io.SuFile;
|
|
|
|
import com.topjohnwu.superuser.io.SuFileOutputStream;
|
2016-11-22 06:45:26 +01:00
|
|
|
|
2017-09-03 16:10:54 +02:00
|
|
|
import java.io.BufferedInputStream;
|
2018-01-22 22:04:59 +01:00
|
|
|
import java.io.BufferedOutputStream;
|
2016-11-22 06:45:26 +01:00
|
|
|
import java.io.File;
|
2017-06-15 21:07:46 +02:00
|
|
|
import java.io.FileInputStream;
|
2016-11-29 06:24:48 +01:00
|
|
|
import java.io.FileOutputStream;
|
2018-05-13 12:14:10 +02:00
|
|
|
import java.io.IOException;
|
2016-11-20 18:33:12 +01:00
|
|
|
import java.io.InputStream;
|
2018-01-22 22:04:59 +01:00
|
|
|
import java.io.OutputStream;
|
2018-01-01 17:25:08 +01:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
2016-11-20 18:33:12 +01:00
|
|
|
|
|
|
|
public class ZipUtils {
|
2017-06-15 21:07:46 +02:00
|
|
|
|
2018-05-13 12:14:10 +02:00
|
|
|
public static void unzip(File zip, File folder, String path, boolean junkPath) throws IOException {
|
2017-09-03 16:10:54 +02:00
|
|
|
InputStream in = new BufferedInputStream(new FileInputStream(zip));
|
2017-08-30 21:07:33 +02:00
|
|
|
unzip(in, folder, path, junkPath);
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2018-05-13 12:14:10 +02:00
|
|
|
public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws IOException {
|
2017-08-30 21:07:33 +02:00
|
|
|
try {
|
2018-01-01 17:25:08 +01:00
|
|
|
ZipInputStream zipfile = new ZipInputStream(zip);
|
|
|
|
ZipEntry entry;
|
|
|
|
while ((entry = zipfile.getNextEntry()) != null) {
|
2017-08-30 21:07:33 +02:00
|
|
|
if (!entry.getName().startsWith(path) || entry.isDirectory()){
|
2016-12-08 16:03:50 +01:00
|
|
|
// Ignore directories, only create files
|
2016-11-29 06:24:48 +01:00
|
|
|
continue;
|
2017-02-20 20:30:37 +01:00
|
|
|
}
|
2017-08-30 21:07:33 +02:00
|
|
|
String name;
|
|
|
|
if (junkPath) {
|
|
|
|
name = entry.getName().substring(entry.getName().lastIndexOf('/') + 1);
|
|
|
|
} else {
|
|
|
|
name = entry.getName();
|
2017-02-20 20:30:37 +01:00
|
|
|
}
|
2018-06-25 13:46:41 +02:00
|
|
|
File dest = new File(folder, name);
|
2018-06-26 00:04:11 +02:00
|
|
|
if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
|
2018-06-25 13:46:41 +02:00
|
|
|
dest = new SuFile(folder, name);
|
|
|
|
dest.getParentFile().mkdirs();
|
|
|
|
}
|
2018-05-13 12:14:10 +02:00
|
|
|
try (OutputStream out = new SuFileOutputStream(dest)) {
|
2018-02-12 16:07:35 +01:00
|
|
|
ShellUtils.pump(zipfile, out);
|
2017-02-20 20:30:37 +01:00
|
|
|
}
|
2016-11-29 06:24:48 +01:00
|
|
|
}
|
2018-05-13 12:14:10 +02:00
|
|
|
}
|
|
|
|
catch(IOException e) {
|
2016-11-29 06:24:48 +01:00
|
|
|
e.printStackTrace();
|
2017-02-14 22:24:02 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 01:23:02 +01:00
|
|
|
public static void signZip(File input, File output) throws Exception {
|
2018-03-10 22:28:47 +01:00
|
|
|
try (JarMap map = new JarMap(input, false);
|
|
|
|
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
|
2018-08-04 20:29:40 +02:00
|
|
|
SignAPK.sign(map, out);
|
2018-01-22 22:04:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 17:17:43 +01:00
|
|
|
}
|