Java解压多重zip压缩包 -- org.apache.commons.compress.archivers.zip.ZipFile结合ZipArchiveI

Java解压多重zip压缩包

    -- org.apache.commons.compress.archivers.zip.ZipFile结合ZipArchiveInputStream来实现



针对前一篇文章Javazip-- ByteArrayOutputStreamorg.apache.commons.compress打包后的zip文件如何解压的问题。

部分代码如下:



        File f = new File("c:/test.zip");

        ZipFile zf = new ZipFile(f, "gbk");

        File folder = new File("c:/test");

        if (!folder.exists()) {

            folder.mkdirs();

        }



        for (Enumeration<ZipArchiveEntry> files = zf.getEntries(); files.hasMoreElements();) {



            ZipArchiveEntry zae = files.nextElement();

            String zipname = zae.getName();

            if (zipname.endsWith(".zip")) {

                String innerzip = StringUtils.removeEnd(zipname, ".zip");

                File innerfolder = new File(folder + File.separator + innerzip);

                if (!innerfolder.exists()) {

                    innerfolder.mkdirs();

                }

                ZipArchiveInputStream zais = new ZipArchiveInputStream(zf.getInputStream(zae), "GBK", true);

                FileOutputStream fos = null;

                ZipArchiveEntry innerzae = null;

                while ((innerzae = zais.getNextZipEntry()) != null) {

                    fos = new FileOutputStream(folder + File.separator + innerzip + File.separator + innerzae.getName());

                    IOUtils.copy(zais, fos);

                }

                zais.close();

                fos.flush();

                fos.close();

            } else {

                ZipArchiveEntry packinfo = zf.getEntry(zipname);

                String filename = folder + File.separator + zipname;

                FileOutputStream fos = new FileOutputStream(filename);

                InputStream is = zf.getInputStream(packinfo);

                IOUtils.copy(is, fos);

                is.close();

                fos.flush();

                fos.close();

            }

        }

        zf.close();



解包后效果如下图:

 

参考资料:

1、《Java打多重zip压缩包 -- ByteArrayOutputStream结合org.apache.commons.compress来实现》

http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6



2Apache Commons Compress

http://commons.apache.org/compress/



3、本文地址

http://docs.google.com/View?id=ajfmzbdvh8wz_44cbjdbrfn







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值