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 ...
随机推荐
- vue使用element-ui 监听使用回车键事件
因为element-ui 对input做了封装,使用@keyup.enter="fn"触发Enter键事件就不会触发,解决:后面追加.native.@keyup.enter.nat ...
- TP5.0+小程序商城构建(1)
1.导语 1.整体的思路与编程思想(大局观.AOP面向切面编程,10-20%) 2.具体的编程知识与技巧(TP5.小程序.数据库等80%) 2.课程内容与产品技术点 1.ThinkPHP5框架 1.编 ...
- 第一篇-Win10打开txt文件出现中文乱码
如果刚开始安装的是英文的Win10系统,那么打开txt文件时很容易出现乱码问题.包括打开cmd窗口,也是不能显示中文的.当然,麻烦的处理方法是: 在cmd中想要显示中文:先输入chcp 936,之后中 ...
- Python之黏包
黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cmd.decode('utf-8'), shell ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- OpenRefine
无论是现今的大数据还是企业内部的小数据,都存在一些普遍的问题,如数据格式不对需要转换,一个单元格内包含多个含义的内容,包含重复项等等,虽然我们也可以使用excel解决,但是excel天生有诸多限制,比 ...
- Maven Tomcat7+ 实现自动化部署
首先在Tomcat里配置deploy的用户(tomcat根目录/conf/tomcat-users.xml): <role rolename="tomcat"/> &l ...
- 2017-12-18python全栈9期第三天第四节之str的索引与切片
#!/user/bin/python# -*- coding:utf-8 -*-s = 'zdlswwzl's1 = s[0]print(s1)s2 = s[-1]print(s2)s3 = s[0: ...
- Windows Dll Injection、Process Injection、API Hook、DLL后门/恶意程序入侵技术
catalogue 1. 引言2. 使用注册表注入DLL3. 使用Windows挂钩来注入DLL4. 使用远程线程来注入DLL5. 使用木马DLL来注入DLL6. 把DLL作为调试器来注入7. 使用c ...
- JAVA-try-catch-finally-自定义异常例子(适合初学者)
package com.net.xinfang.reflect; import java.io.IOException; import java.util.Scanner; /*** * 运行try块 ...