This commit is contained in:
@@ -8,21 +8,49 @@ import java.io.OutputStream;
|
||||
/**
|
||||
* 抽象压缩器
|
||||
*
|
||||
* @param <T> 压缩器类型
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 10:34
|
||||
*/
|
||||
public abstract class Compressor extends AbstractRunner {
|
||||
public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner<T> {
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到目标文件
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toFile 目标压缩文件
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public void run(String fromPath, File toFile) throws Exception {
|
||||
public T run(String fromPath, File toFile) throws Exception {
|
||||
doRunToFile(fromPath, toFile);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到输出流
|
||||
* 输出流由调用方管理
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toStream 目标输出流
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T run(String fromPath, OutputStream toStream) throws Exception {
|
||||
doRun(fromPath, toStream);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行压缩到目标文件
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toFile 目标压缩文件
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
protected void doRunToFile(String fromPath, File toFile) throws Exception {
|
||||
try (OutputStream outputStream = IO.getOutputStream(toFile)) {
|
||||
run(fromPath, outputStream);
|
||||
doRun(fromPath, outputStream);
|
||||
outputStream.flush();
|
||||
} catch (Exception exception) {
|
||||
if (isInterrupt) {
|
||||
@@ -36,12 +64,11 @@ public abstract class Compressor extends AbstractRunner {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到输出流
|
||||
* 输出流由调用方管理
|
||||
* 执行压缩到输出流
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toStream 目标输出流
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public abstract void run(String fromPath, OutputStream toStream) throws Exception;
|
||||
protected abstract void doRun(String fromPath, OutputStream toStream) throws Exception;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user