对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. fastjson 反序列化漏洞利用总结

    比赛遇到了,一直没利用成功,这里做个记录. 环境搭建 首先用 vulhub 搭建 fastjson 的漏洞环境. 漏洞环境程序的逻辑为接收 body 的数据然后用 fastjson 解析. 漏洞利用 ...

  2. Expo大作战(七)--expo如何使用Genymotion模拟器

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,将全部来与官网 我猜去全部机翻+个人 ...

  3. apache web服务器安全配置

    尽管现在购买的云服务器很多都有一键web环境安装包,但是如果是自己配置web环境则需要对各种安全配置十分了解,今天我们就来尝试这做好web服务器安全配置.这里的配置不尽完善,若有纰漏之处还望指出. 修 ...

  4. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  5. Oracle EBS GL 会计科目报错 GL_ACCESS_SET_LEDGERS

    1.会计科目设置后,总账中找不到对应账簿                                           2.原因是新版本系统物化视图有问题,参照metalink解决方案得知原路径 ...

  6. Python脚本性能分析

    来自:http://www.cnblogs.com/btchenguang/archive/2012/02/03/2337112.html def foo(): sum = 0 for i in ra ...

  7. ln 硬链接介绍

    硬链接:在linux文件系统中多个文件名指向同一个索引节点(Inode)是正常允许的.硬链接文件就相当于文件的另一个入口. 硬链接的作用:允许一个文件拥有多个有效路径(多个入口),这样用户就可以建立硬 ...

  8. 在Ubuntu17.04中遇到无法清空回收站解决方法

    在Ubuntu17.04下,遇到清空回收站文件时报错,提示”Failed to delete the item from the trash”,无法清空回收站. 回收站其实就是一个文件夹,存放着被删掉 ...

  9. 【转】学习Linux守护进程详细笔记

    [原文]https://www.toutiao.com/i6566814959966093837/ Linux守护进程 一. 守护进程概述 守护进程,也就是通常所说的Daemon进程,是Linux中的 ...

  10. MySQL基础之 AND和OR运算符

    AND和OR运算符 作用:用于基于一个以上的条件对记录进行过滤 用法:可在WHERE子句中把两个或多个条件结合在一起. AND:如果第一个条件和第二个条件都成立,才会显示一条记录 OR:如果第一个条件 ...