java解压zip文件

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.Enumeration;
  9. import java.util.zip.CRC32;
  10. import java.util.zip.CheckedInputStream;
  11.  
  12. import org.apache.tools.zip.ZipEntry;
  13. import org.apache.tools.zip.ZipFile;
  14.  
  15. public class MyZipUtil {
  16. /**
  17. * 解压zip文件
  18. * @throws IOException
  19. */
  20. public static String unZIP(String zipPath,String outPath ){
  21. ZipFile zipFile=null;
  22. try {
  23. zipFile = new ZipFile(zipPath,"GBK");
  24. //压缩文件的实列,并设置编码
  25. //获取压缩文中的所以项
  26. for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
  27. {
  28. OutputStream os = null;
  29. BufferedOutputStream bos =null;
  30. InputStream is =null;
  31. BufferedInputStream bis =null;
  32. CheckedInputStream cos =null;
  33. ZipEntry zipEntry = null;
  34. try {
  35. zipEntry = enumeration.nextElement();//获取元素
  36. //排除空文件夹
  37. if(!zipEntry.getName().endsWith(File.separator))
  38. {
  39. System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
  40. //创建解压目录
  41. File f = new File(outPath);
  42. //判断是否存在解压目录
  43. if(!f.exists())
  44. {
  45. f.mkdirs();//创建解压目录
  46. }
  47. os = new FileOutputStream(outPath+zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1));//创建解压后的文件
  48. bos = new BufferedOutputStream(os);//带缓的写出流
  49. is = zipFile.getInputStream(zipEntry);//读取元素
  50. bis = new BufferedInputStream(is);//读取流的缓存流
  51. cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性
  52. byte [] b = new byte[1024*8];//字节数组,每次读取1024个字节
  53. //循环读取压缩文件的值
  54. while(cos.read(b)!=-1)
  55. {
  56. bos.write(b);//写入到新文件
  57. }
  58. bos.flush();
  59. os.flush();
  60.  
  61. }
  62. else
  63. {
  64. //如果为空文件夹,则创建该文件夹
  65. new File(outPath+zipEntry.getName()).mkdirs();
  66. }
  67.  
  68. } catch (Exception e) {
  69. return "1";
  70. }finally {
  71. if(cos!=null){
  72. cos.close();
  73. }
  74. if(bis!=null){
  75. bis.close();
  76. }
  77. if(is!=null){
  78. is.close();
  79. }
  80. if(bos!=null){
  81. bos.close();
  82. }
  83. if(os!=null){
  84. os.close();
  85. }
  86. if(zipEntry!=null){
  87. zipEntry.clone();
  88. }
  89. }
  90. }
  91. System.out.println("解压完成");
  92. } catch (IOException e1) {
  93. // TODO Auto-generated catch block
  94. e1.printStackTrace();
  95. }finally {
  96.  
  97. try {
  98. zipFile.close();
  99. } catch (IOException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. }
  104.  
  105. return "0";
  106. }
  107. }

zip文件解压工具类的更多相关文章

  1. Zip包解压工具类

    最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错. package com.winning.polaris.admin.utils ...

  2. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  3. ZIP文件解压

    public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...

  4. Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用

    文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...

  5. linux下压缩成zip文件解压zip文件

    linux  zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux  zip命令参数列表: -a     将文件转成ASCII模式 -F     尝试修复损坏 ...

  6. Python实现加密的ZIP文件解压(密码已知)

    博主在上篇博文介绍了<Python实现加密的RAR文件解压(密码已知)>后,又尝试了ZIP文件的解压方法,下面开始分享. 当ZIP文件的压缩密码已知时,可以通过调用zipfile库进行解压 ...

  7. Java_压缩与解压工具类

    转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...

  8. linux下zip文件解压后乱码解决方案

    解决办法一,利用pyton来处理 1.vi uzip文件2.复制一下内容(Python) #!/usr/bin/env python # -*- coding: utf-8 -*- # uzip.py ...

  9. zip文件解压或压缩

    <span style="font-size:18px;">/** * lsz */ public final class ZipUtil { /** * 解压zip文 ...

随机推荐

  1. ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies

    There are NNN children in kindergarten. Miss Li bought them NNN candies. To make the process more in ...

  2. Confluence 6 针对你的数据库类型确定校验 SQL

    不同的数据库通常要求不同的 SQL 校验查询.校验查询通常需要尽可能的简单,这个查询在链接从数据库连接池中取出的时候都会被执行一次. 针对不同的数据库类型,我们推荐先的校验查询 SQL: MySQL ...

  3. verilog-产生axis数据流

    首先这是产生aixs数据流的代码 `timescale 1ps/1ps `default_nettype none module axis_switch_0_example_master #( ) ( ...

  4. LeetCode(124):二叉树中的最大路径和

    Hard! 题目描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不需要经过根节点. 示例 1: 输入: [1, ...

  5. nmap 扫描信息收集

    1.端口镜像 port Mirroring 功能通过在交换机上或者路由器上,将一个或者多个源端口的数据流量妆发大奥某一个指定的端口来实现对网络的监听,指定端口成为镜像端口或目的端口. 2.ARP攻击捕 ...

  6. css+js杂记

    css的盒子模型分:ie盒子模型和标准盒子模型 区别: 2.选择器 .id选择器(# myid) .类选择器(.myclassname) .标签选择器(div, h1, p) .相邻选择器(h1 + ...

  7. Python笔记记录

    python2和python3的不同: Unicode(统一码.万国码),在3里面可以直接写中文了. python2里rae_input与python3中的input效果一样 在计算机内存中,统一用U ...

  8. C++ Primer 笔记——模板与泛型编程

    1.编译器用推断出的模板参数来为我们实例化一个特定版本的函数. 2.每个类型参数前必须使用关键字class或typename.在模板参数列表中,这两个关键字含义相同,可以互换使用,也可以同时使用. t ...

  9. The error may exist in com/bjpowernode/dao/StudentDao.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderExcept

    The error may exist in com/bjpowernode/dao/StudentDao.xml### Cause: org.apache.ibatis.builder.Builde ...

  10. Ubuntu16.04安装vim8

    Ubuntu16.04安装vim8 在Ubuntu16.04下编译安装vim8,并配置vim-plug插件管理器,以及安装YouCompleteMe等插件. 安装依赖 sudo apt-get ins ...