对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. java 使用 引用数据类型(以Scanner、Random模块为例)

    创建一个新变量 类型 变量名 = new 类型() 举个例子: Scanner sc = new Scaner() 使用引用数据类型中的功能: 变量.功能名字() Scanner类:接受键盘输入 1. ...

  2. 反射式DLL注入--方法

    使用RWX权限打开目标进程,并为该DLL分配足够大的内存. 将DLL复制到分配的内存空间. 计算DLL中用于执行反射加载的导出的内存偏移量. 调用CreateRemoteThread(或类似的未公开的 ...

  3. 纯js实现页面返回顶部的动画

    啥也不说了,直接上代码 var scrollTop = document.body.scrollTop; document.body.style.marginTop = -scrollTop + 'p ...

  4. mysql INSERT的几个语法 IGNORE|REPLACE|LOW_PRIORITY | DELAYED

    INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以保留 ...

  5. 从外部导入django模块

    import os import sys sys.path.append("D:\\pyweb\\sf"); # 项目位置(不是app) os.environ.setdefault ...

  6. 事务的ACID性质

    最近在读黄健宏的<Redis设计与实现>,现在看到了事务这章,由于之前(上学)没有好好整理过数据库事务的四大性质,导致现在(工作)看到了就和第一次知道一样((lll¬ω¬)).还是要把基础 ...

  7. Linux uptime命令详解

    常见的命令展示 uptime 08:21:34 up 36 min, 2 users, load average: 0.00, 0.00, 0.00 #当前服务器时间: 08:21:34 #当前服务器 ...

  8. chrome历史记录,浏览记录,全选问题.

    一句话, 这个列表支持SHIFT多选 =,=!

  9. 题解 P1034 【矩形覆盖】

    题面 在平面上有n个点(n≤50),每个点用一对整数坐标表示.例如:当n=4时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见图一. 这些点可以用k个矩形(1≤ ...

  10. T4学习- 2、创建设计时模板

    使用设计时 T4 文本模板,您可以在 Visual Studio 项目中生成程序代码和其他文件. 通常,您编写一些模板,以便它们根据来自模型的数据来改变所生成的代码. 模型是包含有关应用程序要求的关键 ...