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. [转] 【译】让人倾倒的 11 个 npm trick

    [From] https://segmentfault.com/a/1190000006804410 本文转载自:众成翻译译者:文蔺链接:http://www.zcfy.cc/article/1206 ...

  2. 处女座和他的小姐姐(三)----数位DP

    链接:https://ac.nowcoder.com/acm/contest/329/G来源:牛客网 经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666! 处女座其实 ...

  3. (转)博弈 SG函数

    此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...

  4. archlinux升级firefox的flash插件

    参考:http://blog.csdn.net/kingolie/article/details/53066415 1. 在https://get.adobe.com/flashplayer/下载文件 ...

  5. datagrip 导出数据库备份到本地

    1.建立自己的数据库 2.如图 3,结果

  6. vue 的计算属性computed自我理解

    类型:{ [key: string]: Function | { get: Function, set: Function } } 计算属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算.注意, ...

  7. 超级详细全截图化VMware 安装ubantu

    一,下载镜像 由于ubantu时国外源所以下载十分的缓慢 这里我用清华源下载:https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/18.10/ub ...

  8. 并列 inline-block 元素互相影响问题

    今天在做页面时发现一个很奇怪的问题:当两个设置了display: inline-block; 属性的元素并列排放时,它们的位置能够互相影响. 我们先来看看元素结构: <div class=&qu ...

  9. vue-cli 3.x安装配置步骤详细说明

      一.vue-cli 3.x简单介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统:是一个类似于 create-react-app 的可以用例命令行快速配置和生成一个 vue 项 ...

  10. 获取全球dns统计信息

    # -*- coding:UTF-8 -*- import requests, time import json from bs4 import BeautifulSoup as bp t3 = ti ...