zip解压线程(解决中文问题)

准备工作:

android自带的zip解压不能处理中文文件名,需要引用第三方jar

apache的commons-compress 1.12

http://central.maven.org/maven2/org/apache/commons/commons-compress/1.12/commons-compress-1.12.jar

下载下来,放到app的libs目录

as转换到project视图

右击那个jar

然后:

就会在build.gradle(module:app)自动添加:

implementation files(‘libs/commons-compress-1.12.jar‘)

解压线程:

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.RawRes;
import android.util.Log;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;

/*
* @author [email protected]
* create at 2018/6/12
* description:zip解压线程
*/
public class LeesUnzipThread extends AsyncTask<String,Integer,Integer> {
    String TAG="LeesUnzipThread";
    ProgressDialog  progressDialog=null;
    RoundProgressBar roundProgressBar=null;
    String strZipFile=null;
    int srcType=0;
    int resID=-1;//资源ID
    Context mContext=null;

    public LeesUnzipThread(@NonNull String zipFile,ProgressDialog dlg){
        strZipFile=zipFile;
        progressDialog=dlg;
        srcType=0;
    }

    public LeesUnzipThread(@NonNull String zipFile,RoundProgressBar roundProgressBar){
        strZipFile=zipFile;
        this.roundProgressBar=roundProgressBar;
        srcType=0;
    }

    public LeesUnzipThread(@RawRes int srcID,@NonNull Context context,ProgressDialog dlg){
        resID=srcID;
        mContext=context;
        progressDialog=dlg;
        srcType=1;
    }

    public LeesUnzipThread(@RawRes int srcID,@NonNull Context context,RoundProgressBar roundProgressBar){
        resID=srcID;
        mContext=context;
        this.roundProgressBar=roundProgressBar;
        srcType=1;
    }

    private int getZipFileCount(@NonNull InputStream is){
        int nFileCount=0;
        ZipArchiveInputStream zis=new ZipArchiveInputStream(is,"GBK");

        ArchiveEntry ae=null;
        ZipEntry ze=null;
        try {
            while ((ze =(ZipEntry) zis.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    nFileCount++;
                }
            }
        }catch (IOException e){
            Log.e(TAG,e.getMessage());
        }finally {
            try{
                if(zis!=null){
                    zis.close();
                }
            }catch (IOException e){
                Log.e(TAG,e.getMessage());
            }
        }
        return nFileCount;
    }

    /*
    * 函数功能:获取压缩包中文件(不含目录)的数量
    * Author: [email protected]
    * Create: 2018/6/12 22:32
    */
    public int getFileCount() {
        InputStream is = null;
        int nFileCount = 0;//压缩文档中文件数量
        if(srcType==1){//res.raw下的zip文档
            Resources resources=mContext.getResources();
            is = resources.openRawResource(resID);
        }else if(srcType==0){//普通路径下的zip文档
            File file = new File(strZipFile);
            try {
                if(!file.exists()){
                    throw new IOException("File "+ strZipFile +"not exist!");
                }
                is = new FileInputStream(file);
            }catch (IOException e){
                Log.e(TAG,e.getMessage());
            }
        }

        if(is!=null){
            nFileCount=getZipFileCount(is);
            try {
                is.close();
            }catch (IOException e){
                nFileCount=0;
                Log.e(TAG,e.getMessage());
            }
        }

        return nFileCount;
    }

    private void unZipToFolder(@NonNull String strDestFolder){
        Log.i("unZipResZip: ","strDestFolder:\t"+ strDestFolder);
        if(!strDestFolder.endsWith("\\") && !strDestFolder.endsWith("/") ){
            strDestFolder += File.separator;
        }

        File dir = new File(strDestFolder);
        if(!dir.exists()){
            dir.mkdirs();
        }

        int BUFFER = 4096; //这里缓冲区我们使用4KB,
        InputStream is = null;
        int nFileCounter = 0;//解压计数(不含目录)
        //region 准备InputStream
        if(srcType==1){//res.raw下的zip文档
            Resources resources=mContext.getResources();
            is = resources.openRawResource(resID);
        }else if(srcType==0){//普通路径下的zip文档
            File file = new File(strZipFile);
            try {
                if(!file.exists()){
                    throw new IOException("File "+ strZipFile +"not exist!");
                }
                is = new FileInputStream(file);
            }catch (IOException e){
                Log.e(TAG,e.getMessage());
            }
        }
        //endregion

        if(is!=null){
            //region 解压缩
            ZipArchiveInputStream zis=new ZipArchiveInputStream(is,"GBK");
            ArchiveEntry ae=null;
            try {
                while ((ae = zis.getNextEntry()) != null) {
                    // 获取文件名
                    String entryFileName = ae.getName();
                    // 解压目标文件全路径
                    String strDstFullPath = strDestFolder + entryFileName;
                    if (ae.isDirectory()) {
                        File folder = new File(strDstFullPath);
                        if(!folder.exists()){
                            folder.mkdirs();
                        }
                        continue;
                    }
                    OutputStream os=new BufferedOutputStream(new FileOutputStream(strDstFullPath));
                    byte[] buf = new byte[1024 ];
                    int len = -1;
                    while((len = zis.read(buf)) != -1) {
                        os.write(buf, 0, len);
                    }
                    os.flush();
                    os.close();
                    nFileCounter++;
                    publishProgress(nFileCounter);
                }
            }catch (IOException e){
                Log.e(TAG,e.getMessage());
            }finally {
                try{
                    if(zis!=null){
                        zis.close();
                    }
                }catch (IOException e){
                    Log.e(TAG,e.getMessage());
                }
            }
            //endregion
            try {
                is.close();
            }catch (IOException e){
                Log.e(TAG,e.getMessage());
            }
        }

    }
    @Override
    protected Integer doInBackground(String... strings) {
        //region 参数校验
        String strDestFolder=strings[0];

        if(strDestFolder==null){
            try {
                throw new FileNotFoundException("dest folder if null");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
        //endregion
        unZipToFolder(strDestFolder);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress){
        super.onProgressUpdate(progress);
        if(progressDialog!=null){
            progressDialog.setProgress(progress[0]);
        }
        else if(roundProgressBar!=null){
            roundProgressBar.setProgress(progress[0]);
        }
    }
}

调用示例:

PermissionUtil.verifyStoragePermissions(this);

       //RoundProgressBar为自定义圆形进度
        mRoundProgressBar=(RoundProgressBar)findViewById(R.id.rpb_unzip);

       //raw下zip  内含中文目录
        LeesUnzipThread unzipThread=new LeesUnzipThread(R.raw.dlsw,getBaseContext(),mRoundProgressBar);
       //sdcard/download目录下zip  内含中文目录
//        LeesUnzipThread unzipThread=new LeesUnzipThread("/storage/emulated/0/download/dlsw.zip",mRoundProgressBar);
        int nFileCount=unzipThread.getFileCount();
        if(nFileCount>0){
            mRoundProgressBar.setMax(nFileCount);            //开始解压到目标文件夹
            unzipThread.execute("/storage/emulated/0/dlshouwen/");
        }

原文地址:https://www.cnblogs.com/xhzxlqt/p/9175677.html

时间: 2024-08-02 18:01:52

zip解压线程(解决中文问题)的相关文章

解决ubuntu中zip解压的中文乱码问题

在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码问题. 安装 代码: sudo apt-get install unar 12.04以下或者想编译安装的朋友请参考: 使用 代码: lsar foo.zip #列出所有文件 如果列出的文件名已经正确 代码: unar foo.zip #解压所有文件 如果列出的文件名还不正确 代码: lsar -e GB18030 foo.zip #指定使用GB18030编码列出所有文件 unar -e GB18030 foo

JAVA zip解压 MALFORMED 错误

最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(ZipCoder.java:58) at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567) at java.util.zip.ZipFil

CentOS7下zip解压和unzip压缩文件

1.安装zip.unzip应用. yum install zip unzip 2.压缩和解压文件 以下命令均在/home目录下操作 cd /home #进入/home目录 a.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 b.把/home目录下面的mydata.zip解压到mydatabak目录里面 unzip mydata.zip -d mydatabak c.把/home目录下面的abc文件夹和12

windows下tomcat zip解压版安装方法

下面记录一下在win7(32位)系统下,安装zip解压版的方法: 一.下载zip压缩包 地址:http://tomcat.apache.org/download-80.cgi 二.解压 我把解压包解压放在了D盘下,具体的路径是:D:\Java IDE\apache-tomcat-8.0.39 三.配置jdk到tomcat 在tomcat安装目录下的bin目录中有startup.bat和shutdown.bat这两个文件, 都使用记事本打开,在第一行"@echo off"的下一行追加新行

20140220-MySQL的安装(使用zip解压绿色安装方式)

20140220-MySQL的安装(使用zip解压绿色安装方式) 谷歌输入MySQL可以直接找到官网的下载地址.下载绿色版. 选择:MySQL Community Server. 如果你已经安装了MySQL可以先执行: (1)停止MySQL的服务:net stop mysql(不需要分号) (2)移除MySQL的服务:mysqld remove(不需要分号) 安装步骤 1.将压缩包解压,复制到指定位置: 2.添加环境变量: 将以下目录添加到环境变量path中:F:\app\mysql-5.6.1

zip解压及zip炸弹的防御

解压功能验证正常,zip炸弹防御部分还没验证完,后续验证后再确认 private static final int MAX_COUNT = 10000; // 注意,long类型后面要加L private static final long MAX_SIZE = 4L * 1024 * 1024 * 1024; private static final int PATH_LENGTH = 512; /** * zip解压及zip炸弹的防御 * 防御要点:1.校验解压后文件大小 2.校验解压后的条

java对 zip文件的压缩和解压(ant解决中文乱码)

说明: 1.对于压缩的文件,当文件名称是中文时,若使用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)进行压缩,压缩完成后,可以看到压缩包中的文件名称是乱码(文件的内容无乱码),所以使用ANT中的ant.jar中的类(org.apache.tools.zip.ZipEntry; org.apache.tools.zip.ZipOutputStream;)用来解决此问题: 2.解压缩时,如果压缩包中为空,则创建

使用JAVA解压加密的中文ZIP压缩包

近来项目中需要对ZIP压缩包解压,然后将解压后的内容存放到指定的目录下. 该压缩包的特性: 使用标准的zip压缩格式(压缩算法没有深入探究) 压缩包中带有目录并且目录名称是中文 压缩时加了密码 因为jre中自带的java.util.zip.*包不支持中文及加密压缩,所以选择使用zip4j包. 下面是解压的实现代码: 1 public class UnZip { 2 private final int BUFF_SIZE = 4096; 3 4 /* 5 获取ZIP文件中的文件名和目录名 6 */

linux解决zip解压乱码问题,实测可用

第一种方法:(推荐) 软件名:p7zip 7z x -tzip -o{输出的文件夹,此处与-o之间没有空格} 要解压的zip文件 更多使用方法 7z -h 第二种方法: 软件名:unarchiver 简单使用: unar zip-file 解压带有密码的zip文件 unar zip-file -p password 更多用法:请使用手册查看 unar -h lsar -h 原文地址:https://www.cnblogs.com/buinar/p/10867411.html