zip解压缩方式

解压缩类(在导包的时候,应添加ant.jar解压缩包)直接贴代码:

public class ZipUtil {
    private static ZipUtil instance;
    private ZipUtil() {
    }
    public static ZipUtil getInstance() {
        if (instance == null) {
            synchronized (ZipUtil.class) {
                if (instance == null) {
                    instance = new ZipUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 解压缩zip包
     *
     * @param zipFilePath zip文件路径
     * @param targetPath  解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
     * @throws IOException
     * @author huangxianfeng
     */
    public void unZip(String zipFilePath, String targetPath)
            throws IOException {
        OutputStream os = null;
        InputStream is = null;
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(zipFilePath);
            String directoryPath = "";
            if (null == targetPath || "".equals(targetPath)) {
                directoryPath = zipFilePath.substring(0, zipFilePath
                        .lastIndexOf("."));
            } else {
                directoryPath = targetPath;
            }
            @SuppressWarnings("rawtypes")
            Enumeration entryEnum = zipFile.entries();
            if (null != entryEnum) {
                ZipEntry zipEntry = null;
                while (entryEnum.hasMoreElements()) {
                    zipEntry = (ZipEntry) entryEnum.nextElement();
                    if (zipEntry.getSize() > 0) {
                        // 文件
                        File targetFile = FileUtil.getInstance().buildFile(directoryPath
                                + File.separator + zipEntry.getName(), false);
                        os = new BufferedOutputStream(new FileOutputStream(targetFile));
                        is = zipFile.getInputStream(zipEntry);
                        byte[] buffer = new byte[4096];
                        int readLen = 0;
                        while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
                            os.write(buffer, 0, readLen);
                        }

                        os.flush();
                        os.close();
                    }
                    if (zipEntry.isDirectory()) {
                        String pathTemp = directoryPath + File.separator
                                + zipEntry.getName();
                        File file = new File(pathTemp);
                        file.mkdirs();
                        System.out.println(pathTemp);
//	                        continue;
                    }
                }
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (null != zipFile) {
                zipFile = null;
            }
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.close();
            }
        }
    }

    public static class FileUtil {

        public static FileUtil instance;


        public FileUtil() {

        }

        public static FileUtil getInstance() {
            if (instance == null) {
                synchronized (FileUtil.class) {
                    if (instance == null) {
                        instance = new FileUtil();
                    }
                }
            }
            return instance;
        }


        public File buildFile(String fileName, boolean isDirectory) {
            File target = new File(fileName);
            if (isDirectory) {
                target.mkdirs();
            } else {
                if (!target.getParentFile().exists()) {
                    target.getParentFile().mkdirs();
                    target = new File(target.getAbsolutePath());
                }
            }
            return target;
        }
    }


    public int streamLen;

    /**
     * @描述:把raw文件夹下压缩包存储到SD卡中
     * @方法名:&unZip
     * @创建时间:&2015-9-16 &上午10:10:43
     */
    public void downZip(InputStream inputStream, String filePath) {
        int readCount = 0;
        FileOutputStream output;
        try {
            output = new FileOutputStream(filePath);
            BufferedInputStream b = new BufferedInputStream(inputStream);
            int len = 2048;
            byte[] buffer = new byte[len];
            while ((readCount = b.read(buffer)) != -1) {
                output.write(buffer, 0, readCount);
            }
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("e.getMessage==" + e.getMessage());
        }
    }
}

查看及获取源代码:
http://blog.csdn.net/huang3513/article/details/53032469


博客声明
欢迎转载博客,但请保留文章原始出处
作者:定陶黄公子
出处:http://blog.csdn.net/huang3513/article/details/53032469

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值