fix ZipUtil bug

This commit is contained in:
Looly
2020-11-26 13:17:24 +08:00
parent 949dd9bbb5
commit 994a7a49bf
5 changed files with 59 additions and 17 deletions

View File

@@ -459,13 +459,12 @@ public class ZipUtil {
* @throws IORuntimeException IO异常
* @since 4.5.8
*/
@SuppressWarnings("unchecked")
public static File unzip(ZipFile zipFile, File outFile) throws IORuntimeException {
if(outFile.exists() && outFile.isFile()){
throw new UtilException("Target path [{}] exist!", outFile.getAbsolutePath());
}
try {
final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFile.entries();
final Enumeration<? extends ZipEntry> em = zipFile.entries();
ZipEntry zipEntry;
File outItemFile;
while (em.hasMoreElements()) {
@@ -487,6 +486,40 @@ public class ZipUtil {
return outFile;
}
/**
* 获取压缩包中的指定文件流
* @param zipFile 压缩文件
* @param path 需要提取文件的文件名或路径
* @return 压缩文件流,如果未找到返回{@code null}
* @since 5.5.2
*/
public static InputStream get(File zipFile, Charset charset, String path){
try {
return get(new ZipFile(zipFile, charset), path);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 获取压缩包中的指定文件流
* @param zipFile 压缩文件
* @param path 需要提取文件的文件名或路径
* @return 压缩文件流,如果未找到返回{@code null}
* @since 5.5.2
*/
public static InputStream get(ZipFile zipFile, String path){
final ZipEntry entry = zipFile.getEntry(path);
if(null != entry){
try {
return zipFile.getInputStream(entry);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
return null;
}
/**
* 解压<br>
* ZIP条目不使用高速缓冲。
@@ -1024,15 +1057,9 @@ public class ZipUtil {
throw new UtilException(StrUtil.format("File [{}] not exist!", srcFile.getAbsolutePath()));
}
try {
final File parentFile = zipFile.getCanonicalFile().getParentFile();
// 压缩文件不能位于被压缩的目录内
if (srcFile.isDirectory() && parentFile.getCanonicalPath().contains(srcFile.getCanonicalPath())) {
throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getCanonicalPath(), srcFile.getCanonicalPath());
}
} catch (IOException e) {
throw new UtilException(e);
// 压缩文件不能位于被压缩的目录内
if(srcFile.isDirectory() && FileUtil.isSub(srcFile, zipFile.getParentFile())){
throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getPath(), srcFile.getPath());
}
}
}

View File

@@ -377,4 +377,11 @@ public class FileUtilTest {
String mimeType = FileUtil.getMimeType("test2Write.jpg");
Assert.assertEquals("image/jpeg", mimeType);
}
@Test
public void isSubTest() {
File file = new File("d:/test");
File file2 = new File("d:/test2/aaa");
Assert.assertFalse(FileUtil.isSub(file, file2));
}
}