博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UnzipUtil
阅读量:4495 次
发布时间:2019-06-08

本文共 2433 字,大约阅读时间需要 8 分钟。

public class UnzipUtil {    private static final Logger logger = LoggerFactory.getLogger(CopyFileUtil.class);    /**     * Size of the buffer to read/write data     */    private static final int BUFFER_SIZE = 4096;    /**     * Extracts a zip file specified by the zipFilePath to a directory specified     * by destDirectory (will be created if does not exists)     *     * @param zipFilePath     * @param destDirectory     * @throws IOException     */    public static void unzip(String zipFilePath, String destDirectory) {        File destDir = new File(destDirectory);        if (!destDir.exists()) {            boolean mkdirs = destDir.mkdirs();            if (!mkdirs) {                logger.error("Call UnzipUtility.unzip,destDir mkdirs is false");            }        }        try {            ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));            ZipEntry entry = zipIn.getNextEntry();            // iterates over entries in the zip file            while (entry != null) {                String filePath = destDirectory + File.separator + entry.getName();                if (!entry.isDirectory()) {                    // if the entry is a file, extracts it                    extractFile(zipIn, filePath);                } else {                    // if the entry is a directory, make the directory                    File dir = new File(filePath);                    boolean mkdirs = dir.mkdirs();                    if (!mkdirs) {                        logger.error("Call UnzipUtility.unzip,dir mkdirs is false");                    }                }                zipIn.closeEntry();                entry = zipIn.getNextEntry();            }            zipIn.close();        } catch (IOException e) {            logger.warn("call UnzipUtility.unzip, occur exception. e.getMessage:[{}]", e.getMessage());        }    }    /**     * Extracts a zip entry (file entry)     *     * @param zipIn     * @param filePath     * @throws IOException     */    public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));        byte[] bytesIn = new byte[BUFFER_SIZE];        int read = 0;        while ((read = zipIn.read(bytesIn)) != -1) {            bos.write(bytesIn, 0, read);        }        bos.close();    }}

 

转载于:https://www.cnblogs.com/yanmingzhang/p/10334710.html

你可能感兴趣的文章
unix系统内核优点
查看>>
协议(五)-从电报机到智能手机
查看>>
Solr学习
查看>>
HDU-4628 Pieces 搜索 | DP
查看>>
动态代理
查看>>
蓝瘦香菇
查看>>
关于数组和List之间相互转换的方法
查看>>
Hybrid开发站点
查看>>
2014-软件工程基础-总结
查看>>
[linux]segvcatch简单使用
查看>>
webpack之傻瓜式教程及前端自动化入门
查看>>
Python学习-5.Python的变量与数据类型及字符串的分割与连接
查看>>
【TypeScript】TypeScript 学习 2——接口
查看>>
Failed to sync Gradle project 'XX'错误解决
查看>>
vue-router 重难点总结笔记
查看>>
GDI+绘图
查看>>
团队项目冲刺第七天
查看>>
数据库的持续集成和版本控制
查看>>
nginx反向代理nginx,RealServer日志打印真实ip
查看>>
Visual Studio蛋疼问题解决(1)
查看>>