diff --git a/app/src/main/java/com/example/studyapp/MainActivity.java b/app/src/main/java/com/example/studyapp/MainActivity.java index 27badf8..09f8101 100644 --- a/app/src/main/java/com/example/studyapp/MainActivity.java +++ b/app/src/main/java/com/example/studyapp/MainActivity.java @@ -154,7 +154,18 @@ public class MainActivity extends AppCompatActivity { LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components",null); Button runScriptButton = findViewById(R.id.run_script_button); if (runScriptButton != null) { - runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); + runScriptButton.setOnClickListener(v -> { + initializeExecutorService(); + + executorService.submit(() -> { + try { + TaskUtil.infoUpload(MainActivity.this, getAndroidId(MainActivity.this), "wsj.reader_sp"); + } catch (Exception e) { + e.printStackTrace(); + } + }); + }); +// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); } else { LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found",null); Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/com/example/studyapp/task/TaskUtil.java b/app/src/main/java/com/example/studyapp/task/TaskUtil.java index 7e74a78..be10324 100644 --- a/app/src/main/java/com/example/studyapp/task/TaskUtil.java +++ b/app/src/main/java/com/example/studyapp/task/TaskUtil.java @@ -6,8 +6,11 @@ import android.content.Context; import android.os.Environment; import android.text.TextUtils; import android.util.Log; + +import com.example.studyapp.utils.FileUtils; import com.example.studyapp.utils.LogFileUtil; import com.example.studyapp.utils.ShellUtils; +import com.example.studyapp.utils.ZipUtils; import com.google.android.gms.common.util.CollectionUtils; import com.google.android.gms.common.util.MapUtils; import com.google.gson.Gson; @@ -224,11 +227,12 @@ public class TaskUtil { if (zipFile.exists()) { delFileSh(zipFile.getAbsolutePath()); } - File copiedAPKFile = new File(context.getCacheDir(), packAge+"_upload.apk"); + File copiedAPKFile = new File(context.getCacheDir(), packAge); if (copiedAPKFile.exists()) { delFileSh(copiedAPKFile.getAbsolutePath()); } - copyFolderSh(apkSourceFile, copiedAPKFile.getAbsolutePath()); + String parentPath = FileUtils.getParentDirectory(apkSourceFile); + copyFolderSh(parentPath, copiedAPKFile.getAbsolutePath()); // boolean success = clearUpFileInDst(copiedDir); // if (success) { // // 压缩APK文件 @@ -236,13 +240,21 @@ public class TaskUtil { // } // 压缩APK文件 - compressToZip(copiedAPKFile, zipFile); +// compressToZip(copiedAPKFile, zipFile); + try { + ZipUtils.zipDirectory(copiedAPKFile.getAbsolutePath(), zipFile.getAbsolutePath()); + }catch (Exception e){ + zipFile.delete(); + e.printStackTrace(); + } + Log.e("TAG", "infoUpload compress finish: "); // 上传压缩文件 if (!zipFile.exists()) { Log.w("TaskUtil", "Upload file does not exist: " + outputZipPath); return; } - + String safeNewPath = zipFile.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\""); + String chmod = "chmod 777 \"" + safeNewPath + "\""; uploadFile(zipFile); //uninstall @@ -250,6 +262,7 @@ public class TaskUtil { String chmodResult = ShellUtils.execRootCmdAndGetResult(uninstall); // 清理临时文件 delFileSh(copiedAPKFile.getAbsolutePath()); + ShellUtils.execRootCmdAndGetResult(chmod); delFileSh(zipFile.getAbsolutePath()); } @@ -282,6 +295,7 @@ public class TaskUtil { String result = ShellUtils.execRootCmdAndGetResult(cmd); String chmod = "chmod 777 \"" + safeNewPath + "\""; String chmodResult = ShellUtils.execRootCmdAndGetResult(chmod); + recursiveChmod777(dst); // if (!TextUtils.isEmpty(chmodResult)) { LogFileUtil.logAndWrite(android.util.Log.ERROR, "TaskUtil", "chmodResult. Result: " + chmodResult, null); // return false; @@ -299,12 +313,37 @@ public class TaskUtil { } } + public static void recursiveChmod777(File dir) { + if (dir == null || !dir.exists()) { + Log.e("Chmod", "目录不存在或为null"); + return; + } + + // 先修改当前目录权限 + String currentPath = dir.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\""); + String chmodCmd = "chmod 777 \"" + currentPath + "\""; + ShellUtils.execRootCmdAndGetResult(chmodCmd); + + // 递归处理子项 + File[] children = dir.listFiles(); + if (children != null) { + for (File child : children) { + if (child.isDirectory()) { + recursiveChmod777(child); // 递归处理子目录 + } else { + String filePath = child.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\""); + chmodCmd = "chmod 777 \"" + filePath + "\""; + ShellUtils.execRootCmdAndGetResult(chmodCmd); + } + } + } + } + /** *在给定的目标目录中清除特定的文件和目录。 *子目录未命名为“缓存”,并且删除了大于3 MB的文件。 *记录保留的文件和目录的路径。 * - * @param DST将处理其文件和子目录的目标目录 * @return true如果目的目录存在并且已成功处理,则为false否则 */ private static boolean clearUpFileInDst(File dst) { diff --git a/app/src/main/java/com/example/studyapp/utils/FileUtils.java b/app/src/main/java/com/example/studyapp/utils/FileUtils.java new file mode 100644 index 0000000..4fe7d81 --- /dev/null +++ b/app/src/main/java/com/example/studyapp/utils/FileUtils.java @@ -0,0 +1,52 @@ +package com.example.studyapp.utils; + +import java.io.File; + +public class FileUtils { + /** + * 获取文件的父目录路径 + * @param filePath 文件完整路径 + * @return 父目录路径,如果失败返回null + */ + public static String getParentDirectory(String filePath) { + if (filePath == null || filePath.isEmpty()) { + return null; + } + + File file = new File(filePath); + File parent = file.getParentFile(); + + return parent != null ? parent.getAbsolutePath() : null; + } + + /** + * 获取文件的父目录File对象 + * @param file 文件对象 + * @return 父目录File对象 + */ + public static File getParentDirectory(File file) { + return file != null ? file.getParentFile() : null; + } + + /** + * 获取多级上级目录 + * @param file 文件对象 + * @param levels 向上追溯的层级数 + * @return 上级目录路径 + */ + public static String getAncestorDirectory(File file, int levels) { + if (file == null || levels <= 0) { + return file != null ? file.getAbsolutePath() : null; + } + + File parent = file; + for (int i = 0; i < levels; i++) { + parent = parent.getParentFile(); + if (parent == null) { + break; + } + } + + return parent != null ? parent.getAbsolutePath() : null; + } +} diff --git a/app/src/main/java/com/example/studyapp/utils/ZipUtils.java b/app/src/main/java/com/example/studyapp/utils/ZipUtils.java new file mode 100644 index 0000000..5adf09c --- /dev/null +++ b/app/src/main/java/com/example/studyapp/utils/ZipUtils.java @@ -0,0 +1,64 @@ +package com.example.studyapp.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipUtils { + /** + * 压缩目录为ZIP文件 + * @param sourceDirPath 要压缩的目录路径(如:/sdcard/MyFolder) + * @param zipFilePath 生成的ZIP文件路径(如:/sdcard/Archive.zip) + * @throws IOException 如果操作失败 + */ + public static void zipDirectory(String sourceDirPath, String zipFilePath) throws IOException { + File sourceDir = new File(sourceDirPath); + if (!sourceDir.exists() || !sourceDir.isDirectory()) { + throw new IOException("源目录不存在或不是目录"); + } + + try (FileOutputStream fos = new FileOutputStream(zipFilePath); + ZipOutputStream zos = new ZipOutputStream(fos)) { + + addDirectoryToZip(sourceDir, sourceDir, zos); + } + } + + /** + * 递归添加目录内容到ZIP + */ + private static void addDirectoryToZip(File rootDir, File currentDir, ZipOutputStream zos) throws IOException { + File[] files = currentDir.listFiles(); + if (files == null) return; + + for (File file : files) { + if (file.isDirectory()) { + addDirectoryToZip(rootDir, file, zos); // 递归处理子目录 + } else { + addFileToZip(rootDir, file, zos); + } + } + } + + /** + * 添加单个文件到ZIP + */ + private static void addFileToZip(File rootDir, File file, ZipOutputStream zos) throws IOException { + try (FileInputStream fis = new FileInputStream(file)) { + // 计算相对路径(确保ZIP内目录结构正确) + String relativePath = rootDir.toURI().relativize(file.toURI()).getPath(); + ZipEntry zipEntry = new ZipEntry(relativePath); + zos.putNextEntry(zipEntry); + + byte[] buffer = new byte[1024]; + int length; + while ((length = fis.read(buffer)) > 0) { + zos.write(buffer, 0, length); + } + zos.closeEntry(); + } + } +}