add mime support

This commit is contained in:
Looly 2022-03-24 21:58:02 +08:00
parent 692846c983
commit 0b49530c1d
4 changed files with 27 additions and 8 deletions

View File

@ -54,6 +54,8 @@
* 【poi 】 解决sax读取时POI-5.2.x兼容性问题 * 【poi 】 解决sax读取时POI-5.2.x兼容性问题
* 【core 】 修复判断两段时间区间交集问题pr#2210@Github * 【core 】 修复判断两段时间区间交集问题pr#2210@Github
* 【http 】 修复标签误删问题issue#I4Z7BV@Gitee * 【http 】 修复标签误删问题issue#I4Z7BV@Gitee
* 【core 】 修复Win下文件名带*问题pr#584@Gitee
* 【core 】 FileUtil.getMimeType增加rar、7z支持issue#I4ZBN0@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.22 (2022-03-01) # 5.7.22 (2022-03-01)

View File

@ -30,22 +30,22 @@ public class ZipReader implements Closeable {
private ZipInputStream in; private ZipInputStream in;
/** /**
* 创建{@link ZipReader} * 创建ZipReader
* *
* @param zipFile 生成的Zip文件 * @param zipFile 生成的Zip文件
* @param charset 编码 * @param charset 编码
* @return {@link ZipReader} * @return ZipReader
*/ */
public static ZipReader of(File zipFile, Charset charset) { public static ZipReader of(File zipFile, Charset charset) {
return new ZipReader(zipFile, charset); return new ZipReader(zipFile, charset);
} }
/** /**
* 创建{@link ZipReader} * 创建ZipReader
* *
* @param in Zip输入的流一般为输入文件流 * @param in Zip输入的流一般为输入文件流
* @param charset 编码 * @param charset 编码
* @return {@link ZipReader} * @return ZipReader
*/ */
public static ZipReader of(InputStream in, Charset charset) { public static ZipReader of(InputStream in, Charset charset) {
return new ZipReader(in, charset); return new ZipReader(in, charset);
@ -108,7 +108,7 @@ public class ZipReader implements Closeable {
this.in.reset(); this.in.reset();
ZipEntry zipEntry; ZipEntry zipEntry;
while (null != (zipEntry = in.getNextEntry())) { while (null != (zipEntry = in.getNextEntry())) {
if(zipEntry.getName().equals(path)){ if (zipEntry.getName().equals(path)) {
return this.in; return this.in;
} }
} }
@ -145,7 +145,7 @@ public class ZipReader implements Closeable {
if (null == entryFilter || entryFilter.accept(zipEntry)) { if (null == entryFilter || entryFilter.accept(zipEntry)) {
//gitee issue #I4ZDQI //gitee issue #I4ZDQI
String path = zipEntry.getName(); String path = zipEntry.getName();
if (System.getProperty("os.name").toLowerCase().startsWith("win")) { if (FileUtil.isWindows()) {
path = StrUtil.replace(zipEntry.getName(), "*", "_"); path = StrUtil.replace(zipEntry.getName(), "*", "_");
} }
// FileUtil.file会检查slip漏洞漏洞说明见http://blog.nsfocus.net/zip-slip-2/ // FileUtil.file会检查slip漏洞漏洞说明见http://blog.nsfocus.net/zip-slip-2/

View File

@ -3441,10 +3441,14 @@ public class FileUtil extends PathUtil {
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath); String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
if (null == contentType) { if (null == contentType) {
// 补充一些常用的mimeType // 补充一些常用的mimeType
if (filePath.endsWith(".css")) { if (StrUtil.endWithIgnoreCase(filePath, ".css")) {
contentType = "text/css"; contentType = "text/css";
} else if (filePath.endsWith(".js")) { } else if (StrUtil.endWithIgnoreCase(filePath, ".js")) {
contentType = "application/x-javascript"; contentType = "application/x-javascript";
} else if (StrUtil.endWithIgnoreCase(filePath, ".rar")) {
contentType = "application/x-rar-compressed";
} else if (StrUtil.endWithIgnoreCase(filePath, ".7z")) {
contentType = "application/x-7z-compressed";
} }
} }

View File

@ -1,5 +1,6 @@
package cn.hutool.core.io.file; package cn.hutool.core.io.file;
import cn.hutool.core.io.FileUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -65,4 +66,16 @@ public class PathUtilTest {
mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.mov")); mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.mov"));
Assert.assertEquals("video/quicktime", mimeType); Assert.assertEquals("video/quicktime", mimeType);
} }
@Test
public void getMimeOfRarTest(){
String contentType = FileUtil.getMimeType("a001.rar");
Assert.assertEquals("application/x-rar-compressed", contentType);
}
@Test
public void getMimeOf7zTest(){
String contentType = FileUtil.getMimeType("a001.7z");
Assert.assertEquals("application/x-7z-compressed", contentType);
}
} }