android 解压缩zip文件并取出所有文件

1.选择某个zip文件后,在zip文件的目录创建一个与zip文件同名的文件夹,并在该文件夹下解压缩
因为在windows和在mac下压缩所采用的编码不一样,有可能导致中文名解压缩出来出现乱码。解压缩windows zip包需要设置编码为GBK,解压缩mac zip包需要设置编码为UTF-8。一开始时默认解压缩编码为GBK,若zip包为mac压缩,则转换成UTF-8重新解压缩。能解决大部分的中文名乱码问题,但若是在windows下压缩的mac的文件,或者是在mac下压缩的windows的文件,则无法解决额。
public void unZipFile(Context context, String filePath) {
        String absFilePath = filePath.substring(0, filePath.lastIndexOf("/"));
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
        String folderPath = absFilePath + File.separator + fileName;
        // LogUtil.i("yangqiuyan", absFilePath + " : " + fileName + " : " + folderPath + " ??");
        File zipDir = new File(folderPath);
        if (!zipDir.exists()) {
            zipDir.mkdir();
        }
        if (!unZipFilebyEcode(filePath, folderPath, "GBK")) {
            unZipFilebyEcode(filePath, folderPath, "UTF-8");
        }
        Intent intent = new Intent(context, ShowUnZipFile.class);
        intent.putExtra("filepath",folderPath);
        context.startActivity(intent);
}
若编码不同,则不会进入while循环,以此来判断是否选择了正确的解压缩编码。
private boolean unZipFilebyEcode(String filePath, String folderPath, String ecode) {
        boolean isUnZipSuccess = false;
        try {
            // ZipFile zf = new ZipFile(filePath);
            ZipFile zf = new ZipFile(filePath, ecode);
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry) e.nextElement();
                String entryName = ze2.getName();
                String path = folderPath + "/" + entryName;;
                if (ze2.isDirectory()) {
                    File decompressDirFile = new File(path);
                    if (!decompressDirFile.exists()) {
                        // LogUtil.i("yangqiuyan", path + "  mkdir");
                        decompressDirFile.mkdirs();
                    }
                } else {
                    String fileDir = path.substring(0, path.lastIndexOf("/"));
                    File fileDirFile = new File(fileDir);
                    if (!fileDirFile.exists()) {
                        fileDirFile.mkdirs();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
                    BufferedInputStream bi = new BufferedInputStream(zf.getInputStream(ze2));
                    byte[] readContent = new byte[1024];
                    int readCount = bi.read(readContent);
                    while (readCount != -1) {
                        bos.write(readContent, 0, readCount);
                        readCount = bi.read(readContent);
                    }
                    bi.close();
                    bos.close();
                }
                isUnZipSuccess = true;
            }
            zf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isUnZipSuccess;
    }
2.解压完后,根据路径,取得该文件夹下所有文件
public List<FileItem> getAllFileByPath(String folderPath) {
<span style="white-space:pre">	</span>// FileItem为自己定义的文件类
        List<FileItem> tempList = new ArrayList<FileItem>();
        if (TextUtils.isEmpty(folderPath)) {
            return tempList;
        }
        File file = new File(folderPath);
        if (!file.exists()) {
            return tempList;
        }
        File[] files = file.listFiles(); //取出该路径下所有文件
        if (files != null && files.length > 0) {
            Arrays.sort(files, new FileComparatorByTimeDes()); // 文件排序
            for (int i = 0; i < files.length; i++) {
                FileItem fileItem = new FileItem();
                fileItem.filePath = files[i].getAbsolutePath();
                fileItem.fileName = files[i].getName();
                if (files[i].isDirectory()) {
                    fileItem.isDirectory = true;
                } else {
                    fileItem.isDirectory = false;
                    FILE_TYPE type = FileTypeUtil.getFileType(FileUtil.getFileType(fileItem.filePath));
                    fileItem.fileType = type;
                    try {
                        fileItem.fileSize = FileUtil.getFileSize(fileItem.filePath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                tempList.add(fileItem);
            }
        }
        return tempList;
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值