v0.0.3
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 13s

This commit is contained in:
Timi
2026-04-02 16:23:53 +08:00
parent 56dec33c94
commit b56f4e8969
19 changed files with 372 additions and 282 deletions

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.timi.TimiException;
import java.io.File;
import java.io.InputStream;
@@ -14,31 +15,66 @@ import java.io.InputStream;
*/
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
/** 源压缩文件 */
private File fromFile;
/** 源压缩输入流 */
private InputStream fromStream;
/**
* 压缩文件解压到目标目录
* 绑定源压缩文件
*
* @param fromFile 源压缩文件
* @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败
*/
public T run(File fromFile, String toPath) throws Exception {
doRunFromFile(fromFile, toPath);
public T from(File fromFile) {
TimiException.required(fromFile, "not found fromFile");
this.fromFile = fromFile;
this.fromStream = null;
return self();
}
/**
* 将压缩输入流解压到目标目录
* 输入流由调用方管理
* 绑定源压缩文件路径
*
* @param fromPath 源压缩文件路径
* @return 当前解压器
*/
public T from(String fromPath) {
TimiException.required(fromPath, "not found fromPath");
return from(new File(fromPath));
}
/**
* 绑定源压缩输入流
*
* @param fromStream 源压缩输入流
* @return 当前解压器
*/
public T from(InputStream fromStream) {
TimiException.required(fromStream, "not found fromStream");
this.fromStream = fromStream;
this.fromFile = null;
return self();
}
/**
* 解压到目标目录
*
* @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败
*/
public T run(InputStream fromStream, String toPath) throws Exception {
doRun(fromStream, toPath);
return self();
public T toPath(String toPath) throws Exception {
if (fromFile != null) {
toPath(fromFile, toPath);
return self();
}
if (fromStream != null) {
toPath(fromStream, toPath);
return self();
}
throw new IllegalStateException("not found source");
}
/**
@@ -48,9 +84,9 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录
* @throws Exception 解压失败
*/
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
protected void toPath(File fromFile, String toPath) throws Exception {
try (InputStream inputStream = IO.getInputStream(fromFile)) {
doRun(inputStream, toPath);
toPath(inputStream, toPath);
}
}
@@ -61,5 +97,5 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录
* @throws Exception 解压失败
*/
protected abstract void doRun(InputStream fromStream, String toPath) throws Exception;
protected abstract void toPath(InputStream fromStream, String toPath) throws Exception;
}