内容简介

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

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

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

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

/**
* 将压缩文件中指定后缀名称的文件解压到指定目录
* @param compressFile 压缩文件
* @param baseDirectory 解压到的基础目录(在此目录下创建UUID的目录,存入解压的文件)
* @param decompressSuffs 要提取文件的后缀名
* @return
*/
@Override
public void decompressToUUIDDirectory(File compressFile, String baseDirectory, List<String> decompressSuffs) throws Exception {
List<AttachFile> attachFileList = new ArrayList<>(); //验证压缩文件
boolean isFile = compressFile.isFile();
if (!isFile){
System.out.println(String.format("compressFile非文件格式!",compressFile.getName()));
return null;
}
String compressFileSuff = FileUtil.getFileSuffix(compressFile.getName()).toLowerCase();
if (!compressFileSuff.equals("zip")){
System.out.println(String.format("[%s]文件非zip类型的压缩文件!",compressFile.getName()));
return null;
} //region 解压缩文件(zip)
ZipFile zip = new ZipFile(new File(compressFile.getAbsolutePath()), Charset.forName("GBK"));//解决中文文件夹乱码
for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = entries.nextElement();
String zipEntryName = entry.getName();
//过滤非指定后缀文件
String suff = FileUtil.getFileSuffix(zipEntryName).toLowerCase();
if (decompressSuffs != null && decompressSuffs.size() > 0){
if (decompressSuffs.stream().filter(s->s.equals(suff)).collect(Collectors.toList()).size() <= 0){
continue;
}
}
//创建解压目录(如果复制的代码,这里会报错,没有StrUtil,这里就是创建了一个目录来存储提取的文件,你可以换其他方式来创建目录)
String groupId = StrUtil.getUUID();
File group = new File(baseDirectory + groupId);
if(!group.exists()){
group.mkdirs();
}
//解压文件到目录
String outPath = (baseDirectory + groupId + File.separator + zipEntryName).replaceAll("\\*", "/");
InputStream in = zip.getInputStream(entry);
FileOutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
}
//endregion
}
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. codeforces 553B B. Kyoya and Permutation(找规律)

    题目链接: B. Kyoya and Permutation time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. pthread_cond_wait()用法分析

    很久没看APUE,今天一位朋友问道关于一个mutex的问题,又翻到了以前讨论过的东西,为了不让自己忘记,把曾经的东西总结一下. 先大体看下网上很多地方都有的关于pthread_cond_wait()的 ...

  3. How to manage concurrency in Django models

    How to manage concurrency in Django models The days of desktop systems serving single users are long ...

  4. pytorch--cpu与gpu load时相互转化

    pytorch------cpu与gpu load时相互转化 torch.load(map_location=)学习 将gpu改为cpu时,遇到一个报错:RuntimeError: Attemptin ...

  5. 找工作--volatile

    在Java编写的程序中,有时为了提高程序的运行效率,编译器会自动对其进行优化,把经常访问的变量缓存起来,程序在读取这个变量时有可能会直接从缓存(例如寄存器)中来读取这个值,而不会从内存中读取.这样做的 ...

  6. HDU3018:Ant Trip(欧拉回路)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HeartBleed bug

    前两年的一个严重漏洞,影响很大.出现在openssl 1.0.1和1.0.2 beta(包含1.0.1f和1.0.2beta1).利用了TLS的heartbeat. 简单的说,该漏洞被归为缓冲过度读取 ...

  8. OpenStack安装后检查流程总结

    安装后检查 1. 确保服务正常运行 首先查看服务的运行状态: #service xxx status 为防止对子服务有疏漏,可使用ps + grep 查看: # ps aux |grep xx 2. ...

  9. ceph学习之CRUSH

    CRUSH的全称是Controlled Replication Under Scalable Hashing,是ceph数据存储的分布式选择算法,也是ceph存储引擎的核心.在之前的博客里介绍过,ce ...

  10. Linker Tools Error LNK2001

    https://msdn.microsoft.com/en-us/library/f6xx1b1z.aspx https://www.cnblogs.com/runningRain/p/5674833 ...