一、说明

rar格式的压缩包收费,java支持zip格式的压缩和解压

二、工具类

  1. import java.io.*;
  2. import java.util.Enumeration;
  3. import java.util.List;
  4. import java.util.zip.ZipEntry;
  5. import java.util.zip.ZipFile;
  6. import java.util.zip.ZipOutputStream;
  7. public class ZipUtils {
  8. private static final int BUFFER_SIZE = 2 * 1024;
  9. /**
  10. * zip解压
  11. * @param srcFile zip源文件
  12. * @param destDirPath 解压后的目标文件夹
  13. * @throws RuntimeException 解压失败会抛出运行时异常
  14. */
  15. public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
  16. long start = System.currentTimeMillis();
  17. // 判断源文件是否存在
  18. if (!srcFile.exists()) {
  19. throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
  20. }
  21. // 开始解压
  22. ZipFile zipFile = null;
  23. try {
  24. zipFile = new ZipFile(srcFile);
  25. Enumeration<?> entries = zipFile.entries();
  26. while (entries.hasMoreElements()) {
  27. ZipEntry entry = (ZipEntry) entries.nextElement();
  28. System.out.println("解压" + entry.getName());
  29. // 如果是文件夹,就创建个文件夹
  30. if (entry.isDirectory()) {
  31. String dirPath = destDirPath + "/" + entry.getName();
  32. File dir = new File(dirPath);
  33. dir.mkdirs();
  34. } else {
  35. // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
  36. File targetFile = new File(destDirPath + "/" + entry.getName());
  37. // 保证这个文件的父文件夹必须要存在
  38. if(!targetFile.getParentFile().exists()){
  39. targetFile.getParentFile().mkdirs();
  40. }
  41. targetFile.createNewFile();
  42. // 将压缩文件内容写入到这个文件中
  43. InputStream is = zipFile.getInputStream(entry);
  44. FileOutputStream fos = new FileOutputStream(targetFile);
  45. int len;
  46. byte[] buf = new byte[BUFFER_SIZE];
  47. while ((len = is.read(buf)) != -1) {
  48. fos.write(buf, 0, len);
  49. }
  50. // 关流顺序,先打开的后关闭
  51. fos.close();
  52. is.close();
  53. }
  54. }
  55. long end = System.currentTimeMillis();
  56. System.out.println("解压完成,耗时:" + (end - start) +" ms");
  57. } catch (Exception e) {
  58. throw new RuntimeException("unzip error from ZipUtils", e);
  59. } finally {
  60. if(zipFile != null){
  61. try {
  62. zipFile.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 压缩成ZIP 方法
  71. * @param srcFiles 需要压缩的文件列表
  72. * @param out 压缩文件输出流
  73. * @throws RuntimeException 压缩失败会抛出运行时异常
  74. */
  75. public static void toZip(List<File> srcFiles , OutputStream out)throws Exception {
  76. long start = System.currentTimeMillis();
  77. ZipOutputStream zos = null ;
  78. try {
  79. zos = new ZipOutputStream(out);
  80. for (File srcFile : srcFiles) {
  81. byte[] buf = new byte[BUFFER_SIZE];
  82. zos.putNextEntry(new ZipEntry(srcFile.getName()));
  83. int len;
  84. FileInputStream in = new FileInputStream(srcFile);
  85. while ((len = in.read(buf)) != -1){
  86. zos.write(buf, 0, len);
  87. }
  88. zos.closeEntry();
  89. in.close();
  90. }
  91. long end = System.currentTimeMillis();
  92. System.out.println("压缩完成,耗时:" + (end - start) +" ms");
  93. } catch (Exception e) {
  94. throw new RuntimeException("zip error from ZipUtils",e);
  95. }finally{
  96. if(zos != null){
  97. try {
  98. zos.close();
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. }
  105. }

三、使用工具类压缩和解压文件

  1. import com.szfore.utils.ZipUtils;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class TestZip {
  8. public static void main(String[] args) throws Exception{
  9. //testToZip();
  10. testUnzip();
  11. }
  12. /**
  13. * 测试压缩文件
  14. */
  15. public static void testToZip() throws Exception {
  16. File file1 = new File("c:\\1.txt");
  17. File file2 = new File("c:\\2.txt");
  18. List<File> files = new ArrayList<File>();
  19. files.add(file1);
  20. files.add(file2);
  21. OutputStream out = new FileOutputStream("c:\\1.zip");
  22. ZipUtils.toZip(files,out);
  23. }
  24. /**
  25. * 测试解压文件
  26. * @throws Exception
  27. */
  28. public static void testUnzip() throws Exception{
  29. File srcFile = new File("c:\\2.zip");
  30. String destDirPath = "c:\\";
  31. ZipUtils.unZip(srcFile,destDirPath);
  32. }
  33. }

Java操作zip-压缩和解压文件的更多相关文章

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

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

  2. java实现zip压缩和解压工具

    引入ant.jar package com.develop.web.util; import java.io.BufferedInputStream; import java.io.File; imp ...

  3. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  4. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  5. C#压缩和解压文件

    这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...

  6. Android_JarZip压缩和解压文件

        本文资料来自<android开发权威指南> AndroidSDK中提供了java.util.jar和java.util.zip包中的若干类和接口来完成. 压缩文件基本步骤: 1.创 ...

  7. linux zip压缩和解压的各种操控

    1.把/home文件夹以下的mydata文件夹压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata文件夹 2.把/home文件夹以下的mydata.zip解 ...

  8. C# 压缩和解压文件(SharpZipLib)

    先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...

  9. linux压缩和解压文件命令

    tar  解包:tar zxvf filename.tar  打包:tar czvf filename.tar dirnamegz命令  解压1:gunzip filename.gz  解压2:gzi ...

随机推荐

  1. Java 比较器

    比较器 Arrays 类 主要功能: 完成所有与数组有关的操作的工具类 二分查找: 在一个有序的数字序列中进行二分查找 public static int binarySearch(数据类型 [] a ...

  2. [转]Outlook VBA自动处理邮件

    本文转自:https://blog.csdn.net/hnwyllmm/article/details/44874331 需求描述公司里面每天都会有很多邮件,三分之一都是不需要看的,Outlook的过 ...

  3. Go切片去掉重复元素

    1.Go切片去掉重复元素 如果传入的是string类型: //slice去重 func removeRepByMap(slc []string) []string { result := []stri ...

  4. 渗透测试学习 十三、 SQLmap使用详解

    SQLmap介绍 sqlmap是一个由python语言编写的开源的渗透测试工具,它主要是检测SQL注入漏洞,是一款功能强大的SQL漏洞检测利用工具. 他可以检测的数据库有:access.msSQL.M ...

  5. MASK-RCNN(1)

    MASK-RCNN是一个多用途的网络,可以用来做目标检测,实例分割或者人体姿态识别.主要结构如下. 简单的说,就是首先用Faster-RCNN获得ROI,再进行ROI Align,然后输出ROI的分类 ...

  6. mysql里字符集的配置

    [client]default-character-set=utf8[mysqld]character-set-server = utf8[mysql]default-character-set=ut ...

  7. 借助meta影藏顶部菜单

    1===>报错 Cannot find module 'webpack/lib/Chunk' 删除node_modules 然后重新下载 4==> 现在已进入页面,底部就有四个菜单,在点击 ...

  8. layUI学习第一日:myeclipse中使用layUI

    第一步:下载layUI,网址:https://www.layui.com/ 第二步:查看layUI解压后的内容,和官网解释各个文件夹的内容 第三部:在myeclipse中新建一个web project ...

  9. 关于字符串在ie浏览器拼接问题

    常用的字符串在ie浏览器拼接不识别的问题,建议不要使用字符串拼接,可直接用jquery添加方便快捷一些

  10. Excel日历控件实现下拉选取日期含VB代码实现

    以下是Excel2016通过安装控件,实现表格下拉选择日期的一些步骤: 知识准备工作:先了解下如何安装控件,这一部分很重要,excel选择可用宏https://jingyan.baidu.com/ar ...