解压 输入解压包输出解压文件路径

public class MyZipFile {

    private static final String TAG = MyZipFile.class.getSimpleName();
    public static boolean unZipFile(Context context, String inPath, String outPath) throws IOException {
        InputStream is = null;
        try {
            File file = new File(inPath);
            is = new FileInputStream(file);
            Log.i(TAG, "unZipFile()inPath = "+inPath+", outPath = " + outPath);
            if (inPath.contains(".rar") || inPath.contains(".zip")) {
                Log.i(TAG, "unZipFile() outPath:" + outPath);
                ZipInputStream zis = new ZipInputStream(is);
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                boolean isZipFileOk = false;
                try {

                    byte[] buffer = new byte[200 * 1024];
                    int index = outPath.lastIndexOf(File.separator);
                    outPath = outPath.substring(0, index);
                    File fileOut = new File(outPath);
                    if (!fileOut.exists()) {
                        fileOut.mkdirs();
                    }
                    for (ZipEntry zipEntry = zis.getNextEntry();
                         zipEntry != null; zipEntry = zis.getNextEntry()) {
                        String fileName = zipEntry.getName();
                        fileOut = new File(outPath + File.separator + fileName);
                        if (zipEntry.isDirectory()) {//如果是一个目录
                            fileOut.mkdir();
                        } else { //如果是文件
                            if (fileOut.exists()) {
                                //if (BuildConfig.DEBUG || isUpZipDeleteFile) {
//                                if (true) {
//                                    fileOut.delete();
//                                } else {
                                    continue;
                                //}
                            } else {
                                checkFile(fileOut.getParent());
                            }
                            int count = 0;
                            boolean ret = fileOut.createNewFile();
                            fos = new FileOutputStream(fileOut);
                            bos=new BufferedOutputStream(fos);  //加缓存
                            while ((count = zis.read(buffer)) > 0) {
                                bos.write(buffer, 0, count);
                            }
                            if(bos != null) {
                                bos.flush();
                                bos.close();
                                bos = null;
                            }
                            if(fos != null)
                                fos.close();
                            fos = null;

                        }
                    }
                    if(zis != null) {
                        zis.close();
                        zis = null;
                    }
                    isZipFileOk = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    isZipFileOk = false;
                } finally {
                    if(bos != null) {
                        bos.flush();
                        bos.close();
                        bos = null;
                    }
                    if(fos != null)
                        fos.close();
                    fos = null;

                    if(zis != null) {
                        zis.close();
                        zis = null;
                    }

                    return isZipFileOk;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "unZipFile() Exception:" + e.getMessage());
        }
        return false;
    }

    /**
     * 检测目录,不存在则创建
     *
     * @param filePath
     */
    public static boolean checkFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            boolean ret = file.mkdirs();
            return ret;
        }
        return true;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值