Android压缩包下载解压

Android压缩包下载解压_第1张图片

使用说明:在需要处理大文件下载,为了节省流量就在后台做成了压缩包,app端联网下载到本地解压使用;

使用步骤:

1 联网下载

 在下使用的是okGo,此框架比Retrufit还简单,为什么不用呢。

 okGo下载文件和下载其他是不一样的联网方法:

OkGo.post(Constants.HEART_URL + drawingUrl)
        .tag(this)
        .connTimeOut(30000)
        .params("conditionParam", "")
        .isMultipart(true)
        .execute(fileCallback);
 看出来了吧,不一致的地方.execute(FileCallback)

 FileCallBack的常用方法:

/**
 * 下载zip文件的回调
 */
public FileCallback fileCallback = new FileCallback() {
    @Override
    public void onSuccess(File file, okhttp3.Call call, okhttp3.Response response) {
        ToastUtil.showToast("文件下载成功");
       
    }

    @Override
    public void onBefore(BaseRequest request) {
        cancelProgressDialog();
    }

    @Override
    public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
        LogUtil.e("下载进度currentSize:" + currentSize + "  totalSize:" + totalSize + "   progress:" + progress + "   networkSpeed:" + networkSpeed);
        int answer = (int) ((progress*100)/1);
        showProgressDialog("正在下载图纸"+ answer + "%");
    }

    @Override
    public void onAfter(File file, Exception e) {
        cancelProgressDialog();
    }

    @Override
    public void onError(okhttp3.Call call, okhttp3.Response response, Exception e) {
        cancelProgressDialog();
        ToastUtil.showToast("下载失败" + e.getMessage());
    }
};
关于用法,请直接百度下。这里需要详细解释用法的是解压:

2 在下载成功的回调fileCallBack的onSucess()方法里调用自定义工具类直接解压,

解压工具类:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by sensyang on 2018/1/5.
 * @author: 一个人的暗
 * @Emial:[email protected]
 */

public class ZipUtil {

    public static ZipUtil instance;

    public static ZipUtil getInstance(){
        if (instance == null) {
            instance = new ZipUtil();
        }
        return instance;
    }

    /**
     * 解压
     * @param PATH    解压到的地址
     * @param zipName  zip文件
     */

    public boolean unZip(String PATH,String zipName) {
        boolean isOver = true;
        File file = new File(zipName);
        LogUtil.e("进入解压");
        try {
            upZipFile(file, PATH);
            // upZipFile(zip文件,解压到的地址);
        } catch (IOException e) {
            e.printStackTrace();
            isOver = false;
            ToastUtil.showToast("解压失败"+ e.getMessage());
        }
        return isOver;
    }

    /**
     * 解压缩
     * 将zipFile文件解压到folderPath目录下.
     * @param zipFile zip文件
     * @param folderPath 解压到的地址
     * @throws IOException
     */
    private void upZipFile(File zipFile, String folderPath) throws IOException {
        ZipFile zfile = new ZipFile(zipFile);
        Enumeration zList = zfile.entries();
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            if (ze.isDirectory()) {
                String dirstr = folderPath + ze.getName();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }
            OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
            InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
            int readLen = 0;
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                os.write(buf, 0, readLen);
            }
            is.close();
            os.close();
        }
        zfile.close();
    }

    /**
     * 给定根目录,返回一个相对路径所对应的实际文件名.
     * @param baseDir     指定根目录
     * @param absFileName 相对路径名,来自于ZipEntry中的name
     * @return java.io.File 实际的文件
     */
    public File getRealFileName(String baseDir, String absFileName) {
        String[] dirs = absFileName.split("/");
        File ret = new File(baseDir);
        String substr = null;
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                substr = dirs[i];
                try {
                    substr = new String(substr.getBytes("8859_1"), "GB2312");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                ret = new File(ret, substr);

            }
            if (!ret.exists()) {
                ret.mkdirs();
            }
            substr = dirs[dirs.length - 1];
            try {
                substr = new String(substr.getBytes("8859_1"), "GB2312");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            ret = new File(ret, substr);
            LogUtil.e("解压后的路径1:"+ ret.getAbsolutePath());
            return ret;
        }
        return ret;
    }

}

调用方法:

String dirName = Environment.getExternalStorageDirectory() + "/YaRui/";
File f = new File(dirName);
//不存在创建
if (!f.exists()) {
    f.mkdir();
}
ZipUtil.getInstance().unZip(f.getAbsolutePath(),file.getAbsolutePath());

所有代码亲测有用,有问题请咨询:

532245792 一个人的暗,谢谢。(也是转自前辈的,但是看的太多了,到底是说的?)

你可能感兴趣的:(日记)