内容简介

本文主要介绍使用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. C语言链表结构体(学习笔记)

    #include <stdio.h> #define LENTEST 100 // 采取逐步删除的方法求的素数 //先假设1-100都是素数,然后剔除2的倍数, //3的倍数,直到剔除所有 ...

  2. js修改css时如何考虑兼容性问题es5+es6

    es5的写法 var elementStyle = document.createElement('div').style var vendor = (function(){ let transfor ...

  3. Linux-iptables(2)

    iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables,因此理解如何配置iptables将会帮助你更有效地管理Linux防火墙.如果你是第一次 ...

  4. nodejs stream & buffer 互相转换

    stream 转 buffer function streamToBuffer(stream) { return new Promise((resolve, reject) => { let b ...

  5. IDEA-Eclipse结构项目转移到 Idea教程

    1.确定Idea的svn配置 2.从svn导出项目 3.检出项目完成,开始设置 4.配置完成,设置svn忽略文件,忽略掉idea配置文件等 5. 设置完毕

  6. 【leetcode刷题笔记】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. VS2008中_T的作用

    引用: VC++里面定义字符串的时候,用_T来保证兼容性.VC++支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到unicode编码类型的时候,程序不需要修改. 如 ...

  8. debian上安装codeblocks

    1.查看linux的版本uname -a 2.在官网上下载稳定版的codeblocks(www.codeblocks.org) 3.解压codeblocks后,进入到文件夹中用root身份执行dpkg ...

  9. Python:代码单元、代码点介绍

    转于:https://www.cnblogs.com/runwulingsheng/p/5106078.html 博主:你是那天边突然划过的一道闪电 代码点:指编码表(比如Unicode)中某个字符的 ...

  10. 使用ceph命令提示handle_connect_reply connect got BADAUTHORIZER

    输入命令提示如下错误: [root@node1 ~]# rados -p testpool ls 2017-10-21 06:13:25.743045 7f8f89b6d700 0 -- 192.16 ...