This commit is contained in:
@@ -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.OutputStream;
|
||||
@@ -14,33 +15,78 @@ import java.io.OutputStream;
|
||||
*/
|
||||
public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner<T> {
|
||||
|
||||
/** 源路径 */
|
||||
private String fromPath;
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到目标文件
|
||||
* 绑定源路径
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toFile 目标压缩文件
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T run(String fromPath, File toFile) throws Exception {
|
||||
doRunToFile(fromPath, toFile);
|
||||
public T from(String fromPath) {
|
||||
TimiException.required(fromPath, "not found fromPath");
|
||||
this.fromPath = fromPath;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到输出流
|
||||
* 绑定源文件
|
||||
*
|
||||
* @param fromFile 源文件
|
||||
* @return 当前压缩器
|
||||
*/
|
||||
public T from(File fromFile) {
|
||||
TimiException.required(fromFile, "not found fromFile");
|
||||
return from(fromFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩到目标文件
|
||||
*
|
||||
* @param toFile 目标压缩文件
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T toFile(File toFile) throws Exception {
|
||||
toFile(requireFromPath(), toFile);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩到目标文件路径
|
||||
*
|
||||
* @param toPath 目标压缩文件路径
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T toFile(String toPath) throws Exception {
|
||||
return toFile(IO.file(toPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩到输出流
|
||||
* 输出流由调用方管理
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toStream 目标输出流
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T run(String fromPath, OutputStream toStream) throws Exception {
|
||||
doRun(fromPath, toStream);
|
||||
public T toStream(OutputStream toStream) throws Exception {
|
||||
toStream(requireFromPath(), toStream);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已绑定的源路径
|
||||
*
|
||||
* @return 源路径
|
||||
*/
|
||||
protected String requireFromPath() {
|
||||
TimiException.required(fromPath, "not found source");
|
||||
return fromPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行压缩到目标文件
|
||||
*
|
||||
@@ -48,9 +94,9 @@ public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner
|
||||
* @param toFile 目标压缩文件
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
protected void doRunToFile(String fromPath, File toFile) throws Exception {
|
||||
protected void toFile(String fromPath, File toFile) throws Exception {
|
||||
try (OutputStream outputStream = IO.getOutputStream(toFile)) {
|
||||
doRun(fromPath, outputStream);
|
||||
toStream(fromPath, outputStream);
|
||||
outputStream.flush();
|
||||
} catch (Exception exception) {
|
||||
if (isInterrupt) {
|
||||
@@ -70,5 +116,5 @@ public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner
|
||||
* @param toStream 目标输出流
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
protected abstract void doRun(String fromPath, OutputStream toStream) throws Exception;
|
||||
protected abstract void toStream(String fromPath, OutputStream toStream) throws Exception;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user