Android解压缩zip文件

期望:使项目apk包中的大文件批量数据可以单独作为一个apk来管理,所以需要将Android assets文件夹下的zip打包文件解压到SDCard上。

解压缩的操作由于比较耗时,所以新开一个线程执行以下函数

/**将assets对应文件解压到对应的sdcard目录中*/
public void unPress2Sdcard(String fileName){
    try {
        /**目标路径*/
        String destDir = rootPath + File.separator + fileName;
        /**将压缩文件拷贝到内存卡中*/
        mAssetCopyer.copyBigDataToSD(destDir, fileName);
        /**解压文件到目标路径*/
        unzip(destDir, offlinePath);
        /**删除在内存卡上临时存在的压缩文件*/
        FileUtils.deleteFile(destDir);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

对应的先将assets下的zip文件拷贝到SDCard中,这个是直接在网上找的方法拿来用

public void copyBigDataToSD(String strOutFileName,String file) throws IOException{
    InputStream myInput;
    OutputStream myOutput = new FileOutputStream(strOutFileName);
    myInput = mContext.getAssets().open(file);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while(length > 0){
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }
    myOutput.flush();
    myInput.close();
    myOutput.close();
}

然后解压缩操作

@SuppressWarnings("unchecked")  
public static void unzip(String zipFilePath, String unzipFilePath) throws Exception{
    /**验证是否为空*/
    if (isEmpty(zipFilePath) || isEmpty(unzipFilePath)){

    }
    File zipFile = new File(zipFilePath);
    /**创建解压缩文件保存的路径*/
    File unzipFileDir = new File(unzipFilePath);
    if (!unzipFileDir.exists()){
        unzipFileDir.mkdirs();
    }
    //开始解压
    ZipEntry entry = null;
    String entryFilePath = null;
    int count = 0, bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];  
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;  
    ZipFile zip = new ZipFile(zipFile);
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();  
    //循环对压缩包里的每一个文件进行解压
    while(entries.hasMoreElements()){
        entry = entries.nextElement();
        log("log ing5:"+entry.getName());
        /**这里提示如果当前元素是文件夹时,在目录中创建对应文件夹
         * ,如果是文件,得出路径交给下一步处理*/
        entryFilePath = unzipFilePath + File.separator + entry.getName();
        File file = new File(entryFilePath);
        log("~~是否是文件夹:"+file.isDirectory());
        if(entryFilePath.endsWith("/")){
            if(!file.exists()){
                file.mkdir();
            }
            continue;
        }
        /***这里即是上一步所说的下一步,负责文件的写入,不服来咬(≖ ‿ ≖)✧*/
        bos = new BufferedOutputStream(new FileOutputStream(entryFilePath+"/"));
        bis = new BufferedInputStream(zip.getInputStream(entry));
        while ((count = bis.read(buffer, 0, bufferSize)) != -1){
            bos.write(buffer, 0, count);
        }
        bos.flush();
        bos.close();
    }
}

用的也是直接网上down下来的,但是在解压缩操作中遇到的是这一句bos = new BufferedOutputStream(new FileOutputStream(entryFilePath));传入的entryFilePath运行的时候却老提示不是文件夹,于是把中间循环解压元素的代码以及一些冗余的逻辑删除了,改成了现在这样,进入循环先判断是否是文件夹(我这里是靠尾部的字符判断的),如果是,在目标路径中创建,开始下一循环,不是,就把文件解压到对应路径。

最后一步删除SDcard下的压缩文件
FileUtils.deleteFile(destDir);

注意:源压缩包必须是zip格式的并且是快速压缩,超高压缩算法不同,解析不了,我是在window上用快压打包的。

Loading Disqus comments...
Table of Contents