用命令行使用7z压缩
16lz
2021-01-26
import android.content.Context;import android.os.AsyncTask;import com.dongnao.a7zipandroid.command.CommandUtils;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;public class ZipHelper { /** * 执行结果回调 */ public interface OnResultListener { void onSuccess(String msg); void onFailure(int errorno, String msg); void onProgress(String msg); } /** * 将可执行文件 从assets拷贝到 /data/data/包名 下 * * @param context * @param binaryName * @return */ public static boolean loadBinary(Context context, String binaryName) { ///data/data/包名 File binaryFile = new File(context.getFilesDir(), binaryName); if (binaryFile.exists()) { //存在 但不能执行 if (!binaryFile.canExecute()) { //设置可执行并 返回加过 binaryFile.setExecutable(true); } } else { //根据cpu abi拷贝可执行文件 if (CommandUtils.copyAssets2File(context, binaryName)) { if (!binaryFile.canExecute()) { //设置可执行并 返回加过 binaryFile.setExecutable(true); } } } return binaryFile.exists() && binaryFile.canExecute(); } public static void execute(Context context, String cmd, OnResultListener listener) { File filesDir = context.getFilesDir(); // /data/data/包名/7zr new ExecuteAysnTask(filesDir.getAbsolutePath() + "/" + cmd, listener).execute(); } /** * 结果记录 */ static class Result { boolean success; String output; int errorno; public Result(boolean success, String output, int errorno) { this.success = success; this.output = output; this.errorno = errorno; } } static class ExecuteAysnTask extends AsyncTask, String, Result> { private OnResultListener listener; private String cmd; public ExecuteAysnTask(String cmd, OnResultListener listener) { this.cmd = cmd; this.listener = listener; } @Override protected Result doInBackground(Void... voids) { Process process = null; //执行结果输出 String out; try { //执行任务 process = Runtime.getRuntime().exec(cmd); //查看是否执行完成 while (!isComplete(process)) { //读取运行过程中的输出信息 BufferedReader reader = new BufferedReader(new InputStreamReader (process.getInputStream())); String line; while ((line = reader.readLine()) != null) { //报告执行过程 publishProgress(line); } } int exitValue = process.exitValue(); //成功 if (exitValue == 0) { out = CommandUtils.inputStream2String(process.getInputStream()); } else { out = CommandUtils.inputStream2String(process.getErrorStream()); } return new Result(exitValue == 0, out, exitValue); } catch (IOException e) { e.printStackTrace(); out = e.getMessage(); } finally { if (null != process) { process.destroy(); } } return new Result(false, out, -1); } @Override protected void onProgressUpdate(String... values) { listener.onProgress(values[0]); } @Override protected void onPostExecute(Result result) { if (result.success) { listener.onSuccess(result.output); } else { listener.onFailure(result.errorno, result.output); } } /** * 查看程序是否结束 * * @param process * @return */ private boolean isComplete(Process process) { try { //如果已经结束则返回结果 否则会出现IllegalThreadStateException异常 process.exitValue(); return true; } catch (IllegalThreadStateException e) { } return false; } }}
import android.content.Context;import android.os.Build;import android.text.TextUtils;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class CommandUtils { /** * 拷贝文件 * * @param context * @param binary * @return */ public static boolean copyAssets2File(Context context, String binary) { // /data/data/package File filesDirectory = context.getFilesDir(); boolean ret = false; InputStream is = null; FileOutputStream fos = null; try { //根据cpu 拷贝不同的可执行文件 if (Build.CPU_ABI.startsWith("armeabi")) { is = context.getAssets().open("libs/armeabi/" + binary); } else if (Build.CPU_ABI.equals("x86")) { is = context.getAssets().open("libs/" + Build.CPU_ABI + "/" + binary); } else { return false; } fos = new FileOutputStream(new File(filesDirectory, binary)); byte[] buffer = new byte[2048]; int n; while ((n = is.read(buffer)) != -1) { fos.write(buffer, 0, n); } fos.flush(); ret = true; } catch (IOException e) { e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != fos) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return ret; } public static String inputStream2String(InputStream inputStream) { try { BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); String str; StringBuilder sb = new StringBuilder(); while ((str = r.readLine()) != null) { sb.append(str); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }}
更多相关文章
- 理解Android(安卓)UI线程
- 自定义线程池ThreadPoolExecutor
- android之popupwindow显示文件列表
- Android:StartActivies(Intent[] intents)用法
- android 在WebView打开网页
- Android(安卓)可执行文件结构的分析
- EditText与退格键的冲突问题
- Android兼容性优化-8.0之后禁止在后台启动服务的兼容性优化
- Monkey入门之如何在android虚拟机中安装apk包