随笔-46  评论-54  文章-0  trackbacks-0

Java多层目录打包和解压代码(apache commons compress, io, lang)

 

package zip;

 

import java.io.BufferedOutputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import org.apache.commons.compress.archivers.zip.ZipFile;

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang.StringUtils;

 

public class Main {

 

    public static void main(String[] args) throws FileNotFoundException, Exception {

        Main main = new Main("c:/test1.zip");

        main.createZipOut();

        main.packToolFiles("C:/test", "");

        main.closeZipOut();

 

        main = new Main("c:/test2.zip");

        main.createZipOut();

        main.packToolFiles("C:/test", "test");

        main.closeZipOut();

 

        Main.unZipToFolder("c:/test1.zip", "c:/test1");

        

        Main.unZipToFolder("c:/test2.zip", "c:/test2");

    }

    private OutputStream out = null;

    private BufferedOutputStream bos = null;

    private ZipArchiveOutputStream zaos = null;

    private String zipFileName = null;

 

    public Main(String zipname) {

        this.zipFileName = zipname;

    }

 

    public void createZipOut() throws FileNotFoundException, IOException {

        File f = new File(zipFileName);

        out = new FileOutputStream(f);

        bos = new BufferedOutputStream(out);

        zaos = new ZipArchiveOutputStream(bos);

        zaos.setEncoding("GBK");

 

    }

 

    public void closeZipOut() throws Exception {

        zaos.flush();

        zaos.close();

 

        bos.flush();

        bos.close();

 

        out.flush();

        out.close();

    }

 

    /**

     * 把一个目录打包到zip文件中的某目录

     * @param dirpath   目录绝对地址

     * @param pathName  zip中目录

     */

    public void packToolFiles(String dirpath, String pathName) throws FileNotFoundException, IOException {

        if (StringUtils.isNotEmpty(pathName)) {

            pathName = pathName + File.separator;

        }

        packToolFiles(zaos, dirpath, pathName);

    }

 

    /**

     * 把一个目录打包到一个指定的zip文件中

     * @param dirpath           目录绝对地址

     * @param pathName       zip文件抽象地址

     */

    public void packToolFiles(ZipArchiveOutputStream zaos, String dirpath, String pathName) throws FileNotFoundException, IOException {

 

        ByteArrayOutputStream tempbaos = new ByteArrayOutputStream();

        BufferedOutputStream tempbos = new BufferedOutputStream(tempbaos);

 

        File dir = new File(dirpath);

        //返回此绝对路径下的文件

        File[] files = dir.listFiles();

        if (files == null || files.length < 1) {

            return;

        }

        for (int i = 0; i < files.length; i++) {

            //判断此文件是否是一个文件夹

            if (files[i].isDirectory()) {

                packToolFiles(zaos, files[i].getAbsolutePath(), pathName + files[i].getName() + File.separator);

            } else {

                zaos.putArchiveEntry(new ZipArchiveEntry(pathName + files[i].getName()));

                IOUtils.copy(new FileInputStream(files[i].getAbsolutePath()), zaos);

                zaos.closeArchiveEntry();

 

            }

 

        }

 

        tempbaos.flush();

        tempbaos.close();

 

        tempbos.flush();

        tempbos.close();

    }

 

    /**

     * 把一个zip文件解压到一个指定的目录中

     * @param zipfilename   zip文件抽象地址

     * @param outputdir     目录绝对地址

     */

    public static void unZipToFolder(String zipfilename, String outputdir) throws IOException {

        File zipfile = new File(zipfilename);

        if (zipfile.exists()) {

            outputdir = outputdir + File.separator;

            FileUtils.forceMkdir(new File(outputdir));

 

            ZipFile zf = new ZipFile(zipfile, "GBK");

            Enumeration zipArchiveEntrys = zf.getEntries();

            while (zipArchiveEntrys.hasMoreElements()) {

                ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement();

                if (zipArchiveEntry.isDirectory()) {

                    FileUtils.forceMkdir(new File(outputdir + zipArchiveEntry.getName() + File.separator));

                } else {

                    IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(outputdir + zipArchiveEntry.getName())));

                }

            }

        } else {

            throw new IOException("指定的解压文件不存在:\t" + zipfilename);

        }

    }

}

 

 

本文地址:http://docs.google.com/View?id=ajfmzbdvh8wz_60g2dhfkgg

 

posted on 2009-11-12 22:53 rox 阅读(3990) 评论(1)  编辑  收藏 所属分类: Java

评论:
# re: Java多层目录打包和解压代码(apache commons compress, io, lang)[未登录] 2014-07-23 17:06 | s
qwqw  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2014年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(3)

随笔分类(30)

随笔档案(47)

一些达人!

  • chenge's blogs
  • 分享——知识与快乐 pragmatic programmer: programming for fun and money 不管是什么风,欢迎您的到来! 自我介绍 我现在上海某公司担任软件设计师,爱好软件设计,架构和设计模式等。涉及EAI, ERP, SCM, DCM等。 欢迎批评建议,共寻合作机会,共创美好未来!
  • cnJava 的BLOG
  • 又一个Java牛人!
  • Java Security高手,常常会出些题目。
  • Java Security 视野,平台,战略
  • JBoss Rules(Drools) 学习笔记
  • 记录学习JBoss Rules的点点滴滴
  • jini(99% jakarta)的 Blog
  • jini(99% jakarta)雅加達爪哇咖啡台灣總店
  • SenTom,源自“盛唐”!
  • SenTom,源自“盛唐”,致力于Java开源项目在中国的推广,以及文档中文化! SenTom,非营利站点,只希望能为 Java Open Source 社区贡献一点力量! 如果你愿意支持我们: 可以在你的网站为SenTom做一个连接; 可以向我们推荐你知道的Java开源项目; 可以向我们推荐你阅读过的Java开源项目技术文章; 可以向我们提供你撰写的Java开源项目技术文章; 联系email:wayne@sentom.net
  • xheaven的专栏
  • 又一个DB4O的达人!
  • 一个Ruby达人
  • http://ruby-cn.org/的站长,翻译了不少文档了。 牛人一个。
  • 东波'S BLOG
  • 生活,学习,工作的一点看法,想法
  • 利我博客
  • 利我博客,达人,绝对的达人。
  • 孤独键盘手
  • BlogJava-xmatthew
  • 干煸四季豆
  • 给我一支烟,吞云吐雾间,让所有烦累疲劳能够过往如云烟~~~ 我的能力是协助团队取得成功并尽快掌握新知识。
  • 成都心情
  • 成都心情 技术本身没有优良,只有不良的设计和应用。 本 Blog 是从:http://blog.csdn.net/rosen 搬来。
  • 陈刚的BLOG
  • 子在川上曰 逝者如斯夫不舍昼夜

优秀个人

优秀资源

搜索

  •  

最新评论

阅读排行榜

评论排行榜