文件压缩java使用Apache的ant压缩和解压文件(zip)

改章节是一篇关于文件压缩的帖子

    用调ant里的方法可以齐备的实现包含中文在内的zip的压缩与解压

    体具实现代码如下:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

import java.util.zip.ZipOutputStream;

import org.apache.tools.ant.Project;

import org.apache.tools.ant.taskdefs.Expand;

import org.apache.tools.zip.ZipEntry;



/**

 *

 * @author yaohucaizi

 */

public class ZipUtil {



    /**

     *

     * @param file 要压缩的文件

     * @param zipFile 压缩文件放存方地

     */

    public static void zip(File file, File zipFile) {

        ZipOutputStream outputStream = null;

        try {

            outputStream = new ZipOutputStream(new FileOutputStream(zipFile));

            zipFile(outputStream, file, "");

            if (outputStream != null) {

                outputStream.flush();

                outputStream.close();

            }

        } catch (IOException ex) {

            Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);

        } finally {

            try {

                outputStream.close();

            } catch (IOException ex) {

                Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);

            }

        }

    }



    /**

     *

     * @param output ZipOutputStream对象

     * @param file 要压缩的文件或文件夹

     * @param basePath 条目根录目

     */

    private static void zipFile(ZipOutputStream output, File file, String basePath) {

        FileInputStream input = null;

        try {

            // 文件为录目

            if (file.isDirectory()) {

                // 失掉当前录目面里的文件表列

                File list[] = file.listFiles();

                basePath = basePath + (basePath.length() == 0 ? "" : "/")

                        + file.getName();

                // 环循递归压缩每一个文件

                for (File f : list) {

                    zipFile(output, f, basePath);

                }

            } else {

                // 压缩文件

                basePath = (basePath.length() == 0 ? "" : basePath + "/")

                        + file.getName();

                // System.out.println(basePath);

                output.putNextEntry(new ZipEntry(basePath));

                input = new FileInputStream(file);

                int readLen = 0;

                byte[] buffer = new byte[1024 * 8];

                while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {

                    output.write(buffer, 0, readLen);

                }

            }

        } catch (Exception ex) {

            ex.printStackTrace();

        } finally {

            // 关闭流

            if (input != null) {

                try {

                    input.close();

                } catch (IOException ex) {

                    Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);

                }

            }

        }

    }



    /**

     *

     * @param sourceZip 待解压文件路径

     * @param destDir 解压到的路径

     */

    public static void unZip(String sourceZip, String destDir) {

        //保障文件夹路径最后是"/"或者"\"     

        char lastChar = destDir.charAt(destDir.length() - 1);

        if (lastChar != '/' && lastChar != '\\') {

            destDir += File.separator;

        }

        Project p = new Project();

        Expand e = new Expand();

        e.setProject(p);

        e.setSrc(new File(sourceZip));

        e.setOverwrite(false);

        e.setDest(new File(destDir));

        /*   

         ant下的zip工具默许压缩码编为UTF-8码编,   

         而winRAR软件压缩是用的windows默许的GBK或者GB2312码编   

         所以解压缩时要定制码编格式   

         */

        e.setEncoding("gbk");

        e.execute();

    }



    public static void main(String[] args) {

        String sourcePath = "C:/model.zip";

        String destPath = "C:/test";

        unZip(sourcePath, destPath);

        zip(new File("C:/test/model"), new File("d:/model.zip"));

    }

}
    每日一道理
爱,有的时候不需要山盟海誓的承诺,但她一定需要细致入微的关怀与问候;爱,有的时候不需要梁祝化蝶的悲壮,但她一定需要心有灵犀的默契与投合;爱,有的时候不需要雄飞雌从的追随,但她一定需要相濡以沫的支持与理解。

    以上代码可即实现zip文件的压缩与解压,此程过所需jar包点击链接载下         载下链接

    

文章结束给大家分享下程序员的一些笑话语录: 程序员打油诗   
  写字楼里写字间,写字间里程序员;
  程序人员写程序,又拿程序换酒钱。
  酒醒只在网上坐,酒醉还来网下眠;
  酒醉酒醒日复日,网上网下年复年。
  但愿老死电脑间,不愿鞠躬老板前;
  奔驰宝马贵者趣,公交自行程序员。
  别人笑我忒疯癫,我笑自己命太贱;
  不见满街漂亮妹,哪个归得程序员。

你可能感兴趣的:(apache)