2016-11-21 01:33:12 +08:00
|
|
|
package com.topjohnwu.magisk.utils;
|
|
|
|
|
2018-02-12 23:07:35 +08:00
|
|
|
import com.topjohnwu.superuser.ShellUtils;
|
2018-05-13 18:14:10 +08:00
|
|
|
import com.topjohnwu.superuser.io.SuFile;
|
|
|
|
import com.topjohnwu.superuser.io.SuFileOutputStream;
|
2018-01-27 08:34:12 +08:00
|
|
|
import com.topjohnwu.utils.JarMap;
|
|
|
|
import com.topjohnwu.utils.SignAPK;
|
2016-11-22 13:45:26 +08:00
|
|
|
|
2017-09-03 22:10:54 +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;
|
2018-05-13 18:14:10 +08:00
|
|
|
import java.io.IOException;
|
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
|
|
|
|
2018-05-13 18:14:10 +08:00
|
|
|
public static void unzip(File zip, File folder, String path, boolean junkPath) throws IOException {
|
2017-09-03 22:10:54 +08:00
|
|
|
InputStream in = new BufferedInputStream(new FileInputStream(zip));
|
2017-08-31 03:07:33 +08:00
|
|
|
unzip(in, folder, path, junkPath);
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:14:10 +08:00
|
|
|
public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws IOException {
|
2017-08-31 03:07:33 +08:00
|
|
|
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-02-21 03:30:37 +08:00
|
|
|
}
|
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-02-21 03:30:37 +08:00
|
|
|
}
|
2018-06-25 19:46:41 +08:00
|
|
|
File dest = new File(folder, name);
|
2018-06-26 06:04:11 +08:00
|
|
|
if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
|
2018-06-25 19:46:41 +08:00
|
|
|
dest = new SuFile(folder, name);
|
|
|
|
dest.getParentFile().mkdirs();
|
|
|
|
}
|
2018-05-13 18:14:10 +08:00
|
|
|
try (OutputStream out = new SuFileOutputStream(dest)) {
|
2018-02-12 23:07:35 +08:00
|
|
|
ShellUtils.pump(zipfile, out);
|
2017-02-21 03:30:37 +08:00
|
|
|
}
|
2016-11-29 13:24:48 +08:00
|
|
|
}
|
2018-05-13 18:14:10 +08:00
|
|
|
}
|
|
|
|
catch(IOException e) {
|
2016-11-29 13:24:48 +08:00
|
|
|
e.printStackTrace();
|
2017-02-15 05:24:02 +08:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 08:23:02 +08:00
|
|
|
public static void signZip(File input, File output) throws Exception {
|
2018-03-11 05:28:47 +08:00
|
|
|
try (JarMap map = new JarMap(input, false);
|
|
|
|
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
|
|
|
|
signZip(map, out);
|
2018-01-23 05:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 08:23:02 +08:00
|
|
|
public static void signZip(JarMap input, OutputStream output) throws Exception {
|
|
|
|
SignAPK.signZip(null, null, input, output);
|
2016-11-22 13:45:26 +08:00
|
|
|
}
|
2018-01-27 00:17:43 +08:00
|
|
|
}
|