From 36cf627f3d6eed51d71db3843834f237e525f2cb Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 6 Sep 2021 17:59:01 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 1 + .../cn/hutool/core/compress/ZipReader.java | 63 +++++++++++++------ .../java/cn/hutool/core/codec/Base64Test.java | 6 ++ 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aa8927ad..3f547ccbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【setting】 Setting、Props持有URL改为持有Resource(pr#1182@Github) * 【json 】 JSONUtil.toJsonStr增加重载,支持JSONConfig(issue#I48H5L@Gitee) * 【crypto 】 SymmetricCrypto增加setMode方法,update采用累加模式(pr#1642@Github) +* 【core 】 ZipReader支持Filter ### 🐞Bug修复 * 【core 】 修复ListUtil.split方法越界问题(issue#I48Q0P@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/compress/ZipReader.java b/hutool-core/src/main/java/cn/hutool/core/compress/ZipReader.java index 625324ba0..824a8328d 100755 --- a/hutool-core/src/main/java/cn/hutool/core/compress/ZipReader.java +++ b/hutool-core/src/main/java/cn/hutool/core/compress/ZipReader.java @@ -3,6 +3,7 @@ package cn.hutool.core.compress; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.lang.Filter; import cn.hutool.core.util.ZipUtil; import java.io.Closeable; @@ -71,7 +72,7 @@ public class ZipReader implements Closeable { /** * 构造 * - * @param in 读取的的Zip文件流 + * @param in 读取的的Zip文件流 * @param charset 编码 */ public ZipReader(InputStream in, Charset charset) { @@ -88,7 +89,8 @@ public class ZipReader implements Closeable { } /** - * 获取指定路径的文件流 + * 获取指定路径的文件流
+ * 如果是文件模式,则直接获取Entry对应的流,如果是流模式,则遍历entry后,找到对应流返回 * * @param path 路径 * @return 文件流 @@ -101,7 +103,17 @@ public class ZipReader implements Closeable { return ZipUtil.getStream(zipFile, entry); } } else { - throw new UnsupportedOperationException("Zip stream mode not support get!"); + try { + this.in.reset(); + ZipEntry zipEntry; + while (null != (zipEntry = in.getNextEntry())) { + if(zipEntry.getName().equals(path)){ + return this.in; + } + } + } catch (IOException e) { + throw new IORuntimeException(e); + } } return null; @@ -115,22 +127,37 @@ public class ZipReader implements Closeable { * @throws IORuntimeException IO异常 */ public File readTo(File outFile) throws IORuntimeException { + return readTo(outFile, null); + } + + /** + * 解压到指定目录中 + * + * @param outFile 解压到的目录 + * @param entryFilter 过滤器,排除不需要的文件 + * @return 解压的目录 + * @throws IORuntimeException IO异常 + * @since 5.7.12 + */ + public File readTo(File outFile, Filter entryFilter) throws IORuntimeException { read((zipEntry) -> { - // FileUtil.file会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/ - File outItemFile = FileUtil.file(outFile, zipEntry.getName()); - if (zipEntry.isDirectory()) { - // 目录 - //noinspection ResultOfMethodCallIgnored - outItemFile.mkdirs(); - } else { - InputStream in; - if (null != this.zipFile) { - in = ZipUtil.getStream(this.zipFile, zipEntry); + if (null == entryFilter || entryFilter.accept(zipEntry)) { + // FileUtil.file会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/ + final File outItemFile = FileUtil.file(outFile, zipEntry.getName()); + if (zipEntry.isDirectory()) { + // 目录 + //noinspection ResultOfMethodCallIgnored + outItemFile.mkdirs(); } else { - in = this.in; + InputStream in; + if (null != this.zipFile) { + in = ZipUtil.getStream(this.zipFile, zipEntry); + } else { + in = this.in; + } + // 文件 + FileUtil.writeFromStream(in, outItemFile, false); } - // 文件 - FileUtil.writeFromStream(in, outItemFile, false); } }); return outFile; @@ -140,8 +167,8 @@ public class ZipReader implements Closeable { * 读取并处理Zip文件中的每一个{@link ZipEntry} * * @param consumer {@link ZipEntry}处理器 - * @throws IORuntimeException IO异常 * @return this + * @throws IORuntimeException IO异常 */ public ZipReader read(Consumer consumer) throws IORuntimeException { if (null != this.zipFile) { @@ -154,7 +181,7 @@ public class ZipReader implements Closeable { @Override public void close() throws IORuntimeException { - if(null != this.zipFile){ + if (null != this.zipFile) { IoUtil.close(this.zipFile); } else { IoUtil.close(this.in); diff --git a/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java b/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java index fa5b0fe7a..75e732015 100644 --- a/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java +++ b/hutool-core/src/test/java/cn/hutool/core/codec/Base64Test.java @@ -92,5 +92,11 @@ public class Base64Test { @Test public void decodeEmojiTest(){ + String str = "😄"; + final String encode = Base64.encode(str); +// Console.log(encode); + + final String decodeStr = Base64.decodeStr(encode); + Assert.assertEquals(str, decodeStr); } }