---
title: "Java解压多重zip压缩包 -- org.apache.commons.compress.archivers.zip.ZipFile结合ZipArchiveI"
author: "rox"
date: "2009-07-28T19:06:15Z"
tags: ["Apache", "C", "C++", "Google", "Java"]
categories: ["编程语言"]
slug: "java-zip-org-apache-commons-compress-archivers-zip-zipfile-ziparchivei"
draft: false
lastmod: "2009-07-28T19:06:15Z"
id: 434813
url: "https://rox.iteye.com/blog/434813"
---

**Java解压多重zip压缩包**

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

针对前一篇文章[《](http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6)[**Java打多重zip压缩包**](http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6)[-- ByteArrayOutputStream结合org.apache.commons.compress来实现》](http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6)打包后的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&lt;ZipArchiveEntry&gt; 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();

解包后效果如下图：

![](http://docs.google.com/File?id=ajfmzbdvh8wz_45cnbf2tg2_b)

 

参考资料：

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

[http://docs.google.com/View?id=ajfmzbdvh8wz\_38ftxv59f6](http://docs.google.com/View?id=ajfmzbdvh8wz_38ftxv59f6)

2、Apache Commons Compress

[http://commons.apache.org/compress/](http://commons.apache.org/compress/)

3、本文地址

[http://docs.google.com/View?id=ajfmzbdvh8wz\_44cbjdbrfn](http://docs.google.com/View?id=ajfmzbdvh8wz_44cbjdbrfn)