JDK自带的zip AIP在java.util.zip包下面,主要有以下几个类:

java.util.zip.ZipEntry
java.util.zip.ZipInputStream
java.util.zip.ZipOutputStream

本文编写的zip工具类有以下功能:打包(单个文件、目录)、解包、查看zip包文件

工具类代码如下

  1. package org.net5ijy.util.zip;
  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.ArrayList;
  10. import java.util.List;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipInputStream;
  13. import java.util.zip.ZipOutputStream;
  14.  
  15. /**
  16. * ZIP工具类
  17. *
  18. */
  19. public class ZipUtil {
  20.  
  21. /**
  22. * 解包时的读写大小:10MB
  23. */
  24. private static final int SIZE = 1024 * 1024 * 10;
  25.  
  26. /**
  27. * 打包
  28. *
  29. * @param src
  30. * - 需要打包的文件或目录,不可以是根目录,为根目录时抛出IOException
  31. * @param zipFile
  32. * - 打包后的.zip文件
  33. * @throws IOException
  34. */
  35. public static void zip(File src, File zipFile) throws IOException {
  36.  
  37. // 不可以压缩根目录
  38. if (src.getParent() == null) {
  39. throw new IOException("不可以压缩根目录");
  40. }
  41. // 获取基目录
  42. String base = src.getParentFile().getAbsolutePath();
  43.  
  44. ZipOutputStream out = null;
  45. BufferedOutputStream bos = null;
  46.  
  47. try {
  48.  
  49. out = new ZipOutputStream(new FileOutputStream(zipFile));
  50.  
  51. // 创建缓冲输出流
  52. bos = new BufferedOutputStream(out);
  53.  
  54. // 调用函数
  55. zip(out, bos, src, base);
  56.  
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. throw e;
  60. } finally {
  61. // 关闭流
  62. if (bos != null) {
  63. bos.close();
  64. }
  65. if (out != null) {
  66. out.close();
  67. }
  68. }
  69. }
  70.  
  71. private static void zip(ZipOutputStream out, BufferedOutputStream bos,
  72. File sourceFile, String base) throws IOException {
  73.  
  74. // 如果路径为目录(文件夹)
  75. if (sourceFile.isDirectory()) {
  76. out.putNextEntry(new ZipEntry(sourceFile.getAbsolutePath().replace(
  77. base + File.separator, "")
  78. + "/"));
  79. // 取出文件夹中的文件(或子文件夹)
  80. File[] flist = sourceFile.listFiles();
  81. if (flist.length > 0) {
  82. for (int i = 0; i < flist.length; i++) {
  83. zip(out, bos, flist[i], base);
  84. }
  85. }
  86. } else {// 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
  87. out.putNextEntry(new ZipEntry(sourceFile.getAbsolutePath().replace(
  88. base + File.separator, "")));
  89. BufferedInputStream bis = null;
  90. try {
  91. // 文件输入流
  92. bis = new BufferedInputStream(new FileInputStream(sourceFile));
  93. int b = bis.read();
  94. // 将源文件写入到zip文件中
  95. while (b > -1) {
  96. bos.write(b);
  97. b = bis.read();
  98. }
  99. bos.flush();
  100. } catch (Exception e) {
  101. throw e;
  102. } finally {
  103. if (bis != null) {
  104. bis.close();
  105. }
  106. }
  107. }
  108. }
  109.  
  110. /**
  111. * 解包
  112. *
  113. * @param zipFile
  114. * - 需要解包的.zip文件
  115. * @param targetFolder
  116. * - 目标目录
  117. * @throws IOException
  118. */
  119. public static void unzip(File zipFile, File targetFolder)
  120. throws IOException {
  121.  
  122. // 如果源文件是null 或者 不是一个文件
  123. if (zipFile == null || !zipFile.isFile()
  124. || !zipFile.getName().endsWith(".zip")) {
  125. throw new IOException("请选择正确的.zip文件");
  126. }
  127.  
  128. // 如果目标目录为null 或者 不是一个目录
  129. if (targetFolder == null || !targetFolder.isDirectory()) {
  130. throw new IOException("请选择正确的解压目录");
  131. }
  132.  
  133. // 定义zip文件输入流
  134. ZipInputStream zin = null;
  135.  
  136. try {
  137.  
  138. // 从zipFile创建对应的ZipInputStream输入流
  139. zin = new ZipInputStream(new FileInputStream(zipFile));
  140.  
  141. // 获取下一个条目
  142. for (ZipEntry entry = zin.getNextEntry(); entry != null; entry = zin
  143. .getNextEntry()) {
  144.  
  145. // 获取文件或者目录名
  146. String name = entry.getName();
  147. // 目录
  148. if (name.endsWith("/")) {
  149. File dir = new File(targetFolder, name);
  150. dir.mkdirs();
  151. } else {// 文件
  152. unzip(targetFolder, zin, name);
  153. }
  154. }
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. throw e;
  158. } finally {
  159. if (zin != null) {
  160. zin.close();
  161. }
  162. }
  163. }
  164.  
  165. private static void unzip(File targetFolder, ZipInputStream zin, String name)
  166. throws IOException {
  167.  
  168. // 定义输出流
  169. FileOutputStream out = null;
  170.  
  171. try {
  172.  
  173. // 创建输出流
  174. out = new FileOutputStream(new File(targetFolder.getAbsolutePath()
  175. + File.separator + name));
  176.  
  177. byte[] buf = new byte[SIZE];
  178. int len = zin.read(buf);
  179.  
  180. while (len > -1) {
  181. out.write(buf, 0, len);
  182. len = zin.read(buf);
  183. }
  184. out.flush();
  185.  
  186. } catch (Exception e) {
  187. throw e;
  188. } finally {
  189. if (out != null) {
  190. out.close();
  191. }
  192. }
  193. }
  194.  
  195. /**
  196. * 获取.zip文件中的条目集合
  197. *
  198. * @param zipFile
  199. * - 待查看的.zip文件
  200. * @return 条目的集合
  201. * @throws IOException
  202. */
  203. public static List<ZipEntry> zipEntryList(File zipFile) throws IOException {
  204.  
  205. // 如果源文件是null 或者 不是一个文件
  206. if (zipFile == null || !zipFile.isFile()) {
  207. throw new IOException("请选择正确的.zip文件");
  208. }
  209.  
  210. // 定义zip文件输入流
  211. ZipInputStream zin = null;
  212.  
  213. List<ZipEntry> l = new ArrayList<ZipEntry>();
  214.  
  215. try {
  216. // 从zipFile创建对应的ZipInputStream输入流
  217. zin = new ZipInputStream(new FileInputStream(zipFile));
  218. // 获取下一个条目
  219. ZipEntry entry = zin.getNextEntry();
  220.  
  221. while (entry != null) {
  222. l.add(entry);
  223. entry = zin.getNextEntry();
  224. }
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. throw e;
  228. } finally {
  229. if (zin != null) {
  230. zin.close();
  231. }
  232. }
  233. return l;
  234. }
  235.  
  236. /**
  237. * 获取.zip文件中的条目名称集合
  238. *
  239. * @param zipFile
  240. * - 待查看的.zip文件
  241. * @return 条目名称集合
  242. * @throws IOException
  243. */
  244. public static List<String> zipEntryNameList(File zipFile)
  245. throws IOException {
  246. List<String> l = new ArrayList<String>();
  247. List<ZipEntry> entryList = zipEntryList(zipFile);
  248. for (ZipEntry zipEntry : entryList) {
  249. l.add(zipEntry.getName());
  250. }
  251. return l;
  252. }
  253. }

测试代码

  1. import static org.net5ijy.util.zip.ZipUtil.*;
  2.  
  3. public class ZipUtilTest {
  4.  
  5. public static void main(String[] args) throws IOException {
  6.  
  7. zip(new File(
  8. "D:\\workspace\\SpringCloud\\zip-demo\\src\\org\\net5ijy\\util\\zip\\ZipUtil.java"),
  9. new File("d:\\zip01.zip"));
  10.  
  11. zip(new File("D:\\workspace\\SpringCloud\\spring-data-demo2\\"),
  12. new File("d:\\zip02.zip"));
  13.  
  14. unzip(new File("d:\\zip02.zip"), new File("D:\\a\\"));
  15.  
  16. List<String> nameList = zipEntryNameList(new File("d:\\zip02.zip"));
  17. for (String name : nameList) {
  18. System.out.println(name);
  19. }
  20. }
  21. }

使用JDK的zip编写打包工具类的更多相关文章

  1. 【SSH三大框架】Hibernate基础第二篇:编写HibernateUtil工具类优化性能

    相对于上一篇中的代码编写HibernateUtil类以提高程序的执行速度 首先,仍然要写一个javabean(User.java): package cn.itcast.hibernate.domai ...

  2. AntZipUtils【基于Ant的Zip压缩解压缩工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...

  3. JSR303完成validate校验并编写BeanValidator工具类

    一.引入pom依赖 <!-- validator --> <dependency> <groupId>javax.validation</groupId> ...

  4. JDK在线API及常用工具类

    API http://tool.oschina.net/apidocs/apidoc?api=jdk-zh Java SE常用工具类 java.util.Arrays java.util.Collec ...

  5. java jdk原生的http请求工具类

    package com.base; import java.io.IOException; import java.io.InputStream; import java.io.InputStream ...

  6. Zip操作的工具类

     /** * Copyright 2002-2010 the original author is huanghe. */package com.ucap.web.cm.webapp.util; ...

  7. JDBC_part2_DML以及预编译_编写DBUtil工具类

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! jdbc day02 DML语法 比起插叙语句,没有R ...

  8. 观察者模式学习--使用jdk的工具类简单实现

    观察者模式学习之二:使用jdk的自带的工具类实现,与自己实现相比,两者有以下的区别: 1,自己实现,需要定义观察者的接口类和目标对象的接口类.使用java util的工具类,则不需要自己定义观察者和目 ...

  9. Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...

随机推荐

  1. 关于event 和 window.event问题及浏览器兼容问题

    < html> < script language="javascript"> function test(event) { event = event | ...

  2. 基于vue和echarts的数据可视化实现

    基于vue和echarts的数据可视化: https://github.com/MengFangui/awesome-vue.git

  3. 阿里巴巴Druid数据源组件

    目前常用的数据源主要有c3p0.dbcp.proxool.druid,先来说说他们Spring 推荐使用dbcp:Hibernate 推荐使用c3p0和proxool1. DBCP:apacheDBC ...

  4. centos 7 U盘 uefi 模式装机

    公司买了一台新的dell机器,因为装的是window ,所以想给改成Centos 的做服务器,但是问题来了,一上来装好,就完全进入不了引导系统,换了ubuntu 有一次意外装上了,但一直是什么原因,然 ...

  5. 真机调试No target device的解决(android studio)3.4.1

    驱动等都正常,手机连接正常.但是还是报错 搜索到如下办法成功解决:adb未启动或启动失败,这时候选择android studio界面底端的logcat,会自动重启adb.

  6. Loadrunner查询博客列表并循环删除博客列表中的所有博客

    Loadrunner查询博客列表并循环删除博客列表中的所有博客,在博客列表请求中使用关联,获取出列表中博客的数量,并找出博客列表请求的必要参数.关联使用Ordinal=All 找出所有匹配值 查找出所 ...

  7. JSON字符串转实体对象

    JSON转实体两种方式 代码片段 ; i < dt.Rows.Count; i++) { //Json字符串 string designJson = dt.Rows[i]["Desig ...

  8. word2vec原理总结

    一篇很好的入门博客,http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/ 他的翻译,https://www. ...

  9. Nginx实践篇(5)- Nginx代理服务 - 代理缓冲区、代理重新定义请求头、代理连接超时(转)

    Nginx实践篇(5)- Nginx代理服务 - 代理缓冲区.代理重新定义请求头.代理连接超时 nginx参数默认值 http://nginx.org/en/docs/http/ngx_http_co ...

  10. Django ORM 以连接池方式连接底层连接数据库方法

    django原生支持是不支持 以连接池方式连接数据库的 概述 在使用 Django 进行 Web 开发时, 我们避免不了与数据库打交道. 当并发量低的时候, 不会有任何问题. 但一旦并发量达到一定数量 ...