4 Commits

Author SHA1 Message Date
913b9b5328 Merge pull request 'v0.0.2' (#5) from dev into master
Reviewed-on: #5
2026-04-01 14:52:04 +00:00
Timi
56dec33c94 v0.0.2
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 14s
2026-04-01 22:47:09 +08:00
88b1fd6e02 Merge pull request 'v0.0.1' (#4) from dev into master
Reviewed-on: #4
2026-04-01 07:23:36 +00:00
Timi
a24b1855aa v0.0.1
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 10s
2026-04-01 15:23:23 +08:00
17 changed files with 551 additions and 70 deletions

View File

@@ -6,7 +6,7 @@
<groupId>com.imyeyu.compress</groupId> <groupId>com.imyeyu.compress</groupId>
<artifactId>timi-compress</artifactId> <artifactId>timi-compress</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
<properties> <properties>
<maven.test.skip>true</maven.test.skip> <maven.test.skip>true</maven.test.skip>
@@ -106,7 +106,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.44</version> <version>1.18.40</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>

View File

@@ -2,7 +2,6 @@ package com.imyeyu.compress;
import com.imyeyu.java.bean.CallbackArg; import com.imyeyu.java.bean.CallbackArg;
import com.imyeyu.utils.OS; import com.imyeyu.utils.OS;
import lombok.Setter;
import java.io.File; import java.io.File;
import java.io.FilterInputStream; import java.io.FilterInputStream;
@@ -14,17 +13,16 @@ import java.io.OutputStream;
/** /**
* 抽象压缩执行器 * 抽象压缩执行器
* *
* @param <T> 处理器类型
* @author 夜雨 * @author 夜雨
* @since 2024-06-30 18:09 * @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; protected CallbackArg<File> fileCallback;
/** 进度回调 */ /** 进度回调 */
@Setter
protected CallbackArg<Double> progressCallback; protected CallbackArg<Double> progressCallback;
/** 中断标记 */ /** 中断标记 */
@@ -42,6 +40,48 @@ public abstract class AbstractRunner implements OS.FileSystem {
/** 当前已处理字节数 */ /** 当前已处理字节数 */
private long progressBytes = 0L; 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() { public void pause() {
isPause = true; isPause = true;
@@ -60,6 +100,16 @@ public abstract class AbstractRunner implements OS.FileSystem {
isInterrupt = true; isInterrupt = true;
} }
/**
* 返回当前处理器
*
* @return 当前处理器
*/
@SuppressWarnings("unchecked")
protected final T self() {
return (T) this;
}
/** /**
* 初始化字节进度 * 初始化字节进度
* *

View File

@@ -31,12 +31,12 @@ public enum CompressType {
TAR(TarCompressor.class, TarDecompressor.class); 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.compressorType = compressorType;
this.decompressorType = decompressorType; this.decompressorType = decompressorType;
} }
@@ -47,7 +47,7 @@ public enum CompressType {
* @return 压缩操作对象 * @return 压缩操作对象
* @throws Exception 实例化失败 * @throws Exception 实例化失败
*/ */
public Compressor getCompressor() throws Exception { public Compressor<?> getCompressor() throws Exception {
return Ref.newInstance(compressorType); return Ref.newInstance(compressorType);
} }
@@ -57,19 +57,20 @@ public enum CompressType {
* @return 解压操作对象 * @return 解压操作对象
* @throws Exception 实例化失败 * @throws Exception 实例化失败
*/ */
public Decompressor getDecompressor() throws Exception { public Decompressor<?> getDecompressor() throws Exception {
return Ref.newInstance(decompressorType); return Ref.newInstance(decompressorType);
} }
/** /**
* 根据文件获取解压操作对象。会读取文件头识别压缩格式 * 根据文件获取解压操作对象
* 会读取文件头识别压缩格式
* *
* @param file 文件 * @param file 文件
* @return 解压操作对象 * @return 解压操作对象
* @throws UnsupportedOperationException 不支持的文件 * @throws UnsupportedOperationException 不支持的文件
* @throws Exception 实例化失败 * @throws Exception 实例化失败
*/ */
public static Decompressor fromFile(File file) throws Exception { public static Decompressor<?> fromFile(File file) throws Exception {
try (InputStream inputStream = IO.getInputStream(file)) { try (InputStream inputStream = IO.getInputStream(file)) {
byte[] head = new byte[512]; byte[] head = new byte[512];
int length = inputStream.read(head); int length = inputStream.read(head);

View File

@@ -8,21 +8,49 @@ import java.io.OutputStream;
/** /**
* 抽象压缩器 * 抽象压缩器
* *
* @param <T> 压缩器类型
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 10:34 * @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 fromPath 源路径
* @param toFile 目标压缩文件 * @param toFile 目标压缩文件
* @return 当前压缩器
* @throws Exception 压缩失败 * @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)) { try (OutputStream outputStream = IO.getOutputStream(toFile)) {
run(fromPath, outputStream); doRun(fromPath, outputStream);
outputStream.flush(); outputStream.flush();
} catch (Exception exception) { } catch (Exception exception) {
if (isInterrupt) { if (isInterrupt) {
@@ -36,12 +64,11 @@ public abstract class Compressor extends AbstractRunner {
} }
/** /**
* 将指定路径下的文件压缩到输出流 * 执行压缩到输出流
* 输出流由调用方管理
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toStream 目标输出流 * @param toStream 目标输出流
* @throws Exception 压缩失败 * @throws Exception 压缩失败
*/ */
public abstract void run(String fromPath, OutputStream toStream) throws Exception; protected abstract void doRun(String fromPath, OutputStream toStream) throws Exception;
} }

View File

@@ -8,22 +8,23 @@ import java.io.InputStream;
/** /**
* 抽象解压器 * 抽象解压器
* *
* @param <T> 解压器类型
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 18:02 * @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 fromFile 源压缩文件
* @param toPath 目标目录 * @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败 * @throws Exception 解压失败
*/ */
public void run(File fromFile, String toPath) throws Exception { public T run(File fromFile, String toPath) throws Exception {
try (InputStream inputStream = IO.getInputStream(fromFile)) { doRunFromFile(fromFile, toPath);
run(inputStream, toPath); return self();
}
} }
/** /**
@@ -32,7 +33,33 @@ public abstract class Decompressor extends AbstractRunner {
* *
* @param fromStream 源压缩输入流 * @param fromStream 源压缩输入流
* @param toPath 目标目录 * @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败 * @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;
} }

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
@@ -18,10 +19,39 @@ import java.util.List;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:40 * @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 @Override
public void run(String fromPath, OutputStream toStream) throws Exception { protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath); File fromFile = new File(fromPath);
List<File> files = IO.listFile(fromFile); List<File> files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath(); String basePath = files.getFirst().getParentFile().getAbsolutePath();

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
@@ -16,20 +17,49 @@ import java.io.OutputStream;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:47 * @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 @Override
public void run(File fromFile, String toPath) throws Exception { protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.run(fromFile, toPath); super.doRunFromFile(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
public void run(InputStream fromStream, String toPath) throws Exception { protected void doRun(InputStream fromStream, String toPath) throws Exception {
try ( try (
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(nonClosing(fromStream)); GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(nonClosing(fromStream));
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream) TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
@@ -16,10 +17,39 @@ import java.util.List;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:48 * @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 @Override
public void run(String fromPath, OutputStream toStream) throws Exception { protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath); File fromFile = new File(fromPath);
List<File> files = IO.listFile(fromFile); List<File> files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath(); String basePath = files.getFirst().getParentFile().getAbsolutePath();

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
@@ -14,20 +15,49 @@ import java.io.OutputStream;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:48 * @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 @Override
public void run(File fromFile, String toPath) throws Exception { protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.run(fromFile, toPath); super.doRunFromFile(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @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))) { try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(nonClosing(fromStream))) {
TarArchiveEntry entry; TarArchiveEntry entry;
boolean processed = false; boolean processed = false;

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile; import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
@@ -17,10 +18,39 @@ import java.util.List;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:40 * @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 @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"); Path tempFile = Files.createTempFile("timi-compress-", ".7z");
try { try {
run(fromPath, tempFile.toFile()); run(fromPath, tempFile.toFile());
@@ -34,7 +64,7 @@ public class Z7Compressor extends Compressor {
} }
@Override @Override
public void run(String fromPath, File toFile) throws Exception { protected void doRunToFile(String fromPath, File toFile) throws Exception {
File fromFile = new File(fromPath); File fromFile = new File(fromPath);
List<File> files = IO.listFile(fromFile); List<File> files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath(); String basePath = files.getFirst().getParentFile().getAbsolutePath();

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; 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.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile; import org.apache.commons.compress.archivers.sevenz.SevenZFile;
@@ -16,20 +17,49 @@ import java.nio.file.Path;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:42 * @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 @Override
public void run(File fromFile, String toPath) throws Exception { protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.run(fromFile, toPath); super.doRunFromFile(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
public void run(InputStream fromStream, String toPath) throws Exception { protected void doRun(InputStream fromStream, String toPath) throws Exception {
Path tempFile = writeTempFile(fromStream); Path tempFile = writeTempFile(fromStream);
try (SevenZFile file = SevenZFile.builder().setFile(tempFile.toFile()).get()) { try (SevenZFile file = SevenZFile.builder().setFile(tempFile.toFile()).get()) {
SevenZArchiveEntry entry; SevenZArchiveEntry entry;

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import com.imyeyu.java.bean.CallbackArg;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
@@ -16,10 +17,39 @@ import java.util.zip.ZipOutputStream;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:46 * @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 @Override
public void run(String fromPath, OutputStream toStream) throws Exception { protected void doRun(String fromPath, OutputStream toStream) throws Exception {
File fromFile = new File(fromPath); File fromFile = new File(fromPath);
List<File> files = IO.listFile(fromFile); List<File> files = IO.listFile(fromFile);
String basePath = files.getFirst().getParentFile().getAbsolutePath(); String basePath = files.getFirst().getParentFile().getAbsolutePath();

View File

@@ -1,6 +1,7 @@
package com.imyeyu.compress; package com.imyeyu.compress;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import com.imyeyu.java.bean.CallbackArg;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
@@ -16,20 +17,49 @@ import java.util.zip.ZipInputStream;
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:47 * @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 @Override
public void run(File fromFile, String toPath) throws Exception { protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.run(fromFile, toPath); super.doRunFromFile(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @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))) { try (ZipInputStream zipInputStream = new ZipInputStream(nonClosing(fromStream))) {
ZipEntry entry; ZipEntry entry;
boolean processed = false; boolean processed = false;

View File

@@ -1,12 +1,17 @@
package test; package test;
import com.imyeyu.compress.CompressType; import com.imyeyu.compress.CompressType;
import com.imyeyu.compress.GZipCompressor;
import com.imyeyu.compress.GZipDecompressor;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author 夜雨 * @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class GzipTest { public class GzipTest {
@Test @Test
public void testCompress() throws Exception { public void testStaticCompress() throws Exception {
File out = IO.file("testOut/test.gz"); GZipCompressor compressor = GZipCompressor.run("testSrc", "testOut/test.gz");
CompressType.GZIP.getCompressor().run("testSrc", out); 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 @Test
public void testDecompress() throws Exception { public void testDecompress() throws Exception {
File in = IO.file("testOut/test.gz"); File in = IO.file("testOut/test.gz");
GZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).run(in, out.getAbsolutePath());
} }
@@ -35,15 +58,26 @@ public class GzipTest {
} }
@Test @Test
public void testDecompressFromStream() throws Exception { public void testChainDecompressFromStream() throws Exception {
byte[] bytes; byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.getCompressor().run("testSrc", outputStream); CompressType.GZIP.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
File out = IO.dir("testOutDeStream/gzip"); 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)) { 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());
} }
} }

View File

@@ -1,12 +1,17 @@
package test; package test;
import com.imyeyu.compress.CompressType; import com.imyeyu.compress.CompressType;
import com.imyeyu.compress.TarCompressor;
import com.imyeyu.compress.TarDecompressor;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author 夜雨 * @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class TarTest { public class TarTest {
@Test @Test
public void testCompress() throws Exception { public void testStaticCompress() throws Exception {
File out = IO.file("testOut/test.tar"); TarCompressor compressor = TarCompressor.run("testSrc", "testOut/test.tar");
CompressType.TAR.getCompressor().run("testSrc", out); 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 @Test
public void testDecompress() throws Exception { public void testDecompress() throws Exception {
File in = IO.file("testOut/test.tar"); File in = IO.file("testOut/test.tar");
TarCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).run(in, out.getAbsolutePath());
} }
@@ -35,15 +58,26 @@ public class TarTest {
} }
@Test @Test
public void testDecompressFromStream() throws Exception { public void testChainDecompressFromStream() throws Exception {
byte[] bytes; byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.TAR.getCompressor().run("testSrc", outputStream); CompressType.TAR.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
File out = IO.dir("testOutDeStream/tar"); 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)) { 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());
} }
} }

View File

@@ -1,12 +1,17 @@
package test; package test;
import com.imyeyu.compress.CompressType; import com.imyeyu.compress.CompressType;
import com.imyeyu.compress.Z7Compressor;
import com.imyeyu.compress.Z7Decompressor;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author 夜雨 * @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class Z7Test { public class Z7Test {
@Test @Test
public void testCompress() throws Exception { public void testStaticCompress() throws Exception {
File out = IO.file("testOut/test.7z"); Z7Compressor compressor = Z7Compressor.run("testSrc", "testOut/test.7z");
CompressType.Z7.getCompressor().run("testSrc", out); 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 @Test
public void testDecompress() throws Exception { public void testDecompress() throws Exception {
File in = IO.file("testOut/test.7z"); File in = IO.file("testOut/test.7z");
Z7Compressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).run(in, out.getAbsolutePath());
} }
@@ -35,15 +58,26 @@ public class Z7Test {
} }
@Test @Test
public void testDecompressFromStream() throws Exception { public void testChainDecompressFromStream() throws Exception {
byte[] bytes; byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.Z7.getCompressor().run("testSrc", outputStream); CompressType.Z7.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
File out = IO.dir("testOutDeStream/z7"); 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)) { 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());
} }
} }

View File

@@ -1,12 +1,17 @@
package test; package test;
import com.imyeyu.compress.CompressType; import com.imyeyu.compress.CompressType;
import com.imyeyu.compress.ZipCompressor;
import com.imyeyu.compress.ZipDecompressor;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author 夜雨 * @author 夜雨
@@ -15,14 +20,32 @@ import java.io.File;
public class ZipTest { public class ZipTest {
@Test @Test
public void testCompress() throws Exception { public void testStaticCompress() throws Exception {
File out = IO.file("testOut/test.zip"); ZipCompressor compressor = ZipCompressor.run("testSrc", "testOut/test.zip");
CompressType.ZIP.getCompressor().run("testSrc", out); 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 @Test
public void testDecompress() throws Exception { public void testDecompress() throws Exception {
File in = IO.file("testOut/test.zip"); File in = IO.file("testOut/test.zip");
ZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).run(in, out.getAbsolutePath());
} }
@@ -35,15 +58,26 @@ public class ZipTest {
} }
@Test @Test
public void testDecompressFromStream() throws Exception { public void testChainDecompressFromStream() throws Exception {
byte[] bytes; byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.ZIP.getCompressor().run("testSrc", outputStream); CompressType.ZIP.getCompressor().run("testSrc", outputStream);
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
File out = IO.dir("testOutDeStream/zip"); 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)) { 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());
} }
} }