fix zip bug

This commit is contained in:
Looly 2019-11-09 18:29:20 +08:00
parent a3b3743f3f
commit 04cf410cce
4 changed files with 54 additions and 72 deletions

View File

@ -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
-------------------------------------------------------------------------------------------------------------

View File

@ -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
}

View File

@ -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<String, Object> 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<String, Object> 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;
}
}
}

View File

@ -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