解压zip压缩文件,Java

这篇博客详细介绍了如何使用Java通过ZipFile API解压特定zip文件,包括创建目录结构和写入文件内容,适用于初学者和开发者参考。

解压zip压缩文件,Java


    //解压
    public static void decompress(String srcPath, String destPath) throws Exception {
        File file = new File(srcPath);
        if (!file.exists()) {
            throw new RuntimeException(srcPath + "文件不存在");
        }

        ZipFile zf = new ZipFile(file);
        Enumeration entries = zf.entries();
        ZipEntry entry;
        while (entries.hasMoreElements()) {
            entry = (ZipEntry) entries.nextElement();
            //非文件
            if (entry.isDirectory()) {
                String dirPath = destPath + File.separator + entry.getName();
                File dir = new File(dirPath);
                dir.mkdirs();
            } else {
                //文件
                File f = new File(destPath + File.separator + entry.getName());
                if (!f.exists()) {
                    f.createNewFile();
                }

                // 文件数据写入文件
                InputStream is = zf.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(f);

                int count;
                byte[] buf = new byte[1024 * 4];
                while ((count = is.read(buf)) != -1) {
                    fos.write(buf, 0, count);
                }

                is.close();
                fos.close();
            }
        }
    }

传入一个压缩文件(srcPath),然后将压缩文件的内容(文件,目录)递归的解压到目的路径(destPath)下重建文件层次结构。

解压特定zip压缩文件中特定文件,Java_zhangphil的博客-优快云博客https://zhangphil.blog.youkuaiyun.com/article/details/125522938?spm=1001.2014.3001.5502

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangphil

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

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

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

打赏作者

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

抵扣说明:

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

余额充值