From 334beebfebeaf844ee76afc6e34af8d8d247a946 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 19 Nov 2017 06:17:31 +0800 Subject: [PATCH] Not all devices work well with streaming --- .../magisk/asyncs/ProcessRepoZip.java | 78 +++++++++---------- .../com/topjohnwu/magisk/utils/Utils.java | 6 +- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index 3997b996d..2fdf344e9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -49,28 +49,28 @@ public class ProcessRepoZip extends ParallelTask { mHandler = new Handler(); } - private void removeTopFolder(InputStream in, File output) throws IOException { - JarInputStream source = new JarInputStream(in); - JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(output))); + private void removeTopFolder(File input, File output) throws IOException { JarEntry entry; - String path; - while ((entry = source.getNextJarEntry()) != null) { - // Remove the top directory from the path - path = entry.getName().substring(entry.getName().indexOf("/") + 1); - // If it's the top folder, ignore it - if (path.isEmpty()) { - continue; + try ( + JarInputStream in = new JarInputStream(new BufferedInputStream(new FileInputStream(input))); + JarOutputStream out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(output))) + ) { + String path; + while ((entry = in.getNextJarEntry()) != null) { + // Remove the top directory from the path + path = entry.getName().substring(entry.getName().indexOf("/") + 1); + // If it's the top folder, ignore it + if (path.isEmpty()) { + continue; + } + // Don't include placeholder + if (path.equals("system/placeholder")) { + continue; + } + out.putNextEntry(new JarEntry(path)); + Utils.inToOut(in, out); } - // Don't include placeholder - if (path.equals("system/placeholder")) { - continue; - } - dest.putNextEntry(new JarEntry(path)); - Utils.inToOut(source, dest); } - source.close(); - dest.close(); - in.close(); } @Override @@ -97,41 +97,37 @@ public class ProcessRepoZip extends ParallelTask { break; } while (true); - InputStream in = new BufferedInputStream(new ProgressInputStream(conn.getInputStream())); - // Temp files File temp1 = new File(activity.getCacheDir(), "1.zip"); File temp2 = new File(temp1.getParentFile(), "2.zip"); temp1.getParentFile().mkdir(); - // First remove top folder in Github source zip, Web -> temp1 - removeTopFolder(in, temp1); - + // First download the zip, Web -> temp1 + try ( + InputStream in = new BufferedInputStream(new ProgressInputStream(conn.getInputStream())); + OutputStream out = new BufferedOutputStream(new FileOutputStream(temp1)) + ) { + Utils.inToOut(in, out); + in.close(); + } conn.disconnect(); + mHandler.post(() -> { progressDialog.setTitle(R.string.zip_process_title); progressDialog.setMessage(getActivity().getString(R.string.zip_process_msg)); }); - // Then sign the zip for the first time, temp1 -> temp2 - ZipUtils.signZip(temp1, temp2, false); + // First remove top folder in Github source zip, temp1 -> temp2 + removeTopFolder(temp1, temp2); - // Adjust the zip to prevent unzip issues, temp2 -> temp1 - ZipUtils.zipAdjust(temp2.getPath(), temp1.getPath()); + // Then sign the zip for the first time, temp2 -> temp1 + ZipUtils.signZip(temp2, temp1, false); - // Finally, sign the whole zip file again, temp1 -> temp2 - ZipUtils.signZip(temp1, temp2, true); + // Adjust the zip to prevent unzip issues, temp1 -> temp2 + ZipUtils.zipAdjust(temp1.getPath(), temp2.getPath()); - // Write it to the target zip, temp2 -> file - try ( - OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile)); - InputStream source = new BufferedInputStream(new FileInputStream(temp2)) - ) { - byte[] buffer = new byte[4096]; - int length; - while ((length = source.read(buffer)) > 0) - out.write(buffer, 0, length); - } + // Finally, sign the whole zip file again, temp2 -> target + ZipUtils.signZip(temp2, mFile, true); // Delete temp files temp1.delete(); @@ -149,8 +145,8 @@ public class ProcessRepoZip extends ParallelTask { Activity activity = getActivity(); if (activity == null) return; progressDialog.dismiss(); - Uri uri = Uri.fromFile(mFile); if (result) { + Uri uri = Uri.fromFile(mFile); if (Shell.rootAccess() && mInstall) { Intent intent = new Intent(activity, FlashActivity.class); intent.setData(uri).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 19ec5afcc..a8314daeb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -225,13 +225,15 @@ public class Utils { } } - public static void inToOut(InputStream in, OutputStream out) throws IOException { - int read; + public static int inToOut(InputStream in, OutputStream out) throws IOException { + int read, total = 0; byte buffer[] = new byte[4096]; while ((read = in.read(buffer)) > 0) { out.write(buffer, 0, read); + total += read; } out.flush(); + return total; } public static void patchDTBO() {