mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix listFiles bug
This commit is contained in:
parent
4f4dc6a5a3
commit
08dce0cdae
@ -8,9 +8,11 @@
|
||||
### 新特性
|
||||
* 【core】 MapUtil增加newConcurrentHashMap(pr#538@Github)
|
||||
* 【core】 增加StopWatch(issuepr#539@Github)
|
||||
* 【core】 增加ZipUtil.listFiles(issuepr#541@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【core】 修复DateUtil.endOfYear计算错误问题(issuepr#540@Github)
|
||||
* 【core】 修复FileUtil.listFileNames在jar中匹配问题,增加(issuepr#541@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -34,14 +34,12 @@ import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.CRC32;
|
||||
@ -64,6 +62,7 @@ import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
@ -111,8 +110,6 @@ public class FileUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
path = getAbsolutePath(path);
|
||||
|
||||
File file = file(path);
|
||||
if (file.isDirectory()) {
|
||||
return file.listFiles();
|
||||
@ -311,14 +308,13 @@ public class FileUtil {
|
||||
*/
|
||||
public static List<String> listFileNames(String path) throws IORuntimeException {
|
||||
if (path == null) {
|
||||
return null;
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
List<String> paths = new ArrayList<String>();
|
||||
|
||||
int index = path.lastIndexOf(FileUtil.JAR_PATH_EXT);
|
||||
if (index == -1) {
|
||||
// 普通目录路径
|
||||
File[] files = ls(path);
|
||||
if (index < 0) {
|
||||
// 普通目录
|
||||
final List<String> paths = new ArrayList<String>();
|
||||
final File[] files = ls(path);
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
paths.add(file.getName());
|
||||
@ -327,31 +323,20 @@ public class FileUtil {
|
||||
} else {
|
||||
// jar文件
|
||||
path = getAbsolutePath(path);
|
||||
if (false == StrUtil.endWith(path, UNIX_SEPARATOR)) {
|
||||
path = path + UNIX_SEPARATOR;
|
||||
}
|
||||
// jar文件中的路径
|
||||
index = index + FileUtil.JAR_FILE_EXT.length();
|
||||
JarFile jarFile = null;
|
||||
try {
|
||||
jarFile = new JarFile(path.substring(0, index));
|
||||
final String subPath = path.substring(index + 2);
|
||||
for (JarEntry entry : Collections.list(jarFile.entries())) {
|
||||
final String name = entry.getName();
|
||||
if (name.startsWith(subPath)) {
|
||||
final String nameSuffix = StrUtil.removePrefix(name, subPath);
|
||||
if (false == StrUtil.contains(nameSuffix, UNIX_SEPARATOR)) {
|
||||
paths.add(nameSuffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ZipUtil.listFileNames(jarFile, path.substring(index + 1));
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(StrUtil.format("Can not read file path of [{}]", path), e);
|
||||
} finally {
|
||||
IoUtil.close(jarFile);
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,7 +9,10 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
@ -144,7 +147,7 @@ public class ZipUtil {
|
||||
public static File zip(File zipFile, boolean withSrcDir, File... srcFiles) throws UtilException {
|
||||
return zip(zipFile, DEFAULT_CHARSET, withSrcDir, srcFiles);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对文件或文件目录进行压缩
|
||||
*
|
||||
@ -818,6 +821,35 @@ public class ZipUtil {
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Zip文件中指定目录下的所有文件,只显示文件,不显示目录
|
||||
*
|
||||
* @param zipFile Zip文件
|
||||
* @param dir 目录前缀
|
||||
* @return 文件列表
|
||||
* @since 4.6.6
|
||||
*/
|
||||
public static List<String> listFileNames(ZipFile zipFile, String dir) {
|
||||
if (StrUtil.isNotBlank(dir)) {
|
||||
// 目录尾部添加"/"
|
||||
dir = StrUtil.addSuffixIfNot(dir, StrUtil.SLASH);
|
||||
}
|
||||
|
||||
final List<String> fileNames = new ArrayList<>();
|
||||
String name;
|
||||
for (ZipEntry entry : Collections.list(zipFile.entries())) {
|
||||
name = entry.getName();
|
||||
if (StrUtil.isEmpty(dir) || name.startsWith(dir)) {
|
||||
final String nameSuffix = StrUtil.removePrefix(name, dir);
|
||||
if (StrUtil.isNotEmpty(nameSuffix) && false == StrUtil.contains(nameSuffix, CharUtil.SLASH)) {
|
||||
fileNames.add(nameSuffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fileNames;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 获得 {@link ZipOutputStream}
|
||||
@ -854,7 +886,7 @@ public class ZipUtil {
|
||||
* @throws UtilException IO异常
|
||||
*/
|
||||
private static void zip(File file, String srcRootDir, ZipOutputStream out, FileFilter filter) throws UtilException {
|
||||
if(null == file || (null != filter && false == filter.accept(file))) {
|
||||
if (null == file || (null != filter && false == filter.accept(file))) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -222,6 +222,15 @@ public class FileUtilTest {
|
||||
names = FileUtil.listFileNames(".");
|
||||
Assert.assertTrue(names.contains("hutool.jpg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void listFileNamesTest2() {
|
||||
List<String> names = FileUtil.listFileNames("D:\\m2_repo\\commons-cli\\commons-cli\\1.0\\commons-cli-1.0.jar!org/apache/commons/cli/");
|
||||
for (String string : names) {
|
||||
Console.log(string);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
|
Loading…
x
Reference in New Issue
Block a user