ZipEntry引起的ENOENT (No such file or directory)

ZipEntry引起的ENOENT (No such file or directory)

最近做一个功能,里面有解压缩的功能,遇到一个坑,报错如题目,记录分享出来供大家参考;
网上搜到的解压缩的代码基本就是这样的:

        try {
            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();
                /**这里提示如果当前元素是文件夹时,在目录中创建对应文件夹
                 * ,如果是文件,得出路径交给下一步处理*/
                entryFilePath = unzipFilePath + File.separator + entry.getName();
                File file = new File(entryFilePath);
                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();
                AILog.d(TAG,"unzip  end");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

这里面有个坑,遇到这样的多级目录下的文件:
在这里插入图片描述

解压的c.text的时候就会报错;entry是每个实体文件的路径,如果含有多级目录文件,就会爆出 如题目的错误,然后百度错误,大部分是要加权限问题;但是这里的真正问题是,创建文件的时候,多级目录是new不出来的,解决办法如下,全部代码:

public static void unzip(String zipFilePath, String unzipFilePath) {
        
        /**验证是否为空*/
        if (TextUtils.isEmpty(zipFilePath) || TextUtils.isEmpty(unzipFilePath)) {
            return;
        }
        try {
            File zipFile = new File(zipFilePath);
            /**创建解压缩文件保存的路径*/
            File unzipFileDir = new File(unzipFilePath);
            if (!unzipFileDir.exists()) {
                File parent = unzipFileDir.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();  //创建所有父文件夹
                }
                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();
                String entryName = entry.getName();
                String[] fileDirs = entryName.split("\\/");
                if (fileDirs.length > 0) {
                    String topPath = unzipFilePath;
                    for (int i = 0; i < fileDirs.length-1; i++) {
                        topPath += "/" + fileDirs[i];
                        File file1 = new File(topPath);
                        AILog.d(TAG,"topPath:"+topPath);
                        if (file1.exists()) {
                            continue;
                        } else {
                            file1.mkdir();
                        }
                    }
                }

                /**这里提示如果当前元素是文件夹时,在目录中创建对应文件夹
                 * ,如果是文件,得出路径交给下一步处理*/
                entryFilePath = unzipFilePath + File.separator + entry.getName();
                File file = new File(entryFilePath);
                AILog.d(TAG, "entryFilePath::" + entryFilePath);

                if (entryFilePath.endsWith("/")) {
                    if (!file.exists()) {
                        File parent = file.getParentFile();
                        if (!parent.exists()) {
                            parent.mkdirs();  //创建所有父文件夹
                        }
                        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();
                AILog.d(TAG, "unzip  end");

                copyFolder(unzipFilePath, "/ivres");

            }
        } catch (Exception e) {
            AILog.d(TAG, e.getMessage() + "::" + e.getStackTrace() + "::" + e.getCause());
            e.printStackTrace();
        }

        //解压完成后,删除压缩包文件(此处根据需要可进行删除)
        File oldFile = new File(zipFilePath);
        oldFile.delete();
        deletePath(unzipFilePath);

        AILog.d(TAG,"unzip and copy success");
    }

核心代码是这里:
在这里插入图片描述
循环创建分级目录文件夹。

如果你也遇到相同问题,拿去不谢,可以点赞收藏一下。转载请注明出处,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值