依赖的包:

<!-- 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. Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:

    Administrator@DESKTOP-EHCTIOR MINGW64 /d/react-native-eyepetizer (master) $ react-native run-android ...

  2. WPF界面+halcon生成的C#文件

    1.新建WPF应用程序完成后,解决方案资源管理器——引用处右键,添加引用如下两个dll 工具箱,空白处右键,选择项(只添加halcondotnet.dll,位置同上).这样halcon平台的控件就添加 ...

  3. (注意输入格式)bistuoj(旧)1237 成绩统计

    成绩统计 Time Limit(Common/Java):1000MS/3000MS          Memory Limit:65536KByteTotal Submit:88          ...

  4. nginx.conf配置文件详解

    一:nginx配置文件结构 nginx配置文件主要分为六个区域: main(全局设置).events(nginx工作模式).http(http设置). sever(主机设置).location(URL ...

  5. Excel:6种多条件查找方法

    如下图所示,要求根据设备分类和品牌来查找相应的销售数量. 1. 使用VLOOKUP+辅助列进行多条件查找 本例采用的方法是在原表的最前面加一辅助列,辅助列的公式为:=B2&C2 然后再采用VL ...

  6. 1.1实战项目:电影周周看V1(初识小程序)

    第一小程序的实战项目: 覆盖的小程序技术: 讲解方式: 学习方法:

  7. 10款 Mac 系统优化清理工具软件推荐和下载

    本文内容及图片来源[风云社区 SCOEE] 在Windows上有各种安全卫士.系统助手等系统优化和清理工具,比如360安全卫士.腾讯安全管家等,同样MacOS系统也有很多好用的系统优化清理工具,体验比 ...

  8. 20165232 学习基础和c语言基础调查

    做中学读后感 学习是要思考的,仅仅实践是不够的: 不光会动手,还要理解背后的原理 不光会用工具,还要理解支撑的理论 技能是分层次的: 一项技能的掌握程度分为:新手/高级初学者/合格者/精通/专家 对技 ...

  9. OS + RedHat 6.3 x64 NFS / mount.nfs: access denied by server while mounting

    s Linux mount/unmount命令(转) https://www.cnblogs.com/xd502djj/p/3809375.html 问题2:NFS配置项no_root_squash和 ...

  10. 043、data-packed volume container (2019-03-06 周三)

    参考https://www.cnblogs.com/CloudMan6/p/7203285.html     volume container 的数据归根到底还是在host上,我们能不能把数据完全放到 ...