java解压缩zip
依赖的包:
<!-- 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的更多相关文章
- java解压缩zip和rar的工具类
package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...
- java 解压缩Zip文件 ziputil
package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...
- Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php
Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...
- java 版本压缩、解压缩zip
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
- java解压缩.gz .zip .tar.gz等格式的压缩包方法总结
1..gz文件是linux下常见的压缩格式.使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream public s ...
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
- java压缩/解压缩zip格式文件
因为项目要用到压缩.解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的.基本上还是比较好用的. 废话少说,直接上代码. } ...
- java实现 zip解压缩
程序实现了ZIP压缩.共分为2部分 : 压缩(compression)与解压(decompression) 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压. ...
- JAVA如何解压缩ZIP文档
代码片段: package org.yu.units; import java.io.Closeable; import java.io.File; import java.io.FileInputS ...
随机推荐
- 第三十节,目标检测算法之Fast R-CNN算法详解
Girshick, Ross. “Fast r-cnn.” Proceedings of the IEEE International Conference on Computer Vision. 2 ...
- 马士兵2string buffuer
string不可变的字符序列 s1=s1+s2
- MySQL中IO问题定位
在前面讲过在linux下定位磁盘IO的一个命令:iostat其实还有一个查看linux下磁盘IO读写速度命令:iotop 查看iotop -help,有哪些用法 # iotop -help Usage ...
- Centos 6\7下yum安装R
在linux下安装软件不如windows方便,在windows下我们一般针对可安装的文件(如R.exe)直接双击运行就可以了.而在linux下我们首先要学会使用安装软件的专门工具如centos下的rp ...
- java io系列10之 FilterInputStream
FilterInputStream 介绍 FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”.它的常用的子类有BufferedInputStream和Data ...
- maven坑-Failure to transfer org.apache.maven:maven
参考网址:http://www.mkyong.com/maven/how-to-convert-maven-java-project-to-support-eclipse-ide/ https://b ...
- request模块的使用
安装方式 $ pip install requests 基本GET请求(headers参数 和 parmas参数) 1. 最基本的GET请求可以直接用get方法 response = requests ...
- samba服务器之无认证进入共享目录
修改设备中的/log/samba/lib/smb.conf文件 security = share [web] ...
- Linux下C语言连接MySQL
本文出自 http://blog.csdn.net/shuangde800 首先保证安装: 1:安装MySQL:sudo apt-get install mysql-server mysql-cl ...
- 转---Python——numpy random类
numpy中利用random类获取随机数. numpy.random.random() 生成随机浮点数 默认为生成一个随机的浮点数,范围是在0.0~1.0之间,也可以通过参数size设置返回数据的si ...