package com.lanyuan.assembly.util;

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;  
   
  /**
   * 解压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();  
              }  
          }  
      }

/*public static void main(String[] args)  
    {  
        unZip("F:\\110000002.zip");
        String f = "F:\\110000002";
        File file = new File(f);
        String[] test=file.list();
        for(int i=0;i<test.length;i++){
            System.out.println(test[i]);
        }
        
        System.out.println("------------------");
        
        String fileName = "";
        
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                System.out.println("文     件:"+tempList[i]);
                
                fileName = tempList[i].getName();
                
                System.out.println("文件名:"+fileName);
            }
            if (tempList[i].isDirectory()) {
                System.out.println("文件夹:"+tempList[i]);
            }
        }
    }  */
}

java 解压缩Zip文件 ziputil的更多相关文章

  1. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  2. Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...

  3. java 生成zip文件并导出

    总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...

  4. java对 zip文件的压缩和解压(ant解决中文乱码)

    说明: 1.对于压缩的文件,当文件名称是中文时,若使用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)进行压缩, ...

  5. java压缩zip文件中文乱码问题(转——作者:riching)

    本人遇到了同样的问题,用了以下方案,奇迹般的解决了.我很纳闷为什么,经理说:好读书,不求甚解,不要问为什么... 用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问 ...

  6. java解压缩zip

    依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...

  7. java压缩zip文件中文乱码问题

    用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...

  8. java 读取Zip文件进行写入

    直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch; import java.io.BufferedReader; import java.io.F ...

  9. Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)

    一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...

随机推荐

  1. 利用touchslide实现tab滑动切换

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. 异步的模块合并管理器-m3m4版本和jquery版本(原创)

    m3m4版本 (function(w){ var loading={} var models={} function define(name,args){ loading[name]=true mod ...

  3. 给新人看的 JavaScript的继承

    //这个继承方式是给新人看的,逻辑简单一些 Object.extend=function(props){ //继承父类 var prototype=Object.create(this.prototy ...

  4. cmd命令行编译c/c++程序

    一.打开文件夹(文件夹名字为123,文件夹里面有程序345.cpp) cd 123 二.在cmd里面编译程序 1.cd Desktop      //在打开文件夹之前不回到桌面会出错 2.cd 123 ...

  5. flask综合案例

    一.项目准备 1.新建项目目录students,并创建虚拟环境 mkvirtualenv students 2.安装依赖环境 pip install flask==0.12.4 pip install ...

  6. 剑指offer——面试题5:替换空格

    利用STL: #include"iostream" #include"stdio.h" #include"algorithm" using ...

  7. 6.centos7 gitblit

    1,安装注意事项 1)必须将gitblit安装在/opt/gitblit这个目录下,因为 服务启动的脚本里面默认就是这个路径 如果不按照这个路径安装需要修改脚本 mkdir -p /opt/gitbl ...

  8. 写些最近两个学安卓的笔记-关于Toast

    1.Toast可以在Activity和service里使用,在Service里使用时,Toast是显示在当前的Activity上. 2.Toast出现时,当前的Activity依然可见可交互. 3.T ...

  9. 024-cxf.xml配置文件模板

    版本一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  10. 32位x86处理器编程导入——《x86汇编语言:从实模式到保护模式》读书笔记08

    在说正题之前,我们先看2个概念. 1.指令集架构(ISA) ISA 的全称是 instruction set architecture,中文就是指令集架构,是指对程序员实际"可见" ...