对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. SQLServer 学习笔记之超详细基础SQL语句 Part 11

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 10------------------- DECLARE @myavg ...

  2. 2018-10-27 22:44:33 c language

    2018-10-27  22:44:33 c language 标准的C语言并不支持上面的二进制写法,只是有些编译器自己进行了扩展,才支持二进制数字.并不是所有的编译器都支持二进制数字,只有一部分编译 ...

  3. android--获取屏幕高宽度工具类

    //获得屏幕相关的辅助类 public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw n ...

  4. python 中* 和**的作用

    先举个 ** 使用的例子: data = {"a": 1, "b": 2} def foo(**kwargs): print kwargs foo(a=1, b ...

  5. python之绘制图形库turtle

    关于绘制图形库turtle# 画布上,默认有一个坐标原点为画布中心的坐标轴(0,0),默认"standard"模式坐标原点上有一只面朝x轴正方向小乌龟 一:海龟箭头Turtle相关 ...

  6. 转:jQuery插件开发全解析

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  7. 一个汇编的HelloWorld!

    花了一下午时间,感觉最坑的是,书写代码的个数和编译器的坑比较多,还各种版本的编译器! 会让人“眼花缭乱”! 主要代码 将文件保存为*.asm include io32.inc .data ;数据 sr ...

  8. Linux vsftpd 配置文件详解

    .默认配置: >允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES >匿名用户使用的登陆名为ftp或anonymous,口令为空:匿名 ...

  9. shp转oracle spatial

    2010年12月1日  终于搞定了shp到oracle spatial,说下步骤和感受吧! 1 XP系统:转换工具的下载(shp2sdo.exe ):下载后把此文件复制到PATH变量包含的目录下(E: ...

  10. jQuery 1.11 / 2.1 beta 版发布

    jQuery开发团队近日发布了jQuery 1.11和2.1的beta版本. jQuery 1.x版本支持IE 6/7/8,jQuery 2.x 不支持,如果你已经抛弃了IE 6/7/8用户,可以升级 ...