隨筆-46  評(píng)論-54  文章-0  trackbacks-0

          Java解壓多重zip壓縮包

              -- org.apache.commons.compress.archivers.zip.ZipFile結(jié)合ZipArchiveInputStream來(lái)實(shí)現(xiàn)

          針對(duì)前一篇文章Javazip-- ByteArrayOutputStream結(jié)org.apache.commons.compress來(lái)實(shí)現(xiàn)打包后的zip文件如何解壓的問(wèn)題。

          部分代碼如下:



                  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結(jié)合org.apache.commons.compress來(lái)實(shí)現(xiàn)》

          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







           

          posted on 2009-07-28 19:02 rox 閱讀(6667) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 榆林市| 淮阳县| 乌苏市| 佛山市| 隆林| 灵宝市| 西城区| 共和县| 永修县| 昭通市| 鲜城| 封丘县| 双流县| 和顺县| 柘荣县| 大足县| 顺昌县| 且末县| 称多县| 江口县| 昌都县| 新丰县| 南昌县| 普陀区| 玉屏| 新龙县| 安福县| 内丘县| 九寨沟县| 瑞昌市| 申扎县| 青浦区| 化州市| 苏尼特左旗| 进贤县| 东乡县| 汝南县| 贵州省| 东乌| 灯塔市| 永嘉县|