mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
add test
This commit is contained in:
parent
5cd7e806e1
commit
6b3fc153d7
@ -2,13 +2,17 @@ package cn.hutool.core.io.file;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* {@link FileSystem}相关工具类封装<br>
|
||||
@ -35,6 +39,39 @@ public class FileSystemUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 Zip的{@link FileSystem},默认UTF-8编码
|
||||
*
|
||||
* @param path 文件路径,可以是目录或Zip文件等
|
||||
* @return {@link FileSystem}
|
||||
*/
|
||||
public static FileSystem createZip(String path) {
|
||||
return createZip(path, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 Zip的{@link FileSystem}
|
||||
*
|
||||
* @param path 文件路径,可以是目录或Zip文件等
|
||||
* @param charset 编码
|
||||
* @return {@link FileSystem}
|
||||
*/
|
||||
public static FileSystem createZip(String path, Charset charset) {
|
||||
if(null == charset){
|
||||
charset = CharsetUtil.CHARSET_UTF_8;
|
||||
}
|
||||
final HashMap<String, String> env = new HashMap<>();
|
||||
env.put("create", "true");
|
||||
env.put("encoding", charset.name());
|
||||
|
||||
try {
|
||||
return FileSystems.newFileSystem(
|
||||
URI.create("jar:" + Paths.get(path).toUri()), env);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目录的根路径,或Zip文件中的根路径
|
||||
*
|
||||
|
@ -55,6 +55,19 @@ public class PathUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归遍历目录以及子目录中的所有文件<br>
|
||||
* 如果提供path为文件,直接返回过滤结果
|
||||
*
|
||||
* @param path 当前遍历文件或目录
|
||||
* @param fileFilter 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录,null表示接收全部文件
|
||||
* @return 文件列表
|
||||
* @since 5.4.1
|
||||
*/
|
||||
public static List<File> loopFiles(Path path, FileFilter fileFilter) {
|
||||
return loopFiles(path, -1, fileFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归遍历目录以及子目录中的所有文件<br>
|
||||
* 如果提供path为文件,直接返回过滤结果
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
||||
public class FileSystemUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void listTest(){
|
||||
final FileSystem fileSystem = FileSystemUtil.createZip("d:/test/test.zip",
|
||||
CharsetUtil.CHARSET_GBK);
|
||||
final Path root = FileSystemUtil.getRoot(fileSystem);
|
||||
PathUtil.walkFiles(root, new SimpleFileVisitor<Path>() {
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
|
||||
Console.log(path);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user