1. package com.wazn.learn.util;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipInputStream;
  11. import java.util.zip.ZipOutputStream;
  12.  
  13. /**
  14. * 通过Java的Zip输入输出流实现压缩和解压文件
  15. *
  16. * @author yangzhenyu
  17. *
  18. */
  19. public final class ZipUtil {
  20.  
  21. private ZipUtil() {
  22. // empty
  23. }
  24.  
  25. /**
  26. * 压缩文件
  27. *
  28. * @param filePath
  29. * 待压缩的文件路径
  30. * @return 压缩后的文件
  31. */
  32. public static File zip(String filePath) {
  33. File target = null;
  34. File source = new File(filePath);
  35. if (source.exists()) {
  36. // 压缩文件名=源文件名.zip
  37. String zipName = source.getName() + ".zip";
  38. target = new File(source.getParent(), zipName);
  39. if (target.exists()) {
  40. target.delete(); // 删除旧的文件
  41. }
  42. FileOutputStream fos = null;
  43. ZipOutputStream zos = null;
  44. try {
  45. fos = new FileOutputStream(target);
  46. zos = new ZipOutputStream(new BufferedOutputStream(fos));
  47. // 添加对应的文件Entry
  48. addEntry("/", source, zos);
  49. } catch (IOException e) {
  50. throw new RuntimeException(e);
  51. } finally {
  52. IOUtil.closeQuietly(zos, fos);
  53. }
  54. }
  55. return target;
  56. }
  57.  
  58. /**
  59. * 扫描添加文件Entry
  60. *
  61. * @param base
  62. * 基路径
  63. *
  64. * @param source
  65. * 源文件
  66. * @param zos
  67. * Zip文件输出流
  68. * @throws IOException
  69. */
  70. private static void addEntry(String base, File source, ZipOutputStream zos)
  71. throws IOException {
  72. // 按目录分级,形如:/aaa/bbb.txt
  73. String entry = base + source.getName();
  74. if (source.isDirectory()) {
  75. for (File file : source.listFiles()) {
  76. // 递归列出目录下的所有文件,添加文件Entry
  77. addEntry(entry + "/", file, zos);
  78. }
  79. } else {
  80. FileInputStream fis = null;
  81. BufferedInputStream bis = null;
  82. try {
  83. byte[] buffer = new byte[1024 * 10];
  84. fis = new FileInputStream(source);
  85. bis = new BufferedInputStream(fis, buffer.length);
  86. int read = 0;
  87. zos.putNextEntry(new ZipEntry(entry));
  88. while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
  89. zos.write(buffer, 0, read);
  90. }
  91. zos.closeEntry();
  92. } finally {
  93. IOUtil.closeQuietly(bis, fis);
  94. }
  95. }
  96. }
  97.  
  98. /**
  99. * 解压文件
  100. *
  101. * @param filePath
  102. * 压缩文件路径
  103. */
  104. public static void unzip(String filePath) {
  105. File source = new File(filePath);
  106. if (source.exists()) {
  107. ZipInputStream zis = null;
  108. BufferedOutputStream bos = null;
  109. try {
  110. zis = new ZipInputStream(new FileInputStream(source));
  111. ZipEntry entry = null;
  112. while ((entry = zis.getNextEntry()) != null
  113. && !entry.isDirectory()) {
  114. File target = new File(source.getParent(), entry.getName());
  115. if (!target.getParentFile().exists()) {
  116. // 创建文件父目录
  117. target.getParentFile().mkdirs();
  118. }
  119. // 写入文件
  120. bos = new BufferedOutputStream(new FileOutputStream(target));
  121. int read = 0;
  122. byte[] buffer = new byte[1024 * 10];
  123. while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
  124. bos.write(buffer, 0, read);
  125. }
  126. bos.flush();
  127. }
  128. zis.closeEntry();
  129. } catch (IOException e) {
  130. throw new RuntimeException(e);
  131. } finally {
  132. IOUtil.closeQuietly(zis, bos);
  133. }
  134. }
  135. }
  136.  
  137. }

Java常用工具类之压缩解压的更多相关文章

  1. JavaEE-实验一 Java常用工具类编程

    该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1.  使用类String类的分割split 将字符串  “Solutions to selected exercises ca ...

  2. Java 压缩文件夹工具类(包含解压)

    依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...

  3. Java常用工具类整理

    字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...

  4. Archiver 3 for Mac(解压缩工具) ,想压缩解压慢一点就这么难!

    Archiver 3 for Mac是一款分割合并解压缩工具,简单实用且功能齐全,你只需简单的拖放文件就可以进行压缩,还可以设定解压密码,从而保护自己的隐私.如果文件很大你还可以切割文件.Archiv ...

  5. JAVA常用工具类汇总

    一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...

  6. Java常用工具类---XML工具类、数据验证工具类

    package com.jarvis.base.util; import java.io.File;import java.io.FileWriter;import java.io.IOExcepti ...

  7. java常用工具类(二)

    1.FtpUtil package com.itjh.javaUtil; import java.io.File; import java.io.FileOutputStream; import ja ...

  8. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  9. java常用工具类(java技术交流群57388149)

    package com.itjh.javaUtil;   import java.util.ArrayList; import java.util.List;   /** * * String工具类. ...

随机推荐

  1. 无密码ssh登录linux

    简介 ssh是常见的远程登录linux的方式,大部分时候需要输入用户名密码登录.本文介绍如何无密码登录linux,适用于mac和linux,windows不清楚. 不过这不是什么新的知识,基本上大家都 ...

  2. Mybatis批量删除之Error code 1064, SQL state 42000;

    (一)小小的一次记载. (二):最近的项目都是使用MyBatis,批量新增自己都会写了,但是一次批量删除可把我给折腾了下,写法网上都有,但是照着做就是不行,最后问公司的人,问网友才得到答案,那就是jd ...

  3. NOIP模拟赛15

    NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day1 T1 天天去哪儿吃 直接枚举 #include<cstdio> #include<algorithm> using ...

  4. [NOIP2003]栈 题解(卡特兰数)

    [NOIP2003]栈 Description 宁宁考虑的是这样一个问题:一个操作数序列,从1,2,一直到n(图示为1到3的情况),栈A的深度大于n. 现在可以进行两种操作: 1.将一个数,从操作数序 ...

  5. flex布局语法(阮一峰)

    Flex 布局教程:语法篇   作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + posi ...

  6. D - Keiichi Tsuchiya the Drift King Gym - 102028D (几何)

    题目链接:https://cn.vjudge.net/contest/275150#problem/D 题目大意: 问你能满足那个矩形可以顺利通过的条件,然后求出最小的w 具体思路:首先,我们应该将情 ...

  7. GSON转换日期数据为特定的JSON数据

    通过JSON传递数据的时候经常需要传递日期,Java中可以通过GSON将日期转换为特定格式的JSON数据. 1.普通的GSON转换日期 public void query(HttpServletReq ...

  8. 「caffe编译bug」.build_release/lib/libcaffe.so: undefined reference to cv::imread

    转自:https://www.douban.com/note/568788483/ CXX/LD -o .build_release/tools/convert_imageset.bin.build_ ...

  9. <mvc:annotation-driven/>都做了那些事情

    mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢? <mv ...

  10. Percona XtraDB Cluster(PXC) -集群环境安装

    Percona XtraDB Cluster(PXC)   ---服务安装篇   1.测试环境搭建: Ip 角色 OS PXC-version 172.16.40.201 Node1 Redhat/C ...