用命令行使用7z压缩

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<Void, 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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值