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. knime 设置 小数点精度

    kinme 默认小数精度是保留三位小数. 如果0.0003,knime会自动舍弃,读出0.下面步骤教你怎么把小数精度全部显示. File->references->preferred re ...

  2. Google Authenticator(谷歌身份验证器)

    <!DOCTYPE html>Google Authenticator(谷歌身份验证器) ] Google Authenticator(谷歌身份验证器) Google Authentica ...

  3. Json化数据-调微信接口

    // 先获取用户openid列表 List<String> openids = wxPhotoUpload.getUserOpenIdList(access_token); TreeMap ...

  4. Go初探

      官方网站:https://golang.org/ 标准库文档:https://golang.org/pkg/ 在线编码学习:https://play.golang.org/ PS:请自行FQ 简介 ...

  5. linux命令行下的操作的快捷键

    历史相关命令 命令                   含义!!                      执行上一条命令!num                 执行历史命令中的第num条命令!-n ...

  6. Ace教你一步一步做Android新闻客户端(五) 优化Listview

    今天写存货了 调试一些动画参数花了些时间 ,嘿嘿存货不多了就没法做教程了,今天来教大家优化listview,等下我把代码编辑下 这次代码有些多 所以我把条理给大家理清楚.思路就是把加载图片的权利交给O ...

  7. 【linux相识相知】压缩与打包

    我们日常使用window的时候,经常会用到压缩与解压缩,如果要压缩一个文件,右击选择[添加到压缩文件],解压缩则右击选择[解压到当前文件夹],“点点点”就能完成.但是在一个没有装图形化界面的linux ...

  8. 【一】JMeter的介绍安装和使用

    利用JMeter进行性能测试 一.JMeter介绍二.Jmeter安装三.工作原理四.脚本录制五.运行JMeter进行测试六.JMeter主要组件介绍七.参数化设置八.动态数据关联九.使用插件进行服务 ...

  9. OpenLayers 3 实现划线,画点

    //划线 this.drawLine = function (type) { //清除双击放大效果 this.map.getInteractions().item(1).setActive(false ...

  10. Angular6 基础(数据绑定、生命周期、父子组件通讯、响应式编程)

    Angular相比于vue来说,更像一个完整的框架,本身就集成了很多模块,如路由,HTTP,服务等,而vue是需要另外引入比如(vuex,axios等).Angular引入了依赖注入.单元测试.类等后 ...