Compare commits
2 Commits
88b1fd6e02
...
v0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 913b9b5328 | |||
|
|
56dec33c94 |
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.compress</groupId>
|
||||
<artifactId>timi-compress</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
|
||||
<properties>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
|
||||
@@ -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 <T> 处理器类型
|
||||
* @author 夜雨
|
||||
* @since 2024-06-30 18:09
|
||||
*/
|
||||
public abstract class AbstractRunner implements OS.FileSystem {
|
||||
public abstract class AbstractRunner<T extends AbstractRunner<T>> implements OS.FileSystem {
|
||||
|
||||
/** 文件处理回调 */
|
||||
@Setter
|
||||
protected CallbackArg<File> fileCallback;
|
||||
|
||||
/** 进度回调 */
|
||||
@Setter
|
||||
protected CallbackArg<Double> progressCallback;
|
||||
|
||||
/** 中断标记 */
|
||||
@@ -42,6 +40,48 @@ public abstract class AbstractRunner implements OS.FileSystem {
|
||||
/** 当前已处理字节数 */
|
||||
private long progressBytes = 0L;
|
||||
|
||||
/**
|
||||
* 设置文件处理回调
|
||||
*
|
||||
* @param fileCallback 文件处理回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T setFileCallback(CallbackArg<File> fileCallback) {
|
||||
this.fileCallback = fileCallback;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件处理回调
|
||||
*
|
||||
* @param fileCallback 文件处理回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T fileCallback(CallbackArg<File> fileCallback) {
|
||||
return setFileCallback(fileCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置进度回调
|
||||
*
|
||||
* @param progressCallback 进度回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T setProgressCallback(CallbackArg<Double> progressCallback) {
|
||||
this.progressCallback = progressCallback;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置进度回调
|
||||
*
|
||||
* @param progressCallback 进度回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T progressCallback(CallbackArg<Double> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化字节进度
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -8,22 +8,23 @@ import java.io.InputStream;
|
||||
/**
|
||||
* 抽象解压器
|
||||
*
|
||||
* @param <T> 解压器类型
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 18:02
|
||||
*/
|
||||
public abstract class Decompressor extends AbstractRunner {
|
||||
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
|
||||
|
||||
/**
|
||||
* 将压缩文件解压到目标目录
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -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<GZipCompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行压缩
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -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<GZipDecompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行解压
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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)
|
||||
|
||||
@@ -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<TarCompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行压缩
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -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<TarDecompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行解压
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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;
|
||||
|
||||
@@ -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<Z7Compressor> {
|
||||
|
||||
/**
|
||||
* 静态执行压缩
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -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<Z7Decompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行解压
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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;
|
||||
|
||||
@@ -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<ZipCompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行压缩
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -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<ZipDecompressor> {
|
||||
|
||||
/**
|
||||
* 静态执行解压
|
||||
*
|
||||
* @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<File> fileCallback, CallbackArg<Double> 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;
|
||||
|
||||
@@ -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<Double> 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<Double> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Double> 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<Double> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Double> 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<Double> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Double> 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<Double> 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user