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. Ubuntu下的UNITY和GNOME界面

    [转自] http://www.tuicool.com/articles/nUbMVbU 从Ubuntu 11.04后,UNITY就作为默认界面来推广.如果用户需要体验GNOME 3,还需要用户自己安 ...

  2. MYSQL常用查命令

    MYSQL常用查命令 mysql> select version();        查看MySQL的版本号 mysql> select current_date();        查看 ...

  3. Linux设备驱动开发基础--阻塞型设备驱动

    1. 当一个设备无法立刻满足用户的读写请求时(例如调用read时,设备没有数据提供),驱动程序应当(缺省的)阻塞进程,使它进入等待(睡眠)状态,知道请求可以得到满足. 2. Linux内核等待队列:在 ...

  4. UNIX文件mode_t详解 ... S_IRUSR

    打开文件.新建文件和关闭文件操作 打开文件操作使用系统调用函数open(),该函数的作用是建立一个文件描述符,其他的函数可以通过文件描述符对指定文件进行读取与写入的操作.打开文件的一般形式是: ope ...

  5. 发送请求时params和data的区别

    在使用axios时,注意到配置选项中包含params和data两者,以为他们是相同的,实则不然. 因为params是添加到url的请求字符串中的,用于get请求. 而data是添加到请求体(body) ...

  6. ckeditor和ckfinder

    ckeditor是一个所见即所得的富文本编辑器,用来代替drupal自带的编辑器. 但是从drupal.com下载的ckeditor模块本身没有实现功能,它指向了由cdn.ckeditor.com所提 ...

  7. 使用 Git 管理源代码(转)

    什么是 Git? 非常简单地说,Git 是 Linus Torvalds 最近实现的源代码管理软件.正如所提供的文档中说的一样,“Git 是一个快速.可扩展的分布式版本控制系统,它具有极为丰富的命令集 ...

  8. ArrayList,LinkList,HashMap

    ArrayList底层实现数组,这是ArrayList get()方法的源码,底层是数组 根据下标返回在数组中对应的位置 ,查询快,插入慢 // Positional Access Operation ...

  9. linux查看占用内存最多的程序

    1.linux查看占用内存最多的程序 ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head 2.查看占用cpu最多的程序 ps aux|head ...

  10. mysql通过数据文件恢复数据方法

    情况描述:服务器硬盘损坏,服务器换了个新硬盘 ,然后老硬盘插在上面.挂载在这台机器.可以从老硬盘里面拿到数据.只拿到了里面的mysql数据文件夹,把数据文件夹覆盖新的服务器mysql数据文件夹 启动报 ...