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

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

  1. JAVA解压.Z及.ZIP文件

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <dependency ...

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

    /// <summary> /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe)) /// </summary&g ...

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

    nginx开启目录浏览,解决中文乱码问题 方法如下: server { listen 80; #listen [::]:80; server_name gongzi.liwenhui.xin gz.l ...

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

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

  5. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  6. python-----自动解压并删除zip文件

    如何自动解压并删除zip? 如何解压  →  使用内置模块来实现(shutil.unpack_archive) 如何删除zip  →  使用内置模块os来实现(os.remove) 如何监测zip的出 ...

  7. java解压多层目录中多个压缩文件和处理压缩文件中有内层目录的情况

    代码: package com.xiaobai; import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...

  8. Linux中解压、压缩 ZIP文件

    解压 unzip -o -d /home/v-gazh myfile.zip # 把myfile.zip文件解压到 /home/v-gazh/ # -o:不提示的情况下覆盖文件: # -d:-d /h ...

  9. 在Ubuntu系统中解压rar和zip文件的方法

    大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...

随机推荐

  1. 洛谷P2415 集合求和

    题目描述 给定一个集合s(集合元素数量<=30),求出此集合所有子集元素之和. 输入输出格式 输入格式: 集合中的元素(元素<=1000) 输出格式: 和 输入输出样例 输入样例#1: 2 ...

  2. android学习笔记二、Activity深入学习

    一.创建和使用: 1.Activity是android的四大组件之一,需要继承Activity并在清单文件中进行声明才能使用.没有声明则报错. 2.启动Activity是通过Intent,有两种方式: ...

  3. Bundles软件

    Bundle 称为:软件集 或 打包捆绑软件(软件束) Bundle就是一组包含了文件集,软件包或许可程序产品的软件,它们组合在一起为了实现一个特定的功能     快速来列出系统bundle软件 sm ...

  4. IBM将收购Linux发行商红帽公司,继续发力云计算市场

    10月29日凌晨消息,IBM和Red Hat当地时间星期日联合宣布,IBM将以340亿美元收购红帽公司(Red Hat).根据两家公司发表的一份联合声明,IBM将以每股190美元的价格,以现金方式收购 ...

  5. Maintaining processor resources during architectural events

    In one embodiment of the present invention, a method includes switching between a first address spac ...

  6. cogs 26. 分组

    26. 分组 ★   输入文件:dataa.in   输出文件:dataa.out   简单对比时间限制:1 s   内存限制:128 MB[问题描述] 现有 n 个学生, 要分成X1 ,X2 ,.. ...

  7. C#List<string>和string[]之间的相互转换

     一.LIST概述 所属命名空间:System.Collections.Generic      public class List<T> : IList<T>, IColle ...

  8. 博弈论 SG函数(模板) HDU 1848 Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. module.exports,exports和export default,export的区别

    前提:CommonJS模块规范和ES6模块规范是完全不同的两个概念. module.exports,exports属于CommonJS模块规范: export default,export属于ES6模 ...

  10. 如何优雅的写UI——(2)MFC下基于CFormView的文档视图程序

    在MFC中可以创建多种类型的窗口程序,如对话框程序.单文档结构程序(非文档/视图结构).单文档(文档/视图结构)以及多文档视图结构程序等. 在编写一般的小工具时,我们的首选显然是对话框程序,不过基于对 ...