mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
|
||||
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
|
||||
@@ -125,14 +126,23 @@ public class SevenZArchiver implements Archiver {
|
||||
}
|
||||
final SevenZOutputFile out = this.sevenZOutputFile;
|
||||
|
||||
final String entryName = StrUtil.addSuffixIfNot(StrUtil.nullToEmpty(path), StrUtil.SLASH) + file.getName();
|
||||
final String entryName;
|
||||
if(StrUtil.isNotEmpty(path)){
|
||||
// 非空拼接路径,格式为:path/name
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + file.getName();
|
||||
} else{
|
||||
// 路径空直接使用文件名或目录名
|
||||
entryName = file.getName();
|
||||
}
|
||||
out.putArchiveEntry(out.createArchiveEntry(file, entryName));
|
||||
|
||||
if (file.isDirectory()) {
|
||||
// 目录遍历写入
|
||||
final File[] files = file.listFiles();
|
||||
for (File childFile : files) {
|
||||
addInternal(childFile, entryName, filter);
|
||||
if(ArrayUtil.isNotEmpty(files)){
|
||||
for (File childFile : files) {
|
||||
addInternal(childFile, entryName, filter);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (file.isFile()) {
|
||||
|
@@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.compress.CompressException;
|
||||
import org.apache.commons.compress.archivers.ArchiveException;
|
||||
@@ -140,7 +141,7 @@ public class StreamArchiver implements Archiver {
|
||||
* 将文件或目录加入归档包,目录采取递归读取方式按照层级加入
|
||||
*
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,null表示位于根路径
|
||||
* @param path 文件或目录的初始路径,{@code null}表示位于根路径
|
||||
* @param filter 文件过滤器,指定哪些文件或目录可以加入,当{@link Filter#accept(Object)}为true时加入。
|
||||
*/
|
||||
private void addInternal(File file, String path, Filter<File> filter) throws IOException {
|
||||
@@ -149,14 +150,23 @@ public class StreamArchiver implements Archiver {
|
||||
}
|
||||
final ArchiveOutputStream out = this.out;
|
||||
|
||||
final String entryName = StrUtil.addSuffixIfNot(StrUtil.nullToEmpty(path), StrUtil.SLASH) + file.getName();
|
||||
final String entryName;
|
||||
if(StrUtil.isNotEmpty(path)){
|
||||
// 非空拼接路径,格式为:path/name
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + file.getName();
|
||||
} else{
|
||||
// 路径空直接使用文件名或目录名
|
||||
entryName = file.getName();
|
||||
}
|
||||
out.putArchiveEntry(out.createArchiveEntry(file, entryName));
|
||||
|
||||
if (file.isDirectory()) {
|
||||
// 目录遍历写入
|
||||
final File[] files = file.listFiles();
|
||||
for (File childFile : files) {
|
||||
addInternal(childFile, entryName, filter);
|
||||
if(ArrayUtil.isNotEmpty(files)){
|
||||
for (File childFile : files) {
|
||||
addInternal(childFile, entryName, filter);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (file.isFile()) {
|
||||
|
Reference in New Issue
Block a user