java压缩包解压后返回路径(这是二级压缩处理,一级压缩的删减代码后可以用)

    解压Zip

 /**
     * 解压Zip文件
     * @param path 文件目录
     * @author wsj
     */
    public static UnZipFilePaths unZip(String path) {
        String savePath;
        File file;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        //保存要解压的文件目录
        savePath = path.substring(0, path.lastIndexOf(".")) + File.separator;
        //创建保存 解压的总目录
        new File(savePath).mkdir();
        ZipFile zipFile = null;
        UnZipFilePaths unZipFilePaths = new UnZipFilePaths();
        UnZipFilePaths unZipFilePaths1 = new UnZipFilePaths();
        List<String> firstFolderPaths = new ArrayList<>();
        List<String> secondFolderPaths = new ArrayList<>();
        try {
            //解决中文乱码问题
            zipFile = new ZipFile(path,"gbk");
            Enumeration<?> entries = zipFile.getEntries();
            while(entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry)entries.nextElement();
                //解压的一个内容的名称
                String oneFilename = entry.getName();
                boolean ismkdir = false;
                //检查此文件是否带有文件夹
                if(oneFilename.lastIndexOf("/") != -1){
                    ismkdir = true;
                }
                //将要存的绝对文件路径
                String oneFilePath = savePath + oneFilename;
                //如果是文件夹先创建
                if(entry.isDirectory()){
                    file = new File(oneFilePath);
                    file.mkdirs();
                    continue;
                }
                //如果是压缩包 调用头方法
//                if(entry.getName().matches(".*.zip")){
//                    System.out.println("是zip文件!");
//                    throw new ServiceException("是zip文件");
//                }
                file = new File(oneFilePath);
                //如果是目录先创建,如果不是目录而是文件则
                if(!file.exists()){
                    if(ismkdir){
                        //目录先创建
                        new File(oneFilePath.substring(0, oneFilePath.lastIndexOf("/"))).mkdirs();
                    }
                }
                //创建文件 的空壳
                file.createNewFile();
                //流
                is = zipFile.getInputStream(entry);
                //往空壳里填流
                FileUtils.copyInputStreamToFile(is, file);
                is.close();
                //如果解压的还是zip文件继续解压,之后删除源文件。
                if (entry.toString().toLowerCase().endsWith(".zip")) {
                    //二级文件的一级文件路径集合
                    unZipFilePaths1 = ZipUtils.unZip(oneFilePath);
                    file.delete();
                }else {
                    //一级文件路径集合
                    firstFolderPaths.add(oneFilePath);
                }
            }
            zipFile.close();
        }catch(IOException ioe){
            ioe.printStackTrace();
            throw new ServiceException("解压失败!");
        }finally{
            try{
                if(is != null){
                    is.close();
                }
                if(zipFile != null){
                    zipFile.close();
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
        unZipFilePaths.setFirstFolderPaths(firstFolderPaths);
        if (unZipFilePaths1.getFirstFolderPaths() != null) {
            unZipFilePaths.setSecondFolderPaths(unZipFilePaths1.getFirstFolderPaths());
        }
        return unZipFilePaths;
    }

 @Data
    public static class UnZipFilePaths{

        UnZipFilePaths(){}

        private List<String> firstFolderPaths;

        private List<String> secondFolderPaths;
    }

UnZipFilePaths 为返回的类中包装的类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值