原文地址: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();}}}

转载于:https://www.cnblogs.com/davidwang456/p/4841209.html

java解压多目录Zip文件(解决中文乱码问题)--转载相关推荐

  1. java解压两层zip文件_Java解压缩zip - 解压缩多个文件或文件夹实例

    java解压缩zip - 多个文件(包括文件夹),具体如下: 对多个文件和文件夹进行压缩,对复杂的文件目录进行解压. 压缩方法使用的是可变参数,可以压缩1到多个文件..可以写数组的方式或者一个个写到参 ...

  2. 解压上传zip文件并获取excel表数据

    1.maven <!-- 解压rar --><dependency><groupId>com.github.junrar</groupId><ar ...

  3. C#压缩或解压(rar和zip文件)

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  4. 推荐一款好用解压RAR、ZIP文件Mac软件,可以输入密码Dr. Unarchiver

    推荐一款好用解压RAR.ZIP文件Mac软件,可以输入密码Dr. Unarchiver. 今天工作中Windows发给我一个加密RAR文件,是公司很重要的文件,可是Mac不支持呀,因此我把App St ...

  5. php utf-8读取,PHP读取文件,解决中文乱码UTF-8的方法分析

    PHP读取文件,解决中文乱码UTF-8的方法分析,方法,中文,操作技巧,文本,本文 PHP读取文件,解决中文乱码UTF-8的方法分析 易采站长站,站长之家为您整理了PHP读取文件,解决中文乱码UTF- ...

  6. nginx开启目录浏览,解决中文乱码问题

    nginx开启目录浏览,解决中文乱码问题 参考文章: (1)nginx开启目录浏览,解决中文乱码问题 (2)https://www.cnblogs.com/maqingyuan/p/9132690.h ...

  7. php读取pdf文件乱码_PHP读取文件,解决中文乱码UTF-8的方法分析

    本文实例讲述了PHP读取文件,解决中文乱码UTF-8的方法.分享给大家供大家参考,具体如下: $opts = array( 'file' => array( 'encoding' => & ...

  8. php 读取pdf文件内容 显示乱码,PHP读取文件,解决中文乱码UTF-8的方法分析

    本文实例讲述了PHP读取文件,解决中文乱码UTF-8的方法.分享给大家供大家参考,具体如下: $opts = array( "file" => array( "en ...

  9. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception {HttpSession session = request.getSes ...

最新文章

  1. Shell脚本十三问
  2. 有效管理要做的两件事:向上管理和向下负责
  3. cv2.waitKey(1) == ord('key')的等待键盘用法
  4. 腾讯首次发布 AI 白皮书讲了什么?
  5. 2020中国人工智能年度评选报名即将截止!4大类别7大奖项开放申请
  6. 如何让“远程登录”更安全
  7. mysql 事物状态有几种_mysql第三章 事务以及日志
  8. 设置UILabel可变高度(根据文本内容自动适应高度)
  9. html标签的嵌套规则有哪些,html 标签的嵌套规则
  10. 任务计划命令 linux,linux执行一次性任务计划at命令
  11. java毕业生设计学生实验报告管理系统计算机源码+系统+mysql+调试部署+lw
  12. ubuntu屏幕放大+复制粘贴共享
  13. Pepper/Nao中级教程:第六章 Choregraphe写App时缺少Python lib库时如何解决
  14. 小故事有大能量(爱情故事)
  15. 随身Win8系统DIY
  16. RO-data、RW-data、ZI-data的解释
  17. OSChina 周四乱弹 ——小小编辑教你装逼斗气
  18. C++ 赛码打字编程题
  19. Java未来城市练习代码01
  20. 全球及中国电动车头盔行业销售前景态势及投资盈利分析报告2021-2027年

热门文章

  1. python最好用的助手_想学Python的小伙伴注意了,pip工具或许是您最好的助手!本文详解安装教程哦!...
  2. sim卡无线上网公用服务器,有人推出内置SIM卡GPRS模块!GM3S开启无线数传模块新时代!...
  3. html5设计图的状态栏标准高度,HTML5 canvas自适应手机屏幕宽高度大小
  4. 相关系数excel_跟着思想学外汇-如何用Excel计算货币相关性
  5. 二分法细节学习-mid
  6. java json 教程,【简明教程】JSON
  7. 单片机断电后不保存程序_为什么单片机语音芯片既有flash又有EEPROM
  8. python中回文设计_Python中的回文递归
  9. translucent可以设置中文吗_物联卡可以作无线网卡使用吗?物联卡笔记本上网设置流程...
  10. 深度学习数学基础(三): 激活函数、正则化函数、损失函数、评价指标