原文地址: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. [ Java ][ Eclipse ] 停止讓 Eclipse 跳出 Password Required

    stackoverflow 上,問題的解決方式: http://stackoverflow.com/questions/4713890/how-to-disable-programmatically- ...

  2. UDP广播

    客户端UDP发送消息至服务器端服务器IP:192.168.1.114服务器端口:2014 客户端 Socket socket = new Socket(AddressFamily.InterNetwo ...

  3. Python——Pygame实现生命游戏(game of life)

    模块:pygame import pygame,sys,time,random from pygame.locals import * """Color"&qu ...

  4. 测试理论--web测试方法总结

    一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使 ...

  5. android+myeclipse+mysql自定义控件下拉框的数据绑定

    原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8031936.html 本人小白,那个大神看到有问题可指出,谢谢.... 这个是 ...

  6. C# 调用者信息特性(Attribute)

    .NET 4.5中引用了三种特性(Attribute), 该特性允许获取调用者的当前编译器的执行文件名.所在行数与方法或属性名称. 命名空间 System.Runtime.CompilerServic ...

  7. EditPlus 使用技巧以及快捷键

    一边阅读,一边动手吧! 为了达到更好的效果,请你先下载我打包的这个 EditPlus压缩包文件(压缩包文件为绿色的EditPlus2.31英文版,含自动完成文件,高亮语法文件和剪切板代码片断文件,这些 ...

  8. Gym 100952 H. Special Palindrome

    http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...

  9. Dubbo springcloud

    简而言之,Dubbo确实类似于Spring Cloud的一个子集,Dubbo功能和文档完善,在国内有很多的成熟用户,然而鉴于Dubbo的社区现状(曾经长期停止维护,2017年7月31日团队又宣布重点维 ...

  10. Vue中独立组件之间数据交互

    独立组件之间数据交互:通过自定义事件 组件A中的[数据],传递给组件B 1.创建组件A,组件B 2.组件B在实例创建完成时就开始监听事件[等待接收数据]:钩子 3.组件A中触发事件,发送数据 注意:接 ...