diff --git a/pom.xml b/pom.xml
index 68a3053..c7134e7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.imyeyu.compress
timi-compress
- 0.0.1
+ 0.0.2
true
diff --git a/src/main/java/com/imyeyu/compress/AbstractRunner.java b/src/main/java/com/imyeyu/compress/AbstractRunner.java
index da93009..100f45f 100644
--- a/src/main/java/com/imyeyu/compress/AbstractRunner.java
+++ b/src/main/java/com/imyeyu/compress/AbstractRunner.java
@@ -2,7 +2,6 @@ package com.imyeyu.compress;
import com.imyeyu.java.bean.CallbackArg;
import com.imyeyu.utils.OS;
-import lombok.Setter;
import java.io.File;
import java.io.FilterInputStream;
@@ -14,17 +13,16 @@ import java.io.OutputStream;
/**
* 抽象压缩执行器
*
+ * @param 处理器类型
* @author 夜雨
* @since 2024-06-30 18:09
*/
-public abstract class AbstractRunner implements OS.FileSystem {
+public abstract class AbstractRunner> implements OS.FileSystem {
/** 文件处理回调 */
- @Setter
protected CallbackArg fileCallback;
/** 进度回调 */
- @Setter
protected CallbackArg progressCallback;
/** 中断标记 */
@@ -42,6 +40,48 @@ public abstract class AbstractRunner implements OS.FileSystem {
/** 当前已处理字节数 */
private long progressBytes = 0L;
+ /**
+ * 设置文件处理回调
+ *
+ * @param fileCallback 文件处理回调
+ * @return 当前处理器
+ */
+ public T setFileCallback(CallbackArg fileCallback) {
+ this.fileCallback = fileCallback;
+ return self();
+ }
+
+ /**
+ * 设置文件处理回调
+ *
+ * @param fileCallback 文件处理回调
+ * @return 当前处理器
+ */
+ public T fileCallback(CallbackArg fileCallback) {
+ return setFileCallback(fileCallback);
+ }
+
+ /**
+ * 设置进度回调
+ *
+ * @param progressCallback 进度回调
+ * @return 当前处理器
+ */
+ public T setProgressCallback(CallbackArg progressCallback) {
+ this.progressCallback = progressCallback;
+ return self();
+ }
+
+ /**
+ * 设置进度回调
+ *
+ * @param progressCallback 进度回调
+ * @return 当前处理器
+ */
+ public T progressCallback(CallbackArg progressCallback) {
+ return setProgressCallback(progressCallback);
+ }
+
/** 暂停任务 */
public void pause() {
isPause = true;
@@ -60,6 +100,16 @@ public abstract class AbstractRunner implements OS.FileSystem {
isInterrupt = true;
}
+ /**
+ * 返回当前处理器
+ *
+ * @return 当前处理器
+ */
+ @SuppressWarnings("unchecked")
+ protected final T self() {
+ return (T) this;
+ }
+
/**
* 初始化字节进度
*
diff --git a/src/main/java/com/imyeyu/compress/CompressType.java b/src/main/java/com/imyeyu/compress/CompressType.java
index 3566741..92a3952 100644
--- a/src/main/java/com/imyeyu/compress/CompressType.java
+++ b/src/main/java/com/imyeyu/compress/CompressType.java
@@ -31,12 +31,12 @@ public enum CompressType {
TAR(TarCompressor.class, TarDecompressor.class);
/** 压缩类 */
- final Class extends Compressor> compressorType;
+ final Class extends Compressor>> compressorType;
/** 解压类 */
- final Class extends Decompressor> decompressorType;
+ final Class extends Decompressor>> decompressorType;
- CompressType(Class extends Compressor> compressorType, Class extends Decompressor> decompressorType) {
+ CompressType(Class extends Compressor>> compressorType, Class extends Decompressor>> decompressorType) {
this.compressorType = compressorType;
this.decompressorType = decompressorType;
}
@@ -47,7 +47,7 @@ public enum CompressType {
* @return 压缩操作对象
* @throws Exception 实例化失败
*/
- public Compressor getCompressor() throws Exception {
+ public Compressor> getCompressor() throws Exception {
return Ref.newInstance(compressorType);
}
@@ -57,19 +57,20 @@ public enum CompressType {
* @return 解压操作对象
* @throws Exception 实例化失败
*/
- public Decompressor getDecompressor() throws Exception {
+ public Decompressor> getDecompressor() throws Exception {
return Ref.newInstance(decompressorType);
}
/**
- * 根据文件获取解压操作对象。会读取文件头识别压缩格式
+ * 根据文件获取解压操作对象
+ * 会读取文件头识别压缩格式
*
* @param file 文件
* @return 解压操作对象
* @throws UnsupportedOperationException 不支持的文件
* @throws Exception 实例化失败
*/
- public static Decompressor fromFile(File file) throws Exception {
+ public static Decompressor> fromFile(File file) throws Exception {
try (InputStream inputStream = IO.getInputStream(file)) {
byte[] head = new byte[512];
int length = inputStream.read(head);
diff --git a/src/main/java/com/imyeyu/compress/Compressor.java b/src/main/java/com/imyeyu/compress/Compressor.java
index c92894a..0bcffba 100644
--- a/src/main/java/com/imyeyu/compress/Compressor.java
+++ b/src/main/java/com/imyeyu/compress/Compressor.java
@@ -8,21 +8,49 @@ import java.io.OutputStream;
/**
* 抽象压缩器
*
+ * @param 压缩器类型
* @author 夜雨
* @version 2024-06-30 10:34
*/
-public abstract class Compressor extends AbstractRunner {
+public abstract class Compressor> extends AbstractRunner {
/**
* 将指定路径下的文件压缩到目标文件
*
* @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;
}
diff --git a/src/main/java/com/imyeyu/compress/Decompressor.java b/src/main/java/com/imyeyu/compress/Decompressor.java
index dc5b9b2..c724803 100644
--- a/src/main/java/com/imyeyu/compress/Decompressor.java
+++ b/src/main/java/com/imyeyu/compress/Decompressor.java
@@ -8,22 +8,23 @@ import java.io.InputStream;
/**
* 抽象解压器
*
+ * @param 解压器类型
* @author 夜雨
* @version 2024-06-30 18:02
*/
-public abstract class Decompressor extends AbstractRunner {
+public abstract class Decompressor> extends AbstractRunner {
/**
* 将压缩文件解压到目标目录
*
* @param fromFile 源压缩文件
* @param toPath 目标目录
+ * @return 当前解压器
* @throws Exception 解压失败
*/
- public void run(File fromFile, String toPath) throws Exception {
- try (InputStream inputStream = IO.getInputStream(fromFile)) {
- run(inputStream, toPath);
- }
+ public T run(File fromFile, String toPath) throws Exception {
+ doRunFromFile(fromFile, toPath);
+ return self();
}
/**
@@ -32,7 +33,33 @@ public abstract class Decompressor extends AbstractRunner {
*
* @param fromStream 源压缩输入流
* @param toPath 目标目录
+ * @return 当前解压器
* @throws Exception 解压失败
*/
- public abstract void run(InputStream fromStream, String toPath) throws Exception;
+ public T run(InputStream fromStream, String toPath) throws Exception {
+ doRun(fromStream, toPath);
+ return self();
+ }
+
+ /**
+ * 执行从文件解压
+ *
+ * @param fromFile 源压缩文件
+ * @param toPath 目标目录
+ * @throws Exception 解压失败
+ */
+ protected void doRunFromFile(File fromFile, String toPath) throws Exception {
+ try (InputStream inputStream = IO.getInputStream(fromFile)) {
+ doRun(inputStream, toPath);
+ }
+ }
+
+ /**
+ * 执行从输入流解压
+ *
+ * @param fromStream 源压缩输入流
+ * @param toPath 目标目录
+ * @throws Exception 解压失败
+ */
+ protected abstract void doRun(InputStream fromStream, String toPath) throws Exception;
}
diff --git a/src/main/java/com/imyeyu/compress/GZipCompressor.java b/src/main/java/com/imyeyu/compress/GZipCompressor.java
index 6a662c2..0e27f4d 100644
--- a/src/main/java/com/imyeyu/compress/GZipCompressor.java
+++ b/src/main/java/com/imyeyu/compress/GZipCompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
@@ -18,10 +19,39 @@ import java.util.List;
* @author 夜雨
* @version 2024-06-30 19:40
*/
-public class GZipCompressor extends Compressor {
+public class GZipCompressor extends Compressor {
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static GZipCompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static GZipCompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new GZipCompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(fromPath, IO.file(toPath));
+ }
@Override
- public void run(String fromPath, OutputStream toStream) throws Exception {
+ protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath);
List files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath();
diff --git a/src/main/java/com/imyeyu/compress/GZipDecompressor.java b/src/main/java/com/imyeyu/compress/GZipDecompressor.java
index 327d83c..c050424 100644
--- a/src/main/java/com/imyeyu/compress/GZipDecompressor.java
+++ b/src/main/java/com/imyeyu/compress/GZipDecompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
@@ -16,20 +17,49 @@ import java.io.OutputStream;
* @author 夜雨
* @version 2024-06-30 19:47
*/
-public class GZipDecompressor extends Decompressor {
+public class GZipDecompressor extends Decompressor {
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static GZipDecompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static GZipDecompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new GZipDecompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(IO.file(fromPath), toPath);
+ }
@Override
- public void run(File fromFile, String toPath) throws Exception {
+ protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
- super.run(fromFile, toPath);
+ super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
- public void run(InputStream fromStream, String toPath) throws Exception {
+ protected void doRun(InputStream fromStream, String toPath) throws Exception {
try (
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(nonClosing(fromStream));
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)
diff --git a/src/main/java/com/imyeyu/compress/TarCompressor.java b/src/main/java/com/imyeyu/compress/TarCompressor.java
index 505abf7..90d5426 100644
--- a/src/main/java/com/imyeyu/compress/TarCompressor.java
+++ b/src/main/java/com/imyeyu/compress/TarCompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
@@ -16,10 +17,39 @@ import java.util.List;
* @author 夜雨
* @version 2024-06-30 19:48
*/
-public class TarCompressor extends Compressor {
+public class TarCompressor extends Compressor {
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static TarCompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static TarCompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new TarCompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(fromPath, IO.file(toPath));
+ }
@Override
- public void run(String fromPath, OutputStream toStream) throws Exception {
+ protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath);
List files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath();
diff --git a/src/main/java/com/imyeyu/compress/TarDecompressor.java b/src/main/java/com/imyeyu/compress/TarDecompressor.java
index 4f2e31c..5f500a2 100644
--- a/src/main/java/com/imyeyu/compress/TarDecompressor.java
+++ b/src/main/java/com/imyeyu/compress/TarDecompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
@@ -14,20 +15,49 @@ import java.io.OutputStream;
* @author 夜雨
* @version 2024-06-30 19:48
*/
-public class TarDecompressor extends Decompressor {
+public class TarDecompressor extends Decompressor {
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static TarDecompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static TarDecompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new TarDecompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(IO.file(fromPath), toPath);
+ }
@Override
- public void run(File fromFile, String toPath) throws Exception {
+ protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
- super.run(fromFile, toPath);
+ super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
- public void run(InputStream fromStream, String toPath) throws Exception {
+ protected void doRun(InputStream fromStream, String toPath) throws Exception {
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(nonClosing(fromStream))) {
TarArchiveEntry entry;
boolean processed = false;
diff --git a/src/main/java/com/imyeyu/compress/Z7Compressor.java b/src/main/java/com/imyeyu/compress/Z7Compressor.java
index 23f1f8b..e8468da 100644
--- a/src/main/java/com/imyeyu/compress/Z7Compressor.java
+++ b/src/main/java/com/imyeyu/compress/Z7Compressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
@@ -17,10 +18,39 @@ import java.util.List;
* @author 夜雨
* @version 2024-06-30 19:40
*/
-public class Z7Compressor extends Compressor {
+public class Z7Compressor extends Compressor {
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static Z7Compressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static Z7Compressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new Z7Compressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(fromPath, IO.file(toPath));
+ }
@Override
- public void run(String fromPath, OutputStream toStream) throws Exception {
+ protected void doRun(String fromPath, OutputStream toStream) throws Exception {
Path tempFile = Files.createTempFile("timi-compress-", ".7z");
try {
run(fromPath, tempFile.toFile());
@@ -34,7 +64,7 @@ public class Z7Compressor extends Compressor {
}
@Override
- public void run(String fromPath, File toFile) throws Exception {
+ protected void doRunToFile(String fromPath, File toFile) throws Exception {
File fromFile = new File(fromPath);
List files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath();
diff --git a/src/main/java/com/imyeyu/compress/Z7Decompressor.java b/src/main/java/com/imyeyu/compress/Z7Decompressor.java
index 473fd3b..4996d41 100644
--- a/src/main/java/com/imyeyu/compress/Z7Decompressor.java
+++ b/src/main/java/com/imyeyu/compress/Z7Decompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
@@ -16,20 +17,49 @@ import java.nio.file.Path;
* @author 夜雨
* @version 2024-06-30 19:42
*/
-public class Z7Decompressor extends Decompressor {
+public class Z7Decompressor extends Decompressor {
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static Z7Decompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static Z7Decompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new Z7Decompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(IO.file(fromPath), toPath);
+ }
@Override
- public void run(File fromFile, String toPath) throws Exception {
+ protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
- super.run(fromFile, toPath);
+ super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
- public void run(InputStream fromStream, String toPath) throws Exception {
+ protected void doRun(InputStream fromStream, String toPath) throws Exception {
Path tempFile = writeTempFile(fromStream);
try (SevenZFile file = SevenZFile.builder().setFile(tempFile.toFile()).get()) {
SevenZArchiveEntry entry;
diff --git a/src/main/java/com/imyeyu/compress/ZipCompressor.java b/src/main/java/com/imyeyu/compress/ZipCompressor.java
index e82d897..5e3c962 100644
--- a/src/main/java/com/imyeyu/compress/ZipCompressor.java
+++ b/src/main/java/com/imyeyu/compress/ZipCompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import java.io.BufferedOutputStream;
import java.io.File;
@@ -16,10 +17,39 @@ import java.util.zip.ZipOutputStream;
* @author 夜雨
* @version 2024-06-30 19:46
*/
-public class ZipCompressor extends Compressor {
+public class ZipCompressor extends Compressor {
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static ZipCompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行压缩
+ *
+ * @param fromPath 源路径
+ * @param toPath 目标文件路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前压缩器
+ * @throws Exception 压缩失败
+ */
+ public static ZipCompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new ZipCompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(fromPath, IO.file(toPath));
+ }
@Override
- public void run(String fromPath, OutputStream toStream) throws Exception {
+ protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath);
List files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath();
diff --git a/src/main/java/com/imyeyu/compress/ZipDecompressor.java b/src/main/java/com/imyeyu/compress/ZipDecompressor.java
index d8f368d..e083789 100644
--- a/src/main/java/com/imyeyu/compress/ZipDecompressor.java
+++ b/src/main/java/com/imyeyu/compress/ZipDecompressor.java
@@ -1,6 +1,7 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
+import com.imyeyu.java.bean.CallbackArg;
import java.io.File;
import java.io.InputStream;
@@ -16,20 +17,49 @@ import java.util.zip.ZipInputStream;
* @author 夜雨
* @version 2024-06-30 19:47
*/
-public class ZipDecompressor extends Decompressor {
+public class ZipDecompressor extends Decompressor {
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static ZipDecompressor run(String fromPath, String toPath) throws Exception {
+ return run(fromPath, toPath, null, null);
+ }
+
+ /**
+ * 静态执行解压
+ *
+ * @param fromPath 源压缩文件路径
+ * @param toPath 目标目录路径
+ * @param fileCallback 文件处理回调
+ * @param progressCallback 进度回调
+ * @return 当前解压器
+ * @throws Exception 解压失败
+ */
+ public static ZipDecompressor run(String fromPath, String toPath, CallbackArg fileCallback, CallbackArg progressCallback) throws Exception {
+ return new ZipDecompressor()
+ .fileCallback(fileCallback)
+ .progressCallback(progressCallback)
+ .run(IO.file(fromPath), toPath);
+ }
@Override
- public void run(File fromFile, String toPath) throws Exception {
+ protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
- super.run(fromFile, toPath);
+ super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
- public void run(InputStream fromStream, String toPath) throws Exception {
+ protected void doRun(InputStream fromStream, String toPath) throws Exception {
try (ZipInputStream zipInputStream = new ZipInputStream(nonClosing(fromStream))) {
ZipEntry entry;
boolean processed = false;
diff --git a/src/test/java/test/GzipTest.java b/src/test/java/test/GzipTest.java
index 5127881..ae5b8ec 100644
--- a/src/test/java/test/GzipTest.java
+++ b/src/test/java/test/GzipTest.java
@@ -1,12 +1,17 @@
package test;
import com.imyeyu.compress.CompressType;
+import com.imyeyu.compress.GZipCompressor;
+import com.imyeyu.compress.GZipDecompressor;
import com.imyeyu.io.IO;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
/**
* @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class GzipTest {
@Test
- public void testCompress() throws Exception {
- File out = IO.file("testOut/test.gz");
- CompressType.GZIP.getCompressor().run("testSrc", out);
+ public void testStaticCompress() throws Exception {
+ GZipCompressor compressor = GZipCompressor.run("testSrc", "testOut/test.gz");
+ Assertions.assertNotNull(compressor);
+ }
+
+ @Test
+ public void testChainCompress() throws Exception {
+ File out = IO.file("testOut/test-chain.gz");
+ GZipCompressor compressor = new GZipCompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
+ Assertions.assertSame(
+ compressor,
+ compressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run("testSrc", out)
+ );
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.gz");
+ GZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@@ -35,15 +58,26 @@ public class GzipTest {
}
@Test
- public void testDecompressFromStream() throws Exception {
+ public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray();
}
File out = IO.dir("testOutDeStream/gzip");
+ GZipDecompressor decompressor = new GZipDecompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
- CompressType.GZIP.getDecompressor().run(inputStream, out.getAbsolutePath());
+ Assertions.assertSame(
+ decompressor,
+ decompressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run(inputStream, out.getAbsolutePath())
+ );
}
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
}
diff --git a/src/test/java/test/TarTest.java b/src/test/java/test/TarTest.java
index 69f0e0b..cd040e8 100644
--- a/src/test/java/test/TarTest.java
+++ b/src/test/java/test/TarTest.java
@@ -1,12 +1,17 @@
package test;
import com.imyeyu.compress.CompressType;
+import com.imyeyu.compress.TarCompressor;
+import com.imyeyu.compress.TarDecompressor;
import com.imyeyu.io.IO;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
/**
* @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class TarTest {
@Test
- public void testCompress() throws Exception {
- File out = IO.file("testOut/test.tar");
- CompressType.TAR.getCompressor().run("testSrc", out);
+ public void testStaticCompress() throws Exception {
+ TarCompressor compressor = TarCompressor.run("testSrc", "testOut/test.tar");
+ Assertions.assertNotNull(compressor);
+ }
+
+ @Test
+ public void testChainCompress() throws Exception {
+ File out = IO.file("testOut/test-chain.tar");
+ TarCompressor compressor = new TarCompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
+ Assertions.assertSame(
+ compressor,
+ compressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run("testSrc", out)
+ );
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.tar");
+ TarCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@@ -35,15 +58,26 @@ public class TarTest {
}
@Test
- public void testDecompressFromStream() throws Exception {
+ public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.TAR.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray();
}
File out = IO.dir("testOutDeStream/tar");
+ TarDecompressor decompressor = new TarDecompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
- CompressType.TAR.getDecompressor().run(inputStream, out.getAbsolutePath());
+ Assertions.assertSame(
+ decompressor,
+ decompressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run(inputStream, out.getAbsolutePath())
+ );
}
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
}
diff --git a/src/test/java/test/Z7Test.java b/src/test/java/test/Z7Test.java
index 267697f..9a8ef46 100644
--- a/src/test/java/test/Z7Test.java
+++ b/src/test/java/test/Z7Test.java
@@ -1,12 +1,17 @@
package test;
import com.imyeyu.compress.CompressType;
+import com.imyeyu.compress.Z7Compressor;
+import com.imyeyu.compress.Z7Decompressor;
import com.imyeyu.io.IO;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
/**
* @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class Z7Test {
@Test
- public void testCompress() throws Exception {
- File out = IO.file("testOut/test.7z");
- CompressType.Z7.getCompressor().run("testSrc", out);
+ public void testStaticCompress() throws Exception {
+ Z7Compressor compressor = Z7Compressor.run("testSrc", "testOut/test.7z");
+ Assertions.assertNotNull(compressor);
+ }
+
+ @Test
+ public void testChainCompress() throws Exception {
+ File out = IO.file("testOut/test-chain.7z");
+ Z7Compressor compressor = new Z7Compressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
+ Assertions.assertSame(
+ compressor,
+ compressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run("testSrc", out)
+ );
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.7z");
+ Z7Compressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@@ -35,15 +58,26 @@ public class Z7Test {
}
@Test
- public void testDecompressFromStream() throws Exception {
+ public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.Z7.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray();
}
File out = IO.dir("testOutDeStream/z7");
+ Z7Decompressor decompressor = new Z7Decompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
- CompressType.Z7.getDecompressor().run(inputStream, out.getAbsolutePath());
+ Assertions.assertSame(
+ decompressor,
+ decompressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run(inputStream, out.getAbsolutePath())
+ );
}
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
}
diff --git a/src/test/java/test/ZipTest.java b/src/test/java/test/ZipTest.java
index 1aa0bca..86f31b6 100644
--- a/src/test/java/test/ZipTest.java
+++ b/src/test/java/test/ZipTest.java
@@ -1,12 +1,17 @@
package test;
import com.imyeyu.compress.CompressType;
+import com.imyeyu.compress.ZipCompressor;
+import com.imyeyu.compress.ZipDecompressor;
import com.imyeyu.io.IO;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
/**
* @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class ZipTest {
@Test
- public void testCompress() throws Exception {
- File out = IO.file("testOut/test.zip");
- CompressType.ZIP.getCompressor().run("testSrc", out);
+ public void testStaticCompress() throws Exception {
+ ZipCompressor compressor = ZipCompressor.run("testSrc", "testOut/test.zip");
+ Assertions.assertNotNull(compressor);
+ }
+
+ @Test
+ public void testChainCompress() throws Exception {
+ File out = IO.file("testOut/test-chain.zip");
+ ZipCompressor compressor = new ZipCompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
+ Assertions.assertSame(
+ compressor,
+ compressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run("testSrc", out)
+ );
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.zip");
+ ZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@@ -35,15 +58,26 @@ public class ZipTest {
}
@Test
- public void testDecompressFromStream() throws Exception {
+ public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.ZIP.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray();
}
File out = IO.dir("testOutDeStream/zip");
+ ZipDecompressor decompressor = new ZipDecompressor();
+ AtomicInteger fileCount = new AtomicInteger();
+ AtomicReference progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
- CompressType.ZIP.getDecompressor().run(inputStream, out.getAbsolutePath());
+ Assertions.assertSame(
+ decompressor,
+ decompressor
+ .fileCallback(file -> fileCount.incrementAndGet())
+ .progressCallback(progress::set)
+ .run(inputStream, out.getAbsolutePath())
+ );
}
+ Assertions.assertTrue(0 < fileCount.get());
+ Assertions.assertEquals(1D, progress.get());
}
}