依赖的包:

<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.8.2</version>
</dependency>

package com.jd.dlink.service.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

/**
* 解压Zip文件工具类
*
* @author zhangqian
*
*/
public class unzipUtil {
private static final int buffer = 2048;

/**
* 解压Zip文件
*
* @param path
* 文件目录
*/
public static void unZip(String path) {
int count = -1;
String savepath = "";

File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;

savepath = path.substring(0, path.lastIndexOf("\\")) + File.separator; // 保存解压文件目录
new File(savepath).mkdir(); // 创建保存目录
ZipFile zipFile = null;
try {
zipFile = new ZipFile(path, "gbk"); // 解决中文乱码问题
Enumeration<?> entries = zipFile.getEntries();

while (entries.hasMoreElements()) {
byte buf[] = new byte[buffer];

ZipEntry entry = (ZipEntry) entries.nextElement();

String filename = entry.getName();
boolean ismkdir = false;
if (filename.lastIndexOf("/") != -1) { // 检查此文件是否带有文件夹
ismkdir = true;
}
filename = savepath + filename;

if (entry.isDirectory()) { // 如果是文件夹先创建
file = new File(filename);
file.mkdirs();
continue;
}
file = new File(filename);
if (!file.exists()) { // 如果是目录先创建
if (ismkdir) {
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); // 目录先创建
}
}
file.createNewFile(); // 创建文件

is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);

while ((count = is.read(buf)) > -1) {
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();

is.close();
}

zipFile.close();

} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
unZip("E:\\jd\\东联\\资源包问题相关.zip");
// String f = "E:\\jd\\东联";
// File file = new File(f);
// String[] test = file.list();
// for (int i = 0; i < test.length; i++) {
// System.out.println(test[i]);
// }
//
// System.out.println("------------------");
//
// String fileName = "";
//
// File[] tempList = file.listFiles();
// for (int i = 0; i < tempList.length; i++) {
// if (tempList[i].isFile()) {
// System.out.println("文 件:" + tempList[i]);
//
// fileName = tempList[i].getName();
//
// System.out.println("文件名:" + fileName);
// }
// if (tempList[i].isDirectory()) {
// System.out.println("文件夹:" + tempList[i]);
// }
// }
}
}

java解压缩zip的更多相关文章

  1. java解压缩zip和rar的工具类

    package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...

  2. java 解压缩Zip文件 ziputil

    package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...

  3. Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...

  4. java 版本压缩、解压缩zip

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  5. java解压缩.gz .zip .tar.gz等格式的压缩包方法总结

    1..gz文件是linux下常见的压缩格式.使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream public s ...

  6. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  7. java压缩/解压缩zip格式文件

    因为项目要用到压缩.解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的.基本上还是比较好用的. 废话少说,直接上代码. }     ...

  8. java实现 zip解压缩

    程序实现了ZIP压缩.共分为2部分 : 压缩(compression)与解压(decompression) 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压. ...

  9. JAVA如何解压缩ZIP文档

    代码片段: package org.yu.units; import java.io.Closeable; import java.io.File; import java.io.FileInputS ...

随机推荐

  1. update linux dns,no need restart

    [root@hc--uatbeta2 ~]# cd /etc[root@hc--uatbeta2 etc]# vi resolv.conf ******* nameserver 10.123.23.*

  2. (贪心)nyoj448-寻找最大数

    题目描述: 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=92081346718538,m=10时,则新的最大数是9888 输入描述: 第一行输入一个正整数T,表示 ...

  3. IAR STM32 ------ CSTACK HEAP 设置一次可用栈的大小,HardFault_Hander

    CSTACK:限制函数中定义数组的最大值,否则进入HardFault_Hander HEAP:限制动态分配内存(C函数库中的malloc)的大小,不用可以设置为0

  4. qml: 自定义按钮-- 仿QML自带控件;

    import QtQuick 2.0 Rectangle { id: btn; width:; height:; radius:; border.color: "#A3A3A3"; ...

  5. 【优秀的艺术文字和图标设计软件】Art Text 3.2.3 for Mac

      [简介] Art Text 3.2.3 版本,这是一款Mac上简单易用的艺术文字和图标设计软件,今这款软件内置了大量的背景纹理和特效,能够让我们非常快速的制作出漂亮的图标,相比专业的PS,Art ...

  6. Linux中sed的用法实践

    Linux中sed的用法实践 参考资料:https://www.cnblogs.com/emanlee/archive/2013/09/07/3307642.html http://www.fn139 ...

  7. .net中 登录 才能下载文件的方法 Response.WriteFile实现下载

    protected void Button2_Click(object sender, EventArgs e) { //可以在这里加是否登录的判断 string fileName = "c ...

  8. ruby----%使用法

    %{String}  用于创建一个使用双引号括起来的字符串,这个表示法与%Q{String}完全一样 result = %{hello} puts "result is: #{result} ...

  9. Linux下C语言连接MySQL

    本文出自   http://blog.csdn.net/shuangde800 首先保证安装: 1:安装MySQL:sudo apt-get install mysql-server mysql-cl ...

  10. jenkins笔记

    java -jar jenkins.war -httpPort=9090 以9090端口启动jekins的web应用(内置jetty)