diff --git a/app/src/main/java/com/github/catvod/spider/Market.java b/app/src/main/java/com/github/catvod/spider/Market.java index ebf7109f..338cf790 100644 --- a/app/src/main/java/com/github/catvod/spider/Market.java +++ b/app/src/main/java/com/github/catvod/spider/Market.java @@ -86,10 +86,10 @@ public class Market extends Spider { setBusy(true); Init.run(this::setDialog, 500); Response response = OkHttp.newCall(url); - File file = new File(Path.download(), Uri.parse(url).getLastPathSegment()); + File file = Path.create(new File(Path.download(), Uri.parse(url).getLastPathSegment())); download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1"))); if (file.getName().endsWith(".zip")) FileUtil.unzip(file, Path.download()); - if (file.getName().endsWith(".apk")) FileUtil.openFile(Path.chmod(file)); + if (file.getName().endsWith(".apk")) FileUtil.openFile(file); else Notify.show("下載完成"); checkCopy(url); dismiss(); diff --git a/app/src/main/java/com/github/catvod/utils/Path.java b/app/src/main/java/com/github/catvod/utils/Path.java index f56d9519..b6b942c4 100644 --- a/app/src/main/java/com/github/catvod/utils/Path.java +++ b/app/src/main/java/com/github/catvod/utils/Path.java @@ -7,14 +7,13 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Path { - private static File check(File file) { + private static File mkdir(File file) { if (!file.exists()) file.mkdirs(); return file; } @@ -28,7 +27,7 @@ public class Path { } public static File tv() { - return check(new File(root() + File.separator + "TV")); + return mkdir(new File(root() + File.separator + "TV")); } public static File tv(String name) { @@ -62,11 +61,10 @@ public class Path { public static File write(File file, byte[] data) { try { - FileOutputStream fos = new FileOutputStream(file); + FileOutputStream fos = new FileOutputStream(create(file)); fos.write(data); fos.flush(); fos.close(); - chmod(file); return file; } catch (Exception ignored) { return file; @@ -75,28 +73,26 @@ public class Path { public static void copy(InputStream in, File out) { try { - copy(in, new FileOutputStream(out)); + int read; + byte[] buffer = new byte[8192]; + FileOutputStream fos = new FileOutputStream(create(out)); + while ((read = in.read(buffer)) != -1) fos.write(buffer, 0, read); + fos.close(); + in.close(); } catch (Exception ignored) { } } - public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException { - byte[] buffer = new byte[8192]; - int amountRead; - while ((amountRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, amountRead); - } - } - public static List list(File dir) { File[] files = dir.listFiles(); return files == null ? Collections.emptyList() : Arrays.asList(files); } - public static File chmod(File file) { + public static File create(File file) throws Exception { try { - Process process = Runtime.getRuntime().exec("chmod 777 " + file); - process.waitFor(); + if (!file.canWrite()) file.setWritable(true); + if (!file.exists()) file.createNewFile(); + Shell.exec("chmod 777 " + file); return file; } catch (Exception e) { e.printStackTrace(); diff --git a/app/src/main/java/com/github/catvod/utils/Shell.java b/app/src/main/java/com/github/catvod/utils/Shell.java new file mode 100644 index 00000000..9563e9c1 --- /dev/null +++ b/app/src/main/java/com/github/catvod/utils/Shell.java @@ -0,0 +1,13 @@ +package com.github.catvod.utils; + +public class Shell { + + public static void exec(String command) { + try { + int code = Runtime.getRuntime().exec(command).waitFor(); + if (code != 0) throw new RuntimeException("Shell command failed with exit code " + code); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file