Rewrite ZipUtils in Kotlin
This commit is contained in:
parent
d3f5f5ee59
commit
8b7144c986
@ -5,7 +5,7 @@ import android.net.Uri;
|
|||||||
import com.topjohnwu.magisk.App;
|
import com.topjohnwu.magisk.App;
|
||||||
import com.topjohnwu.magisk.Const;
|
import com.topjohnwu.magisk.Const;
|
||||||
import com.topjohnwu.magisk.utils.Utils;
|
import com.topjohnwu.magisk.utils.Utils;
|
||||||
import com.topjohnwu.magisk.utils.ZipUtils;
|
import com.topjohnwu.magisk.utils.ZipUtilsKt;
|
||||||
import com.topjohnwu.superuser.Shell;
|
import com.topjohnwu.superuser.Shell;
|
||||||
import com.topjohnwu.superuser.ShellUtils;
|
import com.topjohnwu.superuser.ShellUtils;
|
||||||
import com.topjohnwu.superuser.internal.UiThreadHandler;
|
import com.topjohnwu.superuser.internal.UiThreadHandler;
|
||||||
@ -34,7 +34,7 @@ public abstract class FlashZip {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean unzipAndCheck() throws IOException {
|
private boolean unzipAndCheck() throws IOException {
|
||||||
ZipUtils.unzip(tmpFile, tmpFile.getParentFile(), "META-INF/com/google/android", true);
|
ZipUtilsKt.unzip(tmpFile, tmpFile.getParentFile(), "META-INF/com/google/android", true);
|
||||||
return Shell.su("grep -q '#MAGISK' " + new File(tmpFile.getParentFile(), "updater-script"))
|
return Shell.su("grep -q '#MAGISK' " + new File(tmpFile.getParentFile(), "updater-script"))
|
||||||
.exec().isSuccess();
|
.exec().isSuccess();
|
||||||
}
|
}
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
package com.topjohnwu.magisk.utils;
|
|
||||||
|
|
||||||
import com.topjohnwu.signing.JarMap;
|
|
||||||
import com.topjohnwu.signing.SignAPK;
|
|
||||||
import com.topjohnwu.superuser.ShellUtils;
|
|
||||||
import com.topjohnwu.superuser.io.SuFile;
|
|
||||||
import com.topjohnwu.superuser.io.SuFileOutputStream;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
public class ZipUtils {
|
|
||||||
|
|
||||||
public static void unzip(File zip, File folder, String path, boolean junkPath) throws IOException {
|
|
||||||
InputStream in = new BufferedInputStream(new FileInputStream(zip));
|
|
||||||
unzip(in, folder, path, junkPath);
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws IOException {
|
|
||||||
try {
|
|
||||||
ZipInputStream zipfile = new ZipInputStream(zip);
|
|
||||||
ZipEntry entry;
|
|
||||||
while ((entry = zipfile.getNextEntry()) != null) {
|
|
||||||
if (!entry.getName().startsWith(path) || entry.isDirectory()){
|
|
||||||
// Ignore directories, only create files
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String name;
|
|
||||||
if (junkPath) {
|
|
||||||
name = entry.getName().substring(entry.getName().lastIndexOf('/') + 1);
|
|
||||||
} else {
|
|
||||||
name = entry.getName();
|
|
||||||
}
|
|
||||||
File dest = new File(folder, name);
|
|
||||||
if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
|
|
||||||
dest = new SuFile(folder, name);
|
|
||||||
dest.getParentFile().mkdirs();
|
|
||||||
}
|
|
||||||
try (OutputStream out = new SuFileOutputStream(dest)) {
|
|
||||||
ShellUtils.pump(zipfile, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void signZip(File input, File output) throws Exception {
|
|
||||||
try (JarMap map = new JarMap(input, false);
|
|
||||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
|
|
||||||
SignAPK.sign(map, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
46
app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.kt
Normal file
46
app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.kt
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package com.topjohnwu.magisk.utils
|
||||||
|
|
||||||
|
import com.topjohnwu.superuser.io.SuFile
|
||||||
|
import com.topjohnwu.superuser.io.SuFileOutputStream
|
||||||
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.util.zip.ZipEntry
|
||||||
|
import java.util.zip.ZipInputStream
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
@JvmOverloads
|
||||||
|
fun unzip(zip: File, folder: File, path: String = "", junkPath: Boolean = false) {
|
||||||
|
zip.inputStream().buffered().use {
|
||||||
|
unzip(it, folder, path, junkPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun unzip(zip: InputStream, folder: File, path: String, junkPath: Boolean) {
|
||||||
|
try {
|
||||||
|
val zin = ZipInputStream(zip)
|
||||||
|
var entry: ZipEntry
|
||||||
|
while (true) {
|
||||||
|
entry = zin.nextEntry ?: break
|
||||||
|
if (!entry.name.startsWith(path) || entry.isDirectory) {
|
||||||
|
// Ignore directories, only create files
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val name = if (junkPath)
|
||||||
|
entry.name.substring(entry.name.lastIndexOf('/') + 1)
|
||||||
|
else
|
||||||
|
entry.name
|
||||||
|
|
||||||
|
var dest = File(folder, name)
|
||||||
|
if (!dest.parentFile!!.exists() && !dest.parentFile!!.mkdirs()) {
|
||||||
|
dest = SuFile(folder, name)
|
||||||
|
dest.parentFile!!.mkdirs()
|
||||||
|
}
|
||||||
|
SuFileOutputStream(dest).use { out -> zin.copyTo(out) }
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user