java利用Ant解压指定zip压缩包到指定文件夹

来源:互联网 发布:网络通俗歌手大赛冠军 编辑:程序博客网 时间:2024/05/23 11:23

上一篇介绍了java利用ant压缩指定文件夹:地址是这里:http://blog.csdn.net/zp357252539/article/details/51712467,
本文则继续完成把压缩包解压的功能:说明下,关于压缩包的后缀名有很多,不同的后缀名的压缩算法可能不同,那么解压或压缩的方式也不尽相同,本文和上一篇介绍的都是基于常见的zip格式进行压缩和解压缩。

本文则继续说关于java利用ant来解压zip包的例子:
还是先上代码结构:
这里写图片描述
我们要解压的压缩包位置是这里:D:\test1\zip.zip,如图:
这里写图片描述
要解压的位置是这里:D:\test,如图:
这里写图片描述
我们可以看到当前要解压的位置D:\test目录下还没有数据,

接下来我们执行主类:UnZipCompressorByAnt.java

package com.acconsys.vpm.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;public class UnZipCompressorByAnt {    private static final int BUFFEREDSIZE = 1024;    /**     * 解压缩     *      * @param zipFilePath     *            压缩包路径     * @param fileSavePath     *            解压路径     * @param isDelete     *            是否删除源文件     * @throws Exception     */    public void unZip(String zipFilePath, String fileSavePath, boolean isDelete)            throws Exception {        try {            (new File(fileSavePath)).mkdirs();            File f = new File(zipFilePath);            if ((!f.exists()) && (f.length() <= 0)) {                throw new Exception("要解压的文件不存在!");            }            ZipFile zipFile = new ZipFile(f);            String strPath, gbkPath, strtemp;            File tempFile = new File(fileSavePath);// 从当前目录开始            strPath = tempFile.getAbsolutePath();// 输出的绝对位置            Enumeration<ZipEntry> e = zipFile.getEntries();            while (e.hasMoreElements()) {                org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();                gbkPath = zipEnt.getName();                if (zipEnt.isDirectory()) {                    strtemp = strPath + File.separator + gbkPath;                    File dir = new File(strtemp);                    dir.mkdirs();                    continue;                } else {                    // 读写文件                    InputStream is = zipFile.getInputStream(zipEnt);                    BufferedInputStream bis = new BufferedInputStream(is);                    gbkPath = zipEnt.getName();                    strtemp = strPath + File.separator + gbkPath;                    // 建目录                    String strsubdir = gbkPath;                    for (int i = 0; i < strsubdir.length(); i++) {                        if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {                            String temp = strPath + File.separator                                    + strsubdir.substring(0, i);                            File subdir = new File(temp);                            if (!subdir.exists())                                subdir.mkdir();                        }                    }                    FileOutputStream fos = new FileOutputStream(strtemp);                    BufferedOutputStream bos = new BufferedOutputStream(fos);                    int len;                    byte[] buff = new byte[BUFFEREDSIZE];                    while ((len = bis.read(buff)) != -1) {                        bos.write(buff, 0, len);                    }                    bos.close();                    fos.close();                }            }        } catch (Exception e) {            e.printStackTrace();            throw e;        }        if (isDelete) {            new File(zipFilePath).delete();        }    }    public static void main(String[] args) {        UnZipCompressorByAnt cpr = new UnZipCompressorByAnt();        try {            cpr.unZip("D:\\test1\\zip.zip", "D:\\test", false);        } catch (Exception e) {            e.printStackTrace();        }    }}

执行结果完成后,我们查看D:\test目录下,如下图:
这里写图片描述

我们查看压缩包zip.zip里的内容,发现是和解压之后完全一样的:如图:
这里写图片描述

注意问题:

  1. 乱码的问题,如果发现有这方面的问题@我,或者自己解决吧。

如果要获取代码或jar,戳这里吧:http://pan.baidu.com/s/1o79O4cE

0 0
原创粉丝点击