From 04cf410ccec121c13ce281c3d8a9dbf54e067638 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 9 Nov 2019 18:29:20 +0800 Subject: [PATCH] fix zip bug --- CHANGELOG.md | 1 + .../java/cn/hutool/core/util/ZipUtil.java | 19 +++- .../cn/hutool/core/bean/BeanUtilTest.java | 102 ++++++------------ .../java/cn/hutool/core/util/ZipUtilTest.java | 4 +- 4 files changed, 54 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f83ef1638..0058e9f03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * 【extra】 修复ServletUtil.getCookie大小写问题(pr#79@Gitee) * 【core】 修复IdcardUtil.isValidCard18报错问题(issue#I14LTJ@Gitee) * 【poi】 修复double值可能存在的精度问题(issue#I14FG1@Gitee) +* 【core】 修复Linux下解压目录不正确的问题(issue#I14NO3@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java index df0351032..c15c11e71 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java @@ -430,7 +430,7 @@ public class ZipUtil { while (em.hasMoreElements()) { zipEntry = em.nextElement(); // FileUtil.file会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/ - outItemFile = FileUtil.file(outFile, zipEntry.getName()); + outItemFile = buildFile(outFile, zipEntry.getName()); if (zipEntry.isDirectory()) { // 创建对应目录 outItemFile.mkdirs(); @@ -1056,6 +1056,23 @@ public class ZipUtil { throw new IORuntimeException(e); } } + + /** + * 根据压缩包中的路径构建目录结构,在Win下直接构建,在Linux下拆分路径单独构建 + * + * @param outFile 最外部路径 + * @param fileName 文件名,可以包含路径 + * @return 文件或目录 + * @since 5.0.5 + */ + private static File buildFile(File outFile, String fileName){ + if(false == FileUtil.isWindows() && StrUtil.contains(fileName, CharUtil.SLASH)) { + // 在Linux下多层目录创建存在问题,/会被当成文件名的一部分,此处做处理 + final String[] pathParts = StrUtil.splitToArray(fileName, CharUtil.SLASH); + return FileUtil.file(pathParts); + } + return FileUtil.file(outFile, fileName); + } // ---------------------------------------------------------------------------------------------- Private method end } \ No newline at end of file diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java index 4ca867c4c..ec613194b 100644 --- a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java @@ -3,12 +3,17 @@ package cn.hutool.core.bean; import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.bean.copier.ValueProvider; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Console; import cn.hutool.core.map.MapUtil; +import lombok.Getter; +import lombok.Setter; import org.junit.Assert; import org.junit.Test; import java.beans.PropertyDescriptor; import java.lang.reflect.Type; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -125,6 +130,23 @@ public class BeanUtilTest { Assert.assertEquals("sub名字", map.get("sub_name")); } + @Test + public void beanToMapWithLocalDateTimeTest() { + final LocalDateTime now = LocalDateTime.now(); + + SubPerson person = new SubPerson(); + person.setAge(14); + person.setOpenid("11213232"); + person.setName("测试A11"); + person.setSubName("sub名字"); + person.setDate(now); + person.setDate2(now.toLocalDate()); + + Map map = BeanUtil.beanToMap(person, false, true); + Assert.assertEquals(now, map.get("date")); + Assert.assertEquals(now.toLocalDate(), map.get("date2")); + } + @Test public void getPropertyTest() { SubPerson person = new SubPerson(); @@ -162,12 +184,12 @@ public class BeanUtilTest { // 测试boolean参数值isXXX形式 SubPerson p2 = new SubPerson(); BeanUtil.copyProperties(p1, p2); - Assert.assertTrue(p2.isSlow()); + Assert.assertTrue(p2.getSlow()); // 测试boolean参数值非isXXX形式 SubPerson2 p3 = new SubPerson2(); BeanUtil.copyProperties(p1, p3); - Assert.assertTrue(p3.isSlow()); + Assert.assertTrue(p3.getSlow()); } @Test @@ -180,7 +202,7 @@ public class BeanUtilTest { Map map = MapUtil.newHashMap(); BeanUtil.copyProperties(p1, map); - Assert.assertTrue((Boolean) map.get("isSlow")); + Assert.assertTrue((Boolean) map.get("slow")); Assert.assertEquals("测试", map.get("name")); Assert.assertEquals("sub测试", map.get("subName")); } @@ -214,88 +236,32 @@ public class BeanUtilTest { } // ----------------------------------------------------------------------------------------------------------------- + @Getter + @Setter public static class SubPerson extends Person { public static final String SUBNAME = "TEST"; private UUID id; private String subName; - private Boolean isSlow; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public String getSubName() { - return subName; - } - - public void setSubName(String subName) { - this.subName = subName; - } - - public Boolean isSlow() { - return isSlow; - } - - public void setSlow(Boolean isSlow) { - this.isSlow = isSlow; - } + private Boolean slow; + private LocalDateTime date; + private LocalDate date2; } + @Getter + @Setter public static class SubPerson2 extends Person { private String subName; // boolean参数值非isXXX形式 private Boolean slow; - - public String getSubName() { - return subName; - } - - public void setSubName(String subName) { - this.subName = subName; - } - - public Boolean isSlow() { - return slow; - } - - public void setSlow(Boolean isSlow) { - this.slow = isSlow; - } } + @Getter + @Setter public static class Person { private String name; private int age; private String openid; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getOpenid() { - return openid; - } - - public void setOpenid(String openid) { - this.openid = openid; - } } } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ZipUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ZipUtilTest.java index 4cb606b35..cac7e2bc0 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/ZipUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ZipUtilTest.java @@ -26,10 +26,8 @@ public class ZipUtilTest { @Test @Ignore public void unzipTest() { - File unzip = ZipUtil.unzip("E:\\aaa\\RongGenetor V1.0.0.zip", "e:\\aaa"); + File unzip = ZipUtil.unzip("f:/test/apache-maven-3.6.2.zip", "f:\\test"); Console.log(unzip); - File unzip2 = ZipUtil.unzip("E:\\aaa\\RongGenetor V1.0.0.zip", "e:\\aaa"); - Console.log(unzip2); } @Test