java解压多目录Zip文件(解决中文乱码问题)--转载
2023-09-11 14:21:41 时间
原文地址:http://zhangyongbo.iteye.com/blog/1749439
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 zhangyongbo * */ public class ZipUtil { private static final int buffer = 2048; public static void main(String[] args) { unZip("E:\\java\\Android\\aaa.zip"); } /** * 解压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(); } } } }
指定目录重新的方法如下:
/** * 解压Zip文件 * @param path 文件目录 */ public static void unZipNew(String path,String savepath) { int count = -1; File file = null; InputStream is = null; FileOutputStream fos = null; BufferedOutputStream bos = null; 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(); } } }
相关文章
- MySQL_(Java)【连接池】简单在JDBCUtils.java中创建连接池
- 使用java中的String类操作复杂的字符串
- 最全三大框架整合(使用映射)——Dept.java
- 揽货最短路径解决方案算法 - V2(增加了时间维度-客户允许的服务时间段,C#/JAVA同步实现,带python作图)
- JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)
- Java实现 LeetCode 338 比特位计数
- Java实现 蓝桥杯 算式最大值
- Java实现 LeetCode 55 跳跃游戏
- java实现识别复制串
- Java中多态举例说明
- Java实现 蓝桥杯VIP 算法提高 超级玛丽
- Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素
- java unicode转码为中文 实例
- macos:安装java 17.0.6(android studio报错:Unable to locate a Java Runtime.)
- 【JAVA】毕向东Java基础视频教程-笔记
- ubuntu下DbVisualizer 中文乱码,包括JAVA 乱码
- 【Java】java使用反射访问对象方法和成员变量
- Java超类-java.lang.object
- 用Java打印中文字符的GBK,UTF8和UTF16编码
- 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查
- Atitit 搜索蓝牙设备 powershell的实现 java noede.js python 先用脚本语言python nodejs,不好实现。。Java 也不好实现。。 Netcore可以,
- Atitit java字符串模板渲染总结 目录 1. 总结:指标1 1.1. 支持中文变量 提升可读性1 1.2. 变量placeholder简单性,,velo可以直接¥前导简单。。Free的
- java 截屏 类似于 QQ截屏
- 华为OD机试 - 快递投放问题(Java & JS & Python)
- java中文编码乱码
- 【Java】java 性能监控及工具
- Java 异常解决之java.lang.IllegalArgumentException: Comparison method violates its general contract!
- java集合: LinkedList源码浅析
- 【java】Java 重写(Override)与重载(Overload)
- 【java】Java IO体系总览
- JAVA开发讲义(一)-Java的自白
- C/C++调用java---JNI常用函数