原文地址: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. Noip 2016 Day1 题解

    老师让我们刷历年真题, 然后漫不经心的说了一句:“你们就先做做noip2016 day1 吧” ...... 我还能说什么,,,,,老师你这是明摆着伤害我们啊2333333333 预计分数:100+2 ...

  2. url传递中文的解决方案总结

    File文件控件,选中文件(图片,flash,视频)即立即预览显示 .Net刷新页面的几种方式 Attributes.Add用途与用法(C#) url传递中文的解决方案总结 byte[] bytes ...

  3. c#同步上下文SynchronizationContext学习笔记

    提供在各种同步模型中传播同步上下文的基本功能.同步上下文的工作就是确保调用在正确的线程上执行. 同步上下文的基本操作 Current 获取当前同步上下文 var context = Synchroni ...

  4. python requests请求卡住问题

    最近经常接到别人反馈某个爬虫工具程序没有正常运行,需要下载的资讯数据也没有及时进行收录. 刚开始以为可能是机器的问题,偶尔机器会出现程序运行中途卡住的情况. 但随着异常的情况越来越频繁,我便只好去排查 ...

  5. Java并发包之Semaphore用法

    多线程中的同步概念就是排着队去执行一个任务,执行任务的是一个一个的执行,并不能并行执行,这样的优点是有助于程序逻辑的正确性,不会出现线程安全问题,保证软件的系统功能上的运行稳定性, Semaphore ...

  6. codevs 1019 集合论与图论

    1019 集合论与图论  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 集合论与图论对于小松来说 ...

  7. 检查类型是否包含iterator

  8. [Python] Working with file

    Python allows you to open a file, do operations on it, and automatically close it afterwards using w ...

  9. JSONArray和JSONObject的简单使用

    一.为什么要使用JSONArray和JSONObject 1.后台 -->前台 能够把java对象和集合转化成json字符串格式,这样在前台的ajax方法中能够直接转化成json对象使用 ,从后 ...

  10. server环境信息【C#代码获取】

    server环境信息[C#代码获取] public class ServicesMessage { [DllImport("kernel32", CharSet = CharSet ...