5 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
88b1fd6e02 Merge pull request 'v0.0.1' (#4) from dev into master
Reviewed-on: #4
2026-04-01 07:23:36 +00:00
1f13cdd7bc Merge pull request 'v0.0.1' (#3) from dev into master
Reviewed-on: #3
2026-04-01 07:20:16 +00:00
0b078278f0 Merge pull request 'v0.0.1' (#2) from dev into master
Reviewed-on: #2
2026-04-01 07:13:48 +00:00
c0b2ae11a7 Merge pull request 'v0.0.1' (#1) from dev into master
Reviewed-on: #1
2026-04-01 06:09:52 +00:00
19 changed files with 296 additions and 392 deletions

View File

@@ -1,19 +1,3 @@
# timi-compress
Java 压缩、解压缩工具
## 调用方式
压缩:
```java
ZipCompressor.of("testSrc").toFile("testOut/test.zip");
TarCompressor.of(file).toStream(outputStream);
```
解压:
```java
ZipDecompressor.of("testOut/test.zip").to("testOutDe");
GZipDecompressor.of(inputStream).to("testOutDe");
```

View File

@@ -6,7 +6,7 @@
<groupId>com.imyeyu.compress</groupId>
<artifactId>timi-compress</artifactId>
<version>0.0.4</version>
<version>0.0.2</version>
<properties>
<maven.test.skip>true</maven.test.skip>

View File

@@ -1,7 +1,6 @@
package com.imyeyu.compress;
import com.imyeyu.java.bean.CallbackArg;
import com.imyeyu.java.bean.timi.TimiException;
import com.imyeyu.utils.OS;
import java.io.File;
@@ -12,7 +11,7 @@ import java.io.InputStream;
import java.io.OutputStream;
/**
* 抽象处理
* 抽象压缩执行
*
* @param <T> 处理器类型
* @author 夜雨
@@ -274,12 +273,16 @@ public abstract class AbstractRunner<T extends AbstractRunner<T>> implements OS.
* @return 规范化后的名称
*/
protected String normalizeEntryName(String entryName) {
TimiException.required(entryName, "not found entryName");
if (entryName == null || entryName.isBlank()) {
throw new IllegalArgumentException("条目名称不能为空");
}
String normalized = entryName.replace('\\', '/');
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
TimiException.required(normalized, "not found normalized entryName");
if (normalized.isBlank()) {
throw new IllegalArgumentException("条目名称不能为空");
}
return normalized;
}
}

View File

@@ -30,10 +30,10 @@ public enum CompressType {
/** Tar */
TAR(TarCompressor.class, TarDecompressor.class);
/** 压缩器类型 */
/** 压缩 */
final Class<? extends Compressor<?>> compressorType;
/** 解压器类型 */
/** 解压 */
final Class<? extends Decompressor<?>> decompressorType;
CompressType(Class<? extends Compressor<?>> compressorType, Class<? extends Decompressor<?>> decompressorType) {
@@ -51,28 +51,6 @@ public enum CompressType {
return Ref.newInstance(compressorType);
}
/**
* 获取带源路径的压缩操作对象
*
* @param fromPath 源路径
* @return 压缩操作对象
* @throws Exception 实例化失败
*/
public Compressor<?> compressFrom(String fromPath) throws Exception {
return getCompressor().of(fromPath);
}
/**
* 获取带源文件的压缩操作对象
*
* @param fromFile 源文件
* @return 压缩操作对象
* @throws Exception 实例化失败
*/
public Compressor<?> compressFrom(File fromFile) throws Exception {
return getCompressor().of(fromFile);
}
/**
* 获取解压操作对象
*
@@ -83,28 +61,6 @@ public enum CompressType {
return Ref.newInstance(decompressorType);
}
/**
* 获取带源文件的解压操作对象
*
* @param fromFile 源压缩文件
* @return 解压操作对象
* @throws Exception 实例化失败
*/
public Decompressor<?> decompressedFrom(File fromFile) throws Exception {
return getDecompressor().of(fromFile);
}
/**
* 获取带源输入流的解压操作对象
*
* @param fromStream 源压缩输入流
* @return 解压操作对象
* @throws Exception 实例化失败
*/
public Decompressor<?> decompressedFrom(InputStream fromStream) throws Exception {
return getDecompressor().of(fromStream);
}
/**
* 根据文件获取解压操作对象
* 会读取文件头识别压缩格式
@@ -114,26 +70,26 @@ public enum CompressType {
* @throws UnsupportedOperationException 不支持的文件
* @throws Exception 实例化失败
*/
public static Decompressor<?> from(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);
if (length == -1) {
throw new UnsupportedOperationException("empty file");
}
if (SevenZFile.matches(head, length)) {
return Z7.decompressedFrom(file);
}
if (ZipArchiveInputStream.matches(head, length)) {
return ZIP.decompressedFrom(file);
}
if (GzipCompressorInputStream.matches(head, length)) {
return GZIP.decompressedFrom(file);
}
if (TarArchiveInputStream.matches(head, length)) {
return TAR.decompressedFrom(file);
}
}
throw new UnsupportedOperationException("not support file");
}
if (SevenZFile.matches(head, length)) {
return Z7.getDecompressor();
}
if (ZipArchiveInputStream.matches(head, length)) {
return ZIP.getDecompressor();
}
if (GzipCompressorInputStream.matches(head, length)) {
return GZIP.getDecompressor();
}
if (TarArchiveInputStream.matches(head, length)) {
return TAR.getDecompressor();
}
}
throw new UnsupportedOperationException("not support headHex");
}
}

View File

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

View File

@@ -1,7 +1,6 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.timi.TimiException;
import java.io.File;
import java.io.InputStream;
@@ -15,67 +14,32 @@ import java.io.InputStream;
*/
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
/** 源压缩文件 */
private File fromFile;
/** 源压缩输入流 */
private InputStream fromStream;
/**
* 绑定源压缩文件
* 压缩文件解压到目标目录
*
* @param fromFile 源压缩文件
* @return 当前解压器
*/
protected T of(File fromFile) {
TimiException.required(fromFile, "not found fromFile");
this.fromFile = fromFile;
this.fromStream = null;
return self();
}
/**
* 绑定源压缩文件路径
*
* @param fromPath 源压缩文件路径
* @return 当前解压器
*/
protected T of(String fromPath) {
TimiException.required(fromPath, "not found fromPath");
return of(new File(fromPath));
}
/**
* 绑定源压缩输入流
*
* @param fromStream 源压缩输入流
* @return 当前解压器
*/
public T of(InputStream fromStream) {
TimiException.required(fromStream, "not found fromStream");
this.fromStream = fromStream;
this.fromFile = null;
return self();
}
/**
* 解压到目标目录
*
* @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败
*/
public T toPath(String toPath) throws Exception {
if (fromFile != null) {
toPath(fromFile, toPath);
public T run(File fromFile, String toPath) throws Exception {
doRunFromFile(fromFile, toPath);
return self();
}
if (fromStream != null) {
toPath(fromStream, toPath);
/**
* 将压缩输入流解压到目标目录
* 输入流由调用方管理
*
* @param fromStream 源压缩输入流
* @param toPath 目标目录
* @return 当前解压器
* @throws Exception 解压失败
*/
public T run(InputStream fromStream, String toPath) throws Exception {
doRun(fromStream, toPath);
return self();
}
throw new IllegalStateException("not found source");
}
/**
* 执行从文件解压
@@ -84,9 +48,9 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录
* @throws Exception 解压失败
*/
protected void toPath(File fromFile, String toPath) throws Exception {
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
try (InputStream inputStream = IO.getInputStream(fromFile)) {
toPath(inputStream, toPath);
doRun(inputStream, toPath);
}
}
@@ -97,5 +61,5 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录
* @throws Exception 解压失败
*/
protected abstract void toPath(InputStream fromStream, String toPath) throws Exception;
protected abstract void doRun(InputStream fromStream, String toPath) throws Exception;
}

View File

@@ -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;
@@ -21,35 +22,44 @@ import java.util.List;
public class GZipCompressor extends Compressor<GZipCompressor> {
/**
* 创建压缩
* 静态执行压缩
*
* @param fromPath 源路径
* @return 压缩器
* @param toPath 目标文件路径
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static GZipCompressor from(String fromPath) {
return new GZipCompressor().of(fromPath);
public static GZipCompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建压缩
* 静态执行压缩
*
* @param fromFile 源文件
* @return 压缩器
* @param fromPath 源路径
* @param toPath 目标文件路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static GZipCompressor from(File fromFile) {
return new GZipCompressor().of(fromFile);
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
protected void toStream(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();
initByteProgress(IO.calcSize(fromFile));
try (
GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(new BufferedOutputStream(nonClosing(toStream)));
TarArchiveOutputStream tarOutputStream = null;
try {
tarOutputStream = new TarArchiveOutputStream(gzipOutputStream);
TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream)
) {
for (File sourceFile : files) {
String name = sourceFile.getAbsolutePath().substring(basePath.length() + 1);
TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, normalizeEntryName(name));
@@ -61,13 +71,9 @@ public class GZipCompressor extends Compressor<GZipCompressor> {
handleFile(sourceFile);
}
tarOutputStream.finish();
gzipOutputStream.finish();
finishProgress();
} finally {
if (tarOutputStream != null) {
tarOutputStream.close();
} else {
gzipOutputStream.close();
}
resetProgress();
}
}

View File

@@ -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;
@@ -11,6 +12,7 @@ import java.io.OutputStream;
/**
* Gzip 解压器
* 当前实现保持与原行为一致,实际按 tar.gz 解压
*
* @author 夜雨
* @version 2024-06-30 19:47
@@ -18,47 +20,46 @@ import java.io.OutputStream;
public class GZipDecompressor extends Decompressor<GZipDecompressor> {
/**
* 创建解压
*
* @param fromFile 源压缩文件
* @return 解压器
*/
public static GZipDecompressor from(File fromFile) {
return new GZipDecompressor().of(fromFile);
}
/**
* 创建解压器
* 静态执行解压
*
* @param fromPath 源压缩文件路径
* @return 解压器
* @param toPath 目标目录路径
* @return 当前解压器
* @throws Exception 解压失败
*/
public static GZipDecompressor from(String fromPath) {
return new GZipDecompressor().of(fromPath);
public static GZipDecompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建解压
* 静态执行解压
*
* @param fromStream 源压缩输入流
* @return 解压器
* @param fromPath 源压缩文件路径
* @param toPath 目标目录路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/
public static GZipDecompressor from(InputStream fromStream) {
return new GZipDecompressor().of(fromStream);
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
protected void toPath(File fromFile, String toPath) throws Exception {
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
super.toPath(fromFile, toPath);
super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
protected void toPath(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)

View File

@@ -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;
@@ -19,27 +20,36 @@ import java.util.List;
public class TarCompressor extends Compressor<TarCompressor> {
/**
* 创建压缩
* 静态执行压缩
*
* @param fromPath 源路径
* @return 压缩器
* @param toPath 目标文件路径
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static TarCompressor from(String fromPath) {
return new TarCompressor().of(fromPath);
public static TarCompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建压缩
* 静态执行压缩
*
* @param fromFile 源文件
* @return 压缩器
* @param fromPath 源路径
* @param toPath 目标文件路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static TarCompressor from(File fromFile) {
return new TarCompressor().of(fromFile);
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
protected void toStream(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();

View File

@@ -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;
@@ -17,47 +18,46 @@ import java.io.OutputStream;
public class TarDecompressor extends Decompressor<TarDecompressor> {
/**
* 创建解压
*
* @param fromFile 源压缩文件
* @return 解压器
*/
public static TarDecompressor from(File fromFile) {
return new TarDecompressor().of(fromFile);
}
/**
* 创建解压器
* 静态执行解压
*
* @param fromPath 源压缩文件路径
* @return 解压器
* @param toPath 目标目录路径
* @return 当前解压器
* @throws Exception 解压失败
*/
public static TarDecompressor from(String fromPath) {
return new TarDecompressor().of(fromPath);
public static TarDecompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建解压
* 静态执行解压
*
* @param fromStream 源压缩输入流
* @return 解压器
* @param fromPath 源压缩文件路径
* @param toPath 目标目录路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/
public static TarDecompressor from(InputStream fromStream) {
return new TarDecompressor().of(fromStream);
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
protected void toPath(File fromFile, String toPath) throws Exception {
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
super.toPath(fromFile, toPath);
super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
protected void toPath(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;

View File

@@ -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;
@@ -20,30 +21,39 @@ import java.util.List;
public class Z7Compressor extends Compressor<Z7Compressor> {
/**
* 创建压缩
* 静态执行压缩
*
* @param fromPath 源路径
* @return 压缩器
* @param toPath 目标文件路径
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static Z7Compressor from(String fromPath) {
return new Z7Compressor().of(fromPath);
public static Z7Compressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建压缩
* 静态执行压缩
*
* @param fromFile 源文件
* @return 压缩器
* @param fromPath 源路径
* @param toPath 目标文件路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static Z7Compressor from(File fromFile) {
return new Z7Compressor().of(fromFile);
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
protected void toStream(String fromPath, OutputStream toStream) throws Exception {
protected void doRun(String fromPath, OutputStream toStream) throws Exception {
Path tempFile = Files.createTempFile("timi-compress-", ".7z");
try {
of(fromPath).toFile(tempFile.toFile());
run(fromPath, tempFile.toFile());
try (InputStream inputStream = Files.newInputStream(tempFile)) {
transfer(inputStream, toStream, false);
toStream.flush();
@@ -54,7 +64,7 @@ public class Z7Compressor extends Compressor<Z7Compressor> {
}
@Override
protected void toFile(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();

View File

@@ -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;
@@ -19,47 +20,46 @@ import java.nio.file.Path;
public class Z7Decompressor extends Decompressor<Z7Decompressor> {
/**
* 创建解压
*
* @param fromFile 源压缩文件
* @return 解压器
*/
public static Z7Decompressor from(File fromFile) {
return new Z7Decompressor().of(fromFile);
}
/**
* 创建解压器
* 静态执行解压
*
* @param fromPath 源压缩文件路径
* @return 解压器
* @param toPath 目标目录路径
* @return 当前解压器
* @throws Exception 解压失败
*/
public static Z7Decompressor from(String fromPath) {
return new Z7Decompressor().of(fromPath);
public static Z7Decompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建解压
* 静态执行解压
*
* @param fromStream 源压缩输入流
* @return 解压器
* @param fromPath 源压缩文件路径
* @param toPath 目标目录路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/
public static Z7Decompressor from(InputStream fromStream) {
return new Z7Decompressor().of(fromStream);
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
protected void toPath(File fromFile, String toPath) throws Exception {
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
super.toPath(fromFile, toPath);
super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
protected void toPath(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;

View File

@@ -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;
@@ -19,27 +20,36 @@ import java.util.zip.ZipOutputStream;
public class ZipCompressor extends Compressor<ZipCompressor> {
/**
* 创建压缩
* 静态执行压缩
*
* @param fromPath 源路径
* @return 压缩器
* @param toPath 目标文件路径
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static ZipCompressor from(String fromPath) {
return new ZipCompressor().of(fromPath);
public static ZipCompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建压缩
* 静态执行压缩
*
* @param fromFile 源文件
* @return 压缩器
* @param fromPath 源路径
* @param toPath 目标文件路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public static ZipCompressor from(File fromFile) {
return new ZipCompressor().of(fromFile);
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
protected void toStream(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();

View File

@@ -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;
@@ -19,47 +20,46 @@ import java.util.zip.ZipInputStream;
public class ZipDecompressor extends Decompressor<ZipDecompressor> {
/**
* 创建解压
*
* @param fromFile 源压缩文件
* @return 解压器
*/
public static ZipDecompressor from(File fromFile) {
return new ZipDecompressor().of(fromFile);
}
/**
* 创建解压器
* 静态执行解压
*
* @param fromPath 源压缩文件路径
* @return 解压器
* @param toPath 目标目录路径
* @return 当前解压器
* @throws Exception 解压失败
*/
public static ZipDecompressor from(String fromPath) {
return new ZipDecompressor().of(fromPath);
public static ZipDecompressor run(String fromPath, String toPath) throws Exception {
return run(fromPath, toPath, null, null);
}
/**
* 创建解压
* 静态执行解压
*
* @param fromStream 源压缩输入流
* @return 解压器
* @param fromPath 源压缩文件路径
* @param toPath 目标目录路径
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/
public static ZipDecompressor from(InputStream fromStream) {
return new ZipDecompressor().of(fromStream);
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
protected void toPath(File fromFile, String toPath) throws Exception {
protected void doRunFromFile(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile));
try {
super.toPath(fromFile, toPath);
super.doRunFromFile(fromFile, toPath);
} finally {
resetProgress();
}
}
@Override
protected void toPath(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;

View File

@@ -10,7 +10,6 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -22,22 +21,22 @@ public class GzipTest {
@Test
public void testStaticCompress() throws Exception {
GZipCompressor compressor = GZipCompressor.from("testSrc").toFile("testOut/test.gz");
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);
GZipCompressor compressor = GZipCompressor.from("testSrc");
Assertions.assertSame(
compressor,
compressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toFile(out)
.run("testSrc", out)
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
@@ -46,15 +45,15 @@ public class GzipTest {
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.gz");
GZipCompressor.from("testSrc").toFile(in);
GZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.from(in).toPath(out.getAbsolutePath());
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@Test
public void testCompressToStream() throws Exception {
try (OutputStream os = IO.getOutputStream(IO.file("testOut/test.tar.gz"))) {
CompressType.GZIP.compressFrom("testSrc").toStream(os);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.getCompressor().run("testSrc", outputStream);
}
}
@@ -62,20 +61,22 @@ public class GzipTest {
public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.compressFrom("testSrc").toStream(outputStream);
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);
GZipDecompressor decompressor = GZipDecompressor.from(new ByteArrayInputStream(bytes));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
Assertions.assertSame(
decompressor,
decompressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toPath(out.getAbsolutePath())
.run(inputStream, out.getAbsolutePath())
);
}
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}

View File

@@ -10,7 +10,6 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -22,22 +21,22 @@ public class TarTest {
@Test
public void testStaticCompress() throws Exception {
TarCompressor compressor = TarCompressor.from("testSrc").toFile("testOut/test.tar");
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);
TarCompressor compressor = TarCompressor.from("testSrc");
Assertions.assertSame(
compressor,
compressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toFile(out)
.run("testSrc", out)
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
@@ -46,15 +45,15 @@ public class TarTest {
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.tar");
TarCompressor.from("testSrc").toFile(in);
TarCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.from(in).toPath(out.getAbsolutePath());
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@Test
public void testCompressToStream() throws Exception {
try (OutputStream os = IO.getOutputStream(IO.file("testOut/test.tar"))) {
CompressType.TAR.compressFrom("testSrc").toStream(os);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.TAR.getCompressor().run("testSrc", outputStream);
}
}
@@ -62,20 +61,22 @@ public class TarTest {
public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.TAR.compressFrom("testSrc").toStream(outputStream);
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);
TarDecompressor decompressor = TarDecompressor.from(new ByteArrayInputStream(bytes));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
Assertions.assertSame(
decompressor,
decompressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toPath(out.getAbsolutePath())
.run(inputStream, out.getAbsolutePath())
);
}
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}

View File

@@ -21,22 +21,22 @@ public class Z7Test {
@Test
public void testStaticCompress() throws Exception {
Z7Compressor compressor = Z7Compressor.from("testSrc").toFile("testOut/test.7z");
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);
Z7Compressor compressor = Z7Compressor.from("testSrc");
Assertions.assertSame(
compressor,
compressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toFile(out)
.run("testSrc", out)
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
@@ -45,15 +45,15 @@ public class Z7Test {
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.7z");
Z7Compressor.from("testSrc").toFile(in);
Z7Compressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.from(in).toPath(out.getAbsolutePath());
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@Test
public void testCompressToStream() throws Exception {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.Z7.compressFrom("testSrc").toStream(outputStream);
CompressType.Z7.getCompressor().run("testSrc", outputStream);
}
}
@@ -61,20 +61,22 @@ public class Z7Test {
public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.Z7.compressFrom("testSrc").toStream(outputStream);
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);
Z7Decompressor decompressor = Z7Decompressor.from(new ByteArrayInputStream(bytes));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
Assertions.assertSame(
decompressor,
decompressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toPath(out.getAbsolutePath())
.run(inputStream, out.getAbsolutePath())
);
}
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}

View File

@@ -21,22 +21,22 @@ public class ZipTest {
@Test
public void testStaticCompress() throws Exception {
ZipCompressor compressor = ZipCompressor.from("testSrc").toFile("testOut/test.zip");
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);
ZipCompressor compressor = ZipCompressor.from("testSrc");
Assertions.assertSame(
compressor,
compressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toFile(out)
.run("testSrc", out)
);
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
@@ -45,15 +45,15 @@ public class ZipTest {
@Test
public void testDecompress() throws Exception {
File in = IO.file("testOut/test.zip");
ZipCompressor.from("testSrc").toFile(in);
ZipCompressor.run("testSrc", in.getAbsolutePath());
File out = IO.dir("testOutDe");
CompressType.from(in).toPath(out.getAbsolutePath());
CompressType.fromFile(in).run(in, out.getAbsolutePath());
}
@Test
public void testCompressToStream() throws Exception {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.ZIP.compressFrom("testSrc").toStream(outputStream);
CompressType.ZIP.getCompressor().run("testSrc", outputStream);
}
}
@@ -61,20 +61,22 @@ public class ZipTest {
public void testChainDecompressFromStream() throws Exception {
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.ZIP.compressFrom("testSrc").toStream(outputStream);
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);
ZipDecompressor decompressor = ZipDecompressor.from(new ByteArrayInputStream(bytes));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
Assertions.assertSame(
decompressor,
decompressor
.fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set)
.toPath(out.getAbsolutePath())
.run(inputStream, out.getAbsolutePath())
);
}
Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get());
}