Java进行文件的解压,包含多个子文件

 /**
     * 解压到指定目录
     * @param zipPath
     * @param descDir
     */
    public static Map<String,String> unZipFiles(String zipPath,String descDir)throws IOException{
        return unZipFiles(new File(zipPath), descDir);
    }
    /**
     * 解压文件到指定目录
     * @param zipFile
     * @param descDir
     */
    @SuppressWarnings("rawtypes")
    public static Map<String,String> unZipFiles(File zipFile,String descDir)throws IOException{
        Map<String,String> zipMap = new HashMap<>();
        File pathFile = new File(descDir);
        if(!pathFile.exists()){
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile);
        for(Enumeration entries = zip.entries();entries.hasMoreElements();){
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;
            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if(!file.exists()){
                file.mkdirs();
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory()){
                continue;
            }
            //输出文件路径信息
            System.out.println(outPath);
          
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0){
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
        }
        System.out.println("******************解压完毕********************");
        return  zipMap;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以使用Java的ZipInputStream和ZipEntry类来解压多层zip包,并且按照解压前的文件进行保存。以下是示例代码: ```java import java.io.*; import java.util.zip.*; public class UnzipMultipleLayers { public static void main(String[] args) throws IOException { String inputFile = "input.zip"; String outputDir = "output"; unzipMultipleLayers(inputFile, outputDir); } public static void unzipMultipleLayers(String inputFile, String outputDir) throws IOException { byte[] buffer = new byte[1024]; ZipInputStream zip = new ZipInputStream(new FileInputStream(inputFile)); ZipEntry entry = zip.getNextEntry(); while (entry != null) { String fileName = entry.getName(); File newFile = new File(outputDir + File.separator + fileName); if (entry.isDirectory()) { newFile.mkdirs(); } else { new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zip.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } zip.closeEntry(); entry = zip.getNextEntry(); } zip.close(); } } ``` 在上面的代码中,`unzipMultipleLayers`方法接收一个zip文件名和一个输出目录作为参数,并解压所有zip文件到指定目录中。如果zip包中包含多层zip文件,则会递归解压。`ZipInputStream`类被用来读取zip文件,`ZipEntry`类被用来表示zip文件中的条目。在解压过程中,我们首先检查当前条目是否为目录,如果是,则创建相应的目录;否则,我们创建一个新文件并将当前条目的内容写入该文件中。最后,我们关闭当前条目的输入流并获取下一个条目,如此循环直到所有条目被处理完毕。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨迹嘿嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值