mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add fileNameEditor
This commit is contained in:
@@ -14,6 +14,7 @@ package org.dromara.hutool.extra.compress.archiver;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@@ -52,7 +53,21 @@ public interface Archiver extends Closeable {
|
||||
* @param filter 文件过滤器,指定哪些文件或目录可以加入,{@link Predicate#test(Object)}为{@code true}保留,null表示全部加入
|
||||
* @return this
|
||||
*/
|
||||
Archiver add(File file, String path, Predicate<File> filter);
|
||||
default Archiver add(final File file, final String path, final Predicate<File> filter){
|
||||
return add(file, path, Function.identity(), filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件或目录加入归档包,目录采取递归读取方式按照层级加入
|
||||
*
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,null表示位于根路径
|
||||
* @param fileNameEditor 文件名编辑器
|
||||
* @param filter 文件过滤器,指定哪些文件或目录可以加入,{@link Predicate#test(Object)}为{@code true}保留,null表示全部加入
|
||||
* @return this
|
||||
* @since 6.0.0
|
||||
*/
|
||||
Archiver add(File file, String path, Function<String, String> fileNameEditor, Predicate<File> filter);
|
||||
|
||||
/**
|
||||
* 结束已经增加的文件归档,此方法不会关闭归档流,可以继续添加文件
|
||||
|
@@ -24,6 +24,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@@ -89,9 +90,9 @@ public class SevenZArchiver implements Archiver {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SevenZArchiver add(final File file, final String path, final Predicate<File> filter) {
|
||||
public SevenZArchiver add(final File file, final String path, final Function<String, String> fileNameEditor, final Predicate<File> filter) {
|
||||
try {
|
||||
addInternal(file, path, filter);
|
||||
addInternal(file, path, fileNameEditor, filter);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
@@ -128,23 +129,21 @@ public class SevenZArchiver implements Archiver {
|
||||
/**
|
||||
* 将文件或目录加入归档包,目录采取递归读取方式按照层级加入
|
||||
*
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,null表示位于根路径
|
||||
* @param filter 文件过滤器,指定哪些文件或目录可以加入,当{@link Predicate#test(Object)}为{@code true}保留,null表示保留全部
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,null表示位于根路径
|
||||
* @param fileNameEditor 文件名编辑器
|
||||
* @param filter 文件过滤器,指定哪些文件或目录可以加入,当{@link Predicate#test(Object)}为{@code true}保留,null表示保留全部
|
||||
*/
|
||||
private void addInternal(final File file, final String path, final Predicate<File> filter) throws IOException {
|
||||
private void addInternal(final File file, final String path, final Function<String, String> fileNameEditor, final Predicate<File> filter) throws IOException {
|
||||
if (null != filter && !filter.test(file)) {
|
||||
return;
|
||||
}
|
||||
final SevenZOutputFile out = this.sevenZOutputFile;
|
||||
|
||||
final String entryName;
|
||||
String entryName = (null == fileNameEditor) ? file.getName() : fileNameEditor.apply(file.getName());
|
||||
if (StrUtil.isNotEmpty(path)) {
|
||||
// 非空拼接路径,格式为:path/name
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + file.getName();
|
||||
} else {
|
||||
// 路径空直接使用文件名或目录名
|
||||
entryName = file.getName();
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + entryName;
|
||||
}
|
||||
out.putArchiveEntry(out.createArchiveEntry(file, entryName));
|
||||
|
||||
@@ -153,7 +152,7 @@ public class SevenZArchiver implements Archiver {
|
||||
final File[] files = file.listFiles();
|
||||
if (ArrayUtil.isNotEmpty(files)) {
|
||||
for (final File childFile : files) {
|
||||
addInternal(childFile, entryName, filter);
|
||||
addInternal(childFile, entryName, fileNameEditor, filter);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@@ -30,6 +30,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@@ -116,20 +117,10 @@ public class StreamArchiver implements Archiver {
|
||||
((ArArchiveOutputStream) out).setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件或目录加入归档包,目录采取递归读取方式按照层级加入
|
||||
*
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,null表示位于根路径
|
||||
* @param predicate 文件过滤器,指定哪些文件或目录可以加入,当{@link Predicate#test(Object)}为{@code true}加入,null表示全部加入
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
@Override
|
||||
public StreamArchiver add(final File file, final String path, final Predicate<File> predicate) throws IORuntimeException {
|
||||
public StreamArchiver add(final File file, final String path, final Function<String, String> fileNameEditor, final Predicate<File> predicate) throws IORuntimeException {
|
||||
try {
|
||||
addInternal(file, path, predicate);
|
||||
addInternal(file, path, fileNameEditor, predicate);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
@@ -165,24 +156,22 @@ public class StreamArchiver implements Archiver {
|
||||
/**
|
||||
* 将文件或目录加入归档包,目录采取递归读取方式按照层级加入
|
||||
*
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,{@code null}表示位于根路径
|
||||
* @param predicate 文件过滤器,指定哪些文件或目录可以加入,当{@link Predicate#test(Object)}为{@code true}加入。
|
||||
* @param file 文件或目录
|
||||
* @param path 文件或目录的初始路径,{@code null}表示位于根路径
|
||||
* @param fileNameEditor 文件名编辑器
|
||||
* @param predicate 文件过滤器,指定哪些文件或目录可以加入,当{@link Predicate#test(Object)}为{@code true}加入。
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private void addInternal(final File file, final String path, final Predicate<File> predicate) throws IOException {
|
||||
private void addInternal(final File file, final String path, final Function<String, String> fileNameEditor, final Predicate<File> predicate) throws IOException {
|
||||
if (null != predicate && !predicate.test(file)) {
|
||||
return;
|
||||
}
|
||||
final ArchiveOutputStream out = this.out;
|
||||
|
||||
final String entryName;
|
||||
String entryName = (fileNameEditor == null) ? file.getName() : fileNameEditor.apply(file.getName());
|
||||
if (StrUtil.isNotEmpty(path)) {
|
||||
// 非空拼接路径,格式为:path/name
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + file.getName();
|
||||
} else {
|
||||
// 路径空直接使用文件名或目录名
|
||||
entryName = file.getName();
|
||||
entryName = StrUtil.addSuffixIfNot(path, StrUtil.SLASH) + entryName;
|
||||
}
|
||||
out.putArchiveEntry(out.createArchiveEntry(file, entryName));
|
||||
|
||||
@@ -191,7 +180,7 @@ public class StreamArchiver implements Archiver {
|
||||
final File[] files = file.listFiles();
|
||||
if (ArrayUtil.isNotEmpty(files)) {
|
||||
for (final File childFile : files) {
|
||||
addInternal(childFile, entryName, predicate);
|
||||
addInternal(childFile, entryName, fileNameEditor, predicate);
|
||||
}
|
||||
} else {
|
||||
// 空文件夹也需要关闭
|
||||
|
Reference in New Issue
Block a user