内容简介

本文主要介绍使用ZipFile来提取zip压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下。

导入包:import java.util.zip.ZipFile;

如需添加对rar压缩格式的支持,请参考我的另一篇文章:https://www.cnblogs.com/codecat/p/11078485.html

实现代码(仅供参考,请根据实现情况来修改)

  1. /**
  2. * 将压缩文件中指定后缀名称的文件解压到指定目录
  3. * @param compressFile 压缩文件
  4. * @param baseDirectory 解压到的基础目录(在此目录下创建UUID的目录,存入解压的文件)
  5. * @param decompressSuffs 要提取文件的后缀名
  6. * @return
  7. */
  8. @Override
  9. public void decompressToUUIDDirectory(File compressFile, String baseDirectory, List<String> decompressSuffs) throws Exception {
  10. List<AttachFile> attachFileList = new ArrayList<>();
  11.  
  12. //验证压缩文件
  13. boolean isFile = compressFile.isFile();
  14. if (!isFile){
  15. System.out.println(String.format("compressFile非文件格式!",compressFile.getName()));
  16. return null;
  17. }
  18. String compressFileSuff = FileUtil.getFileSuffix(compressFile.getName()).toLowerCase();
  19. if (!compressFileSuff.equals("zip")){
  20. System.out.println(String.format("[%s]文件非zip类型的压缩文件!",compressFile.getName()));
  21. return null;
  22. }
  23.  
  24. //region 解压缩文件(zip)
  25. ZipFile zip = new ZipFile(new File(compressFile.getAbsolutePath()), Charset.forName("GBK"));//解决中文文件夹乱码
  26. for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();){
  27. ZipEntry entry = entries.nextElement();
  28. String zipEntryName = entry.getName();
  29. //过滤非指定后缀文件
  30. String suff = FileUtil.getFileSuffix(zipEntryName).toLowerCase();
  31. if (decompressSuffs != null && decompressSuffs.size() > 0){
  32. if (decompressSuffs.stream().filter(s->s.equals(suff)).collect(Collectors.toList()).size() <= 0){
  33. continue;
  34. }
  35. }
  36. //创建解压目录(如果复制的代码,这里会报错,没有StrUtil,这里就是创建了一个目录来存储提取的文件,你可以换其他方式来创建目录)
  37. String groupId = StrUtil.getUUID();
  38. File group = new File(baseDirectory + groupId);
  39. if(!group.exists()){
  40. group.mkdirs();
  41. }
  42. //解压文件到目录
  43. String outPath = (baseDirectory + groupId + File.separator + zipEntryName).replaceAll("\\*", "/");
  44. InputStream in = zip.getInputStream(entry);
  45. FileOutputStream out = new FileOutputStream(outPath);
  46. byte[] buf1 = new byte[1024];
  47. int len;
  48. while ((len = in.read(buf1)) > 0) {
  49. out.write(buf1, 0, len);
  50. }
  51. in.close();
  52. out.close();
  53. }
  54. //endregion
  55. }
  1. ZipFile

java 提取(解压)zip文件中特定后缀的文件并保存到指定目录的更多相关文章

  1. java 提取(解压)rar文件中特定后缀的文件并保存到指定目录

    内容简介 本文主要介绍使用junrar来提取rar压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下. 支持v4及以下版本压缩文件,不支持v5及以上. 在rar文件上右键,查看属性,在压 ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  4. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  5. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  6. java代码解压zip文件

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  7. Java动态解压zip压缩包

    import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; impo ...

  8. java无需解压zip压缩包直接读取包内的文件名(含中文)

    java自带了java.util.zip工具可以实现在不解压zip压缩包的情况下读取包内文件的文件名:(注:只能是ZIP格式的,rar我试了不行)代码如下: public static String ...

  9. java使用解压zip文件,文件名乱码解决方案

    File outFileDir = new File(outDir);if (!outFileDir.exists()) { boolean isMakDir = outFileDir.mkdirs( ...

随机推荐

  1. Luogu P1463 [HAOI2007]反素数ant:数学 + dfs【反素数】

    题目链接:https://www.luogu.org/problemnew/show/P1463 题意: 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x ...

  2. 使用kill命令终止进程shell脚本

    因有的程序使用kill才能结束掉进程,没有关闭脚本,以我司的服务为例,服务名叫asset-server服务,只有启动脚本,自编写关闭脚本,及重启动脚本. 关闭服务脚本. vim asset-shutd ...

  3. UVA12163 游戏

    题目大意 现在有两个人在一个n个结点的有向图上玩一个双人游戏,保证图中无环和自圈.游戏的规则如下:1.初始的时候$i$号点有一个正权值$value_i$2.两名玩家依次操作,每个玩家在当前回合可以选择 ...

  4. G 唐纳德与子串(easy)(华师网络赛---字符串,后缀数组)(丧心病狂的用后缀自动机A了一发Easy)

    Time limit per test: 1.0 seconds Memory limit: 256 megabytes 子串的定义是在一个字符串中连续出现的一段字符.这里,我们使用 s[l…r] 来 ...

  5. Excel文本获取拼音

    [说明] 版本:Excel 2010 文件后缀:.xls 有在.xlsb文件下使用未成功.建议使用.xls后缀. 1.调出“开发工具” 步骤:文件-->选项-->自定义功能区-->勾 ...

  6. bzoj 2535 & bzoj 2109 航空管制 —— 贪心+拓扑序

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...

  7. 自己写的基于java Annotation(注解)的数据校验框架

    JavaEE6中提供了基于java Annotation(注解)的Bean校验框架,Hibernate也有类似的基于Annotation的数据校验功能,我在工作中,产品也经常需要使 用数据校验,为了方 ...

  8. Python知识点: __import__

    用法:libvirt = __import__('libvirt') help(__import__) __import__(...)    __import__(name, globals={}, ...

  9. MODBUS TCP和MODBUS RTU的差别

    TCP和RTU协议非常类似, MBAP Header长度共7个字节,分别为Transaction identifier(事务标识符),Protocol identifier(协议标识符),Length ...

  10. leetcode笔记-1 twosum

    # -*- coding: utf-8 -*- #!/bin/env python # Python2.7 nums = [2, 4, 7, 0, 12, 6] print sorted(range( ...