对zip压缩包的解压是比较常见的应用场景,java代码的实现也很简单。废话不多说,直接上代码吧
一、代码
	/**
* zip解压
* @param srcFile zip源文件
* @param destDirPath 解压后的目标文件夹
* @throws RuntimeException 解压失败会抛出运行时异常
*/
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
System.out.println("解压完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
 
1
    /**
2
     * zip解压  
3
     * @param srcFile        zip源文件
4
     * @param destDirPath     解压后的目标文件夹
5
     * @throws RuntimeException 解压失败会抛出运行时异常
6
     */
7
    public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
8
        long start = System.currentTimeMillis();
9
        // 判断源文件是否存在
10
        if (!srcFile.exists()) {
11
            throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
12
        }
13
        // 开始解压
14
        ZipFile zipFile = null;
15
        try {
16
            zipFile = new ZipFile(srcFile);
17
            Enumeration<?> entries = zipFile.entries();
18
            while (entries.hasMoreElements()) {
19
                ZipEntry entry = (ZipEntry) entries.nextElement();
20
                System.out.println("解压" + entry.getName());
21
                // 如果是文件夹,就创建个文件夹
22
                if (entry.isDirectory()) {
23
                    String dirPath = destDirPath + "/" + entry.getName();
24
                    File dir = new File(dirPath);
25
                    dir.mkdirs();
26
                } else {
27
                    // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
28
                    File targetFile = new File(destDirPath + "/" + entry.getName());
29
                    // 保证这个文件的父文件夹必须要存在
30
                    if(!targetFile.getParentFile().exists()){
31
                        targetFile.getParentFile().mkdirs();
32
                    }
33
                    targetFile.createNewFile();
34
                    // 将压缩文件内容写入到这个文件中
35
                    InputStream is = zipFile.getInputStream(entry);
36
                    FileOutputStream fos = new FileOutputStream(targetFile);
37
                    int len;
38
                    byte[] buf = new byte[BUFFER_SIZE];
39
                    while ((len = is.read(buf)) != -1) {
40
                        fos.write(buf, 0, len);
41
                    }
42
                    // 关流顺序,先打开的后关闭
43
                    fos.close();
44
                    is.close();
45
                }
46
            }
47
            long end = System.currentTimeMillis();
48
            System.out.println("解压完成,耗时:" + (end - start) +" ms");
49
        } catch (Exception e) {
50
            throw new RuntimeException("unzip error from ZipUtils", e);
51
        } finally {
52
            if(zipFile != null){
53
                try {
54
                    zipFile.close();
55
                } catch (IOException e) {
56
                    e.printStackTrace();
57
                }
58
            }
59
        }
60
    }

二、小结
    解压的代码并不复杂,不过有个关键点是:创建文件时,需要保证该文件所在的文件夹必须存在。
    如果对Java实现zip压缩感兴趣,可看我上一篇博客:http://www.cnblogs.com/zeng1994/p/7862288.html

Java实现Zip压缩包解压的更多相关文章

  1. Java实现zip文件解压[到指定目录]

    2019独角兽企业重金招聘Python工程师标准>>> package com.ljheee.ziptool.core; import java.io.File; import ja ...

  2. Mac系统下的zip压缩包解压到Windows下出现乱码的解决方法

    环境变量 环境变量是具有特殊名字的一个特定对象,包含了一个或多个应用程序运行所需的信息.(例如PATH,可执行程序的搜索路径,当要求系统运行一个程序,而没告诉系统它的具体路径时,系统就要在PTAH值的 ...

  3. Java压缩包解压到指定文件

    在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...

  4. linux下zip文件解压后乱码解决方案

    解决办法一,利用pyton来处理 1.vi uzip文件2.复制一下内容(Python) #!/usr/bin/env python # -*- coding: utf-8 -*- # uzip.py ...

  5. Linux下的压缩(zip)解压(unzip)缩命令

      .zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip命令 unzip -o ...

  6. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

  7. 把自解压的RAR压缩包解压到指定的软件安装目录

    原文 把自解压的RAR压缩包解压到指定的软件安装目录 今天千里独行同学给轻狂来信问了一个问题:如何把一个自解压的RAR压缩包解压到我们指定的软件安装目录.   其实,在NSIS中,我们可以灵活运用相关 ...

  8. ZIP文件解压

    public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...

  9. linux查看文件夹大小,备份文件夹zip压缩解压

    linux查看文件夹大小,备份文件夹zip压缩解压 du -sh : 查看当前目录总共占的容量.而不单独列出各子项占用的容量 du -lh --max-depth=1 : 查看当前目录下一级子文件和子 ...

随机推荐

  1. Loadrunner脚本优化-参数化之关联MySQL数据库获取数据

    脚本优化-参数化之关联MySQL数据库获取数据 by:授客 QQ:1033553122 测试环境: Loadrunner 11 Win7 64位 实操: 1.   安装MySQL ODBC驱动程序 O ...

  2. Android系统执行Java jar程序 -- dalvik运行dex Java工程

    本文仅针对纯java工程执行进行诠释,一般在PC平台作为jar包形式存在,在Android平台则以dex包形式存在. Java属于高级程序语言,Java程序需要运行在特定的虚拟机中,虚拟机将Java字 ...

  3. mybatis-generator 详细配置及使用,爬坑记录

    mybatis-generator 详细配置及使用,爬坑记录 提示:如果不成功一定是项目路径和 数据库配置出问题,本篇基于 MySQL 8.0.13,调试没有问题. 如果失败,建议使用相同的项目结构, ...

  4. HDFS hflush hsync和close的区别

    HDFS的hflush,hsync和close有啥区别,分别做了什么 hflush: 语义是保证flush的数据被新的reader读到,但是不保证数据被datanode持久化. hsync: 与hfl ...

  5. crm lookup

    1. 大家都知道CRM 里面的Lookup 保存了相关实体的GUID,让我们深入的了解一下CRM Lookup.当我们在2个实体间建立关系的时候,CRM自动生成了一些attributes来保存相关实体 ...

  6. entityFramework 中decimal精度缺失问题

    在entityFramework中,decimal精度默认为2位数,当要设置的精度大于2位并且数据库中设置的decimal精度大于2位时,则将数据保存在数据库中后两位的小数内容将强制为00 解决方案: ...

  7. 设置联想键盘恢复F1~F12默认按键的操作办法

    背景 默认都是笔记本键盘才有Fn组合功能键,台式机很少有.今天领到的是联想键盘,给我的台式机使用后F12很麻烦,必须Fn+F12才可以. 需求 恢复默认的F1~F12功能 方案 只需要下载驱动安装: ...

  8. vbs常用函数

    aa '删除文件夹 sub DeleteFolder(objFolder) call OutputLog(objFolder.Path,true) err.Clear On Error Resume ...

  9. [LOJ 6030]「雅礼集训 2017 Day1」矩阵

    [LOJ 6030] 「雅礼集训 2017 Day1」矩阵 题意 给定一个 \(n\times n\) 的 01 矩阵, 每次操作可以将一行转置后赋值给某一列, 问最少几次操作能让矩阵全为 1. 无解 ...

  10. JS中 map, filter, some, every, forEach, for in, for of 用法总结

    本文转载自:http://blog.csdn.net/gis_swb/article/details/52297343 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let ...