Clean file code

This commit is contained in:
FongMi 2024-02-21 13:53:39 +08:00
parent 6970f6c8fe
commit 7885f33752
3 changed files with 28 additions and 19 deletions

View File

@ -86,10 +86,10 @@ public class Market extends Spider {
setBusy(true); setBusy(true);
Init.run(this::setDialog, 500); Init.run(this::setDialog, 500);
Response response = OkHttp.newCall(url); 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"))); 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(".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("下載完成"); else Notify.show("下載完成");
checkCopy(url); checkCopy(url);
dismiss(); dismiss();

View File

@ -7,14 +7,13 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class Path { public class Path {
private static File check(File file) { private static File mkdir(File file) {
if (!file.exists()) file.mkdirs(); if (!file.exists()) file.mkdirs();
return file; return file;
} }
@ -28,7 +27,7 @@ public class Path {
} }
public static File tv() { 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) { public static File tv(String name) {
@ -62,11 +61,10 @@ public class Path {
public static File write(File file, byte[] data) { public static File write(File file, byte[] data) {
try { try {
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(create(file));
fos.write(data); fos.write(data);
fos.flush(); fos.flush();
fos.close(); fos.close();
chmod(file);
return file; return file;
} catch (Exception ignored) { } catch (Exception ignored) {
return file; return file;
@ -75,16 +73,13 @@ public class Path {
public static void copy(InputStream in, File out) { public static void copy(InputStream in, File out) {
try { try {
copy(in, new FileOutputStream(out)); int read;
} catch (Exception ignored) {
}
}
public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int amountRead; FileOutputStream fos = new FileOutputStream(create(out));
while ((amountRead = inputStream.read(buffer)) != -1) { while ((read = in.read(buffer)) != -1) fos.write(buffer, 0, read);
outputStream.write(buffer, 0, amountRead); fos.close();
in.close();
} catch (Exception ignored) {
} }
} }
@ -93,10 +88,11 @@ public class Path {
return files == null ? Collections.emptyList() : Arrays.asList(files); return files == null ? Collections.emptyList() : Arrays.asList(files);
} }
public static File chmod(File file) { public static File create(File file) throws Exception {
try { try {
Process process = Runtime.getRuntime().exec("chmod 777 " + file); if (!file.canWrite()) file.setWritable(true);
process.waitFor(); if (!file.exists()) file.createNewFile();
Shell.exec("chmod 777 " + file);
return file; return file;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -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();
}
}
}