102 lines
2.2 KiB
Java
102 lines
2.2 KiB
Java
package com.imyeyu.compress;
|
|
|
|
import com.imyeyu.io.IO;
|
|
import com.imyeyu.java.bean.timi.TimiException;
|
|
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
|
|
/**
|
|
* 抽象解压器
|
|
*
|
|
* @param <T> 解压器类型
|
|
* @author 夜雨
|
|
* @version 2024-06-30 18:02
|
|
*/
|
|
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
|
|
|
|
/** 源压缩文件 */
|
|
private File fromFile;
|
|
|
|
/** 源压缩输入流 */
|
|
private InputStream fromStream;
|
|
|
|
/**
|
|
* 绑定源压缩文件
|
|
*
|
|
* @param fromFile 源压缩文件
|
|
* @return 当前解压器
|
|
*/
|
|
protected T of(File fromFile) {
|
|
TimiException.required(fromFile, "not found fromFile");
|
|
this.fromFile = fromFile;
|
|
this.fromStream = null;
|
|
return self();
|
|
}
|
|
|
|
/**
|
|
* 绑定源压缩文件路径
|
|
*
|
|
* @param fromPath 源压缩文件路径
|
|
* @return 当前解压器
|
|
*/
|
|
protected T of(String fromPath) {
|
|
TimiException.required(fromPath, "not found fromPath");
|
|
return of(new File(fromPath));
|
|
}
|
|
|
|
/**
|
|
* 绑定源压缩输入流
|
|
*
|
|
* @param fromStream 源压缩输入流
|
|
* @return 当前解压器
|
|
*/
|
|
public T of(InputStream fromStream) {
|
|
TimiException.required(fromStream, "not found fromStream");
|
|
this.fromStream = fromStream;
|
|
this.fromFile = null;
|
|
return self();
|
|
}
|
|
|
|
/**
|
|
* 解压到目标目录
|
|
*
|
|
* @param toPath 目标目录
|
|
* @return 当前解压器
|
|
* @throws Exception 解压失败
|
|
*/
|
|
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");
|
|
}
|
|
|
|
/**
|
|
* 执行从文件解压
|
|
*
|
|
* @param fromFile 源压缩文件
|
|
* @param toPath 目标目录
|
|
* @throws Exception 解压失败
|
|
*/
|
|
protected void toPath(File fromFile, String toPath) throws Exception {
|
|
try (InputStream inputStream = IO.getInputStream(fromFile)) {
|
|
toPath(inputStream, toPath);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 执行从输入流解压
|
|
*
|
|
* @param fromStream 源压缩输入流
|
|
* @param toPath 目标目录
|
|
* @throws Exception 解压失败
|
|
*/
|
|
protected abstract void toPath(InputStream fromStream, String toPath) throws Exception;
|
|
}
|