Merge pull request 'v0.0.3' (#6) from dev into master

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-04-02 08:26:38 +00:00
19 changed files with 372 additions and 282 deletions

View File

@@ -1,3 +1,19 @@
# timi-compress # timi-compress
Java 压缩、解压缩工具 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> <groupId>com.imyeyu.compress</groupId>
<artifactId>timi-compress</artifactId> <artifactId>timi-compress</artifactId>
<version>0.0.2</version> <version>0.0.3</version>
<properties> <properties>
<maven.test.skip>true</maven.test.skip> <maven.test.skip>true</maven.test.skip>

View File

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

View File

@@ -30,10 +30,10 @@ public enum CompressType {
/** Tar */ /** Tar */
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) {
@@ -51,6 +51,28 @@ public enum CompressType {
return Ref.newInstance(compressorType); return Ref.newInstance(compressorType);
} }
/**
* 获取带源路径的压缩操作对象
*
* @param fromPath 源路径
* @return 压缩操作对象
* @throws Exception 实例化失败
*/
public Compressor<?> ofCompress(String fromPath) throws Exception {
return getCompressor().from(fromPath);
}
/**
* 获取带源文件的压缩操作对象
*
* @param fromFile 源文件
* @return 压缩操作对象
* @throws Exception 实例化失败
*/
public Compressor<?> ofCompress(File fromFile) throws Exception {
return getCompressor().from(fromFile);
}
/** /**
* 获取解压操作对象 * 获取解压操作对象
* *
@@ -61,6 +83,28 @@ public enum CompressType {
return Ref.newInstance(decompressorType); return Ref.newInstance(decompressorType);
} }
/**
* 获取带源文件的解压操作对象
*
* @param fromFile 源压缩文件
* @return 解压操作对象
* @throws Exception 实例化失败
*/
public Decompressor<?> ofCompressed(File fromFile) throws Exception {
return getDecompressor().from(fromFile);
}
/**
* 获取带源输入流的解压操作对象
*
* @param fromStream 源压缩输入流
* @return 解压操作对象
* @throws Exception 实例化失败
*/
public Decompressor<?> ofCompressed(InputStream fromStream) throws Exception {
return getDecompressor().from(fromStream);
}
/** /**
* 根据文件获取解压操作对象 * 根据文件获取解压操作对象
* 会读取文件头识别压缩格式 * 会读取文件头识别压缩格式
@@ -75,21 +119,21 @@ public enum CompressType {
byte[] head = new byte[512]; byte[] head = new byte[512];
int length = inputStream.read(head); int length = inputStream.read(head);
if (length == -1) { if (length == -1) {
throw new UnsupportedOperationException("not support file"); throw new UnsupportedOperationException("empty file");
} }
if (SevenZFile.matches(head, length)) { if (SevenZFile.matches(head, length)) {
return Z7.getDecompressor(); return Z7.ofCompressed(file);
} }
if (ZipArchiveInputStream.matches(head, length)) { if (ZipArchiveInputStream.matches(head, length)) {
return ZIP.getDecompressor(); return ZIP.ofCompressed(file);
} }
if (GzipCompressorInputStream.matches(head, length)) { if (GzipCompressorInputStream.matches(head, length)) {
return GZIP.getDecompressor(); return GZIP.ofCompressed(file);
} }
if (TarArchiveInputStream.matches(head, length)) { if (TarArchiveInputStream.matches(head, length)) {
return TAR.getDecompressor(); return TAR.ofCompressed(file);
} }
} }
throw new UnsupportedOperationException("not support headHex"); throw new UnsupportedOperationException("not support file");
} }
} }

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.timi.TimiException;
import java.io.File; import java.io.File;
import java.io.OutputStream; import java.io.OutputStream;
@@ -14,33 +15,78 @@ import java.io.OutputStream;
*/ */
public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner<T> { public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner<T> {
/** 源路径 */
private String fromPath;
/** /**
* 将指定路径下的文件压缩到目标文件 * 绑定源路径
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toFile 目标压缩文件
* @return 当前压缩器 * @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public T run(String fromPath, File toFile) throws Exception { public T from(String fromPath) {
doRunToFile(fromPath, toFile); TimiException.required(fromPath, "not found fromPath");
this.fromPath = fromPath;
return self(); return self();
} }
/** /**
* 将指定路径下的文件压缩到输出流 * 绑定源文件
*
* @param fromFile 源文件
* @return 当前压缩器
*/
public T from(File fromFile) {
TimiException.required(fromFile, "not found fromFile");
return from(fromFile.getAbsolutePath());
}
/**
* 压缩到目标文件
*
* @param toFile 目标压缩文件
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public T toFile(File toFile) throws Exception {
toFile(requireFromPath(), toFile);
return self();
}
/**
* 压缩到目标文件路径
*
* @param toPath 目标压缩文件路径
* @return 当前压缩器
* @throws Exception 压缩失败
*/
public T toFile(String toPath) throws Exception {
return toFile(IO.file(toPath));
}
/**
* 压缩到输出流
* 输出流由调用方管理 * 输出流由调用方管理
* *
* @param fromPath 源路径
* @param toStream 目标输出流 * @param toStream 目标输出流
* @return 当前压缩器 * @return 当前压缩器
* @throws Exception 压缩失败 * @throws Exception 压缩失败
*/ */
public T run(String fromPath, OutputStream toStream) throws Exception { public T toStream(OutputStream toStream) throws Exception {
doRun(fromPath, toStream); toStream(requireFromPath(), toStream);
return self(); return self();
} }
/**
* 获取已绑定的源路径
*
* @return 源路径
*/
protected String requireFromPath() {
TimiException.required(fromPath, "not found source");
return fromPath;
}
/** /**
* 执行压缩到目标文件 * 执行压缩到目标文件
* *
@@ -48,9 +94,9 @@ public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner
* @param toFile 目标压缩文件 * @param toFile 目标压缩文件
* @throws Exception 压缩失败 * @throws Exception 压缩失败
*/ */
protected void doRunToFile(String fromPath, File toFile) throws Exception { protected void toFile(String fromPath, File toFile) throws Exception {
try (OutputStream outputStream = IO.getOutputStream(toFile)) { try (OutputStream outputStream = IO.getOutputStream(toFile)) {
doRun(fromPath, outputStream); toStream(fromPath, outputStream);
outputStream.flush(); outputStream.flush();
} catch (Exception exception) { } catch (Exception exception) {
if (isInterrupt) { if (isInterrupt) {
@@ -70,5 +116,5 @@ public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner
* @param toStream 目标输出流 * @param toStream 目标输出流
* @throws Exception 压缩失败 * @throws Exception 压缩失败
*/ */
protected abstract void doRun(String fromPath, OutputStream toStream) throws Exception; protected abstract void toStream(String fromPath, OutputStream toStream) 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.timi.TimiException;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
@@ -14,31 +15,66 @@ import java.io.InputStream;
*/ */
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> { public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
/** 源压缩文件 */
private File fromFile;
/** 源压缩输入流 */
private InputStream fromStream;
/** /**
* 压缩文件解压到目标目录 * 绑定源压缩文件
* *
* @param fromFile 源压缩文件 * @param fromFile 源压缩文件
* @param toPath 目标目录
* @return 当前解压器 * @return 当前解压器
* @throws Exception 解压失败
*/ */
public T run(File fromFile, String toPath) throws Exception { public T from(File fromFile) {
doRunFromFile(fromFile, toPath); TimiException.required(fromFile, "not found fromFile");
this.fromFile = fromFile;
this.fromStream = null;
return self(); return self();
} }
/** /**
* 将压缩输入流解压到目标目录 * 绑定源压缩文件路径
* 输入流由调用方管理 *
* @param fromPath 源压缩文件路径
* @return 当前解压器
*/
public T from(String fromPath) {
TimiException.required(fromPath, "not found fromPath");
return from(new File(fromPath));
}
/**
* 绑定源压缩输入流
* *
* @param fromStream 源压缩输入流 * @param fromStream 源压缩输入流
* @return 当前解压器
*/
public T from(InputStream fromStream) {
TimiException.required(fromStream, "not found fromStream");
this.fromStream = fromStream;
this.fromFile = null;
return self();
}
/**
* 解压到目标目录
*
* @param toPath 目标目录 * @param toPath 目标目录
* @return 当前解压器 * @return 当前解压器
* @throws Exception 解压失败 * @throws Exception 解压失败
*/ */
public T run(InputStream fromStream, String toPath) throws Exception { public T toPath(String toPath) throws Exception {
doRun(fromStream, toPath); if (fromFile != null) {
return self(); toPath(fromFile, toPath);
return self();
}
if (fromStream != null) {
toPath(fromStream, toPath);
return self();
}
throw new IllegalStateException("not found source");
} }
/** /**
@@ -48,9 +84,9 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录 * @param toPath 目标目录
* @throws Exception 解压失败 * @throws Exception 解压失败
*/ */
protected void doRunFromFile(File fromFile, String toPath) throws Exception { protected void toPath(File fromFile, String toPath) throws Exception {
try (InputStream inputStream = IO.getInputStream(fromFile)) { try (InputStream inputStream = IO.getInputStream(fromFile)) {
doRun(inputStream, toPath); toPath(inputStream, toPath);
} }
} }
@@ -61,5 +97,5 @@ public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRu
* @param toPath 目标目录 * @param toPath 目标目录
* @throws Exception 解压失败 * @throws Exception 解压失败
*/ */
protected abstract void doRun(InputStream fromStream, String toPath) throws Exception; protected abstract void toPath(InputStream fromStream, String toPath) throws Exception;
} }

View File

@@ -1,7 +1,6 @@
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;
@@ -22,36 +21,27 @@ import java.util.List;
public class GZipCompressor extends Compressor<GZipCompressor> { public class GZipCompressor extends Compressor<GZipCompressor> {
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toPath 目标文件路径 * @return 压缩器
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static GZipCompressor run(String fromPath, String toPath) throws Exception { public static GZipCompressor of(String fromPath) {
return run(fromPath, toPath, null, null); return new GZipCompressor().from(fromPath);
} }
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromFile 源文件
* @param toPath 目标文件路径 * @return 压缩器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static GZipCompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static GZipCompressor of(File fromFile) {
return new GZipCompressor() return new GZipCompressor().from(fromFile);
.fileCallback(fileCallback)
.progressCallback(progressCallback)
.run(fromPath, IO.file(toPath));
} }
@Override @Override
protected void doRun(String fromPath, OutputStream toStream) throws Exception { protected void toStream(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,7 +1,6 @@
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;
@@ -12,7 +11,6 @@ import java.io.OutputStream;
/** /**
* Gzip 解压器 * Gzip 解压器
* 当前实现保持与原行为一致,实际按 tar.gz 解压
* *
* @author 夜雨 * @author 夜雨
* @version 2024-06-30 19:47 * @version 2024-06-30 19:47
@@ -20,46 +18,47 @@ import java.io.OutputStream;
public class GZipDecompressor extends Decompressor<GZipDecompressor> { public class GZipDecompressor extends Decompressor<GZipDecompressor> {
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromFile 源压缩文件
* @param toPath 目标目录路径 * @return 解压器
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static GZipDecompressor run(String fromPath, String toPath) throws Exception { public static GZipDecompressor of(File fromFile) {
return run(fromPath, toPath, null, null); return new GZipDecompressor().from(fromFile);
} }
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromPath 源压缩文件路径
* @param toPath 目标目录路径 * @return 解压器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static GZipDecompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static GZipDecompressor of(String fromPath) {
return new GZipDecompressor() return new GZipDecompressor().from(fromPath);
.fileCallback(fileCallback) }
.progressCallback(progressCallback)
.run(IO.file(fromPath), toPath); /**
* 创建解压器
*
* @param fromStream 源压缩输入流
* @return 解压器
*/
public static GZipDecompressor of(InputStream fromStream) {
return new GZipDecompressor().from(fromStream);
} }
@Override @Override
protected void doRunFromFile(File fromFile, String toPath) throws Exception { protected void toPath(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.doRunFromFile(fromFile, toPath); super.toPath(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
protected void doRun(InputStream fromStream, String toPath) throws Exception { protected void toPath(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,7 +1,6 @@
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;
@@ -20,36 +19,27 @@ import java.util.List;
public class TarCompressor extends Compressor<TarCompressor> { public class TarCompressor extends Compressor<TarCompressor> {
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toPath 目标文件路径 * @return 压缩器
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static TarCompressor run(String fromPath, String toPath) throws Exception { public static TarCompressor of(String fromPath) {
return run(fromPath, toPath, null, null); return new TarCompressor().from(fromPath);
} }
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromFile 源文件
* @param toPath 目标文件路径 * @return 压缩器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static TarCompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static TarCompressor of(File fromFile) {
return new TarCompressor() return new TarCompressor().from(fromFile);
.fileCallback(fileCallback)
.progressCallback(progressCallback)
.run(fromPath, IO.file(toPath));
} }
@Override @Override
protected void doRun(String fromPath, OutputStream toStream) throws Exception { protected void toStream(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,7 +1,6 @@
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;
@@ -18,46 +17,47 @@ import java.io.OutputStream;
public class TarDecompressor extends Decompressor<TarDecompressor> { public class TarDecompressor extends Decompressor<TarDecompressor> {
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromFile 源压缩文件
* @param toPath 目标目录路径 * @return 解压器
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static TarDecompressor run(String fromPath, String toPath) throws Exception { public static TarDecompressor of(File fromFile) {
return run(fromPath, toPath, null, null); return new TarDecompressor().from(fromFile);
} }
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromPath 源压缩文件路径
* @param toPath 目标目录路径 * @return 解压器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static TarDecompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static TarDecompressor of(String fromPath) {
return new TarDecompressor() return new TarDecompressor().from(fromPath);
.fileCallback(fileCallback) }
.progressCallback(progressCallback)
.run(IO.file(fromPath), toPath); /**
* 创建解压器
*
* @param fromStream 源压缩输入流
* @return 解压器
*/
public static TarDecompressor of(InputStream fromStream) {
return new TarDecompressor().from(fromStream);
} }
@Override @Override
protected void doRunFromFile(File fromFile, String toPath) throws Exception { protected void toPath(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.doRunFromFile(fromFile, toPath); super.toPath(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
protected void doRun(InputStream fromStream, String toPath) throws Exception { protected void toPath(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,7 +1,6 @@
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;
@@ -21,39 +20,30 @@ import java.util.List;
public class Z7Compressor extends Compressor<Z7Compressor> { public class Z7Compressor extends Compressor<Z7Compressor> {
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toPath 目标文件路径 * @return 压缩器
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static Z7Compressor run(String fromPath, String toPath) throws Exception { public static Z7Compressor of(String fromPath) {
return run(fromPath, toPath, null, null); return new Z7Compressor().from(fromPath);
} }
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromFile 源文件
* @param toPath 目标文件路径 * @return 压缩器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static Z7Compressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static Z7Compressor of(File fromFile) {
return new Z7Compressor() return new Z7Compressor().from(fromFile);
.fileCallback(fileCallback)
.progressCallback(progressCallback)
.run(fromPath, IO.file(toPath));
} }
@Override @Override
protected void doRun(String fromPath, OutputStream toStream) throws Exception { protected void toStream(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()); from(fromPath).toFile(tempFile.toFile());
try (InputStream inputStream = Files.newInputStream(tempFile)) { try (InputStream inputStream = Files.newInputStream(tempFile)) {
transfer(inputStream, toStream, false); transfer(inputStream, toStream, false);
toStream.flush(); toStream.flush();
@@ -64,7 +54,7 @@ public class Z7Compressor extends Compressor<Z7Compressor> {
} }
@Override @Override
protected void doRunToFile(String fromPath, File toFile) throws Exception { protected void toFile(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,7 +1,6 @@
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;
@@ -20,46 +19,47 @@ import java.nio.file.Path;
public class Z7Decompressor extends Decompressor<Z7Decompressor> { public class Z7Decompressor extends Decompressor<Z7Decompressor> {
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromFile 源压缩文件
* @param toPath 目标目录路径 * @return 解压器
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static Z7Decompressor run(String fromPath, String toPath) throws Exception { public static Z7Decompressor of(File fromFile) {
return run(fromPath, toPath, null, null); return new Z7Decompressor().from(fromFile);
} }
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromPath 源压缩文件路径
* @param toPath 目标目录路径 * @return 解压器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static Z7Decompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static Z7Decompressor of(String fromPath) {
return new Z7Decompressor() return new Z7Decompressor().from(fromPath);
.fileCallback(fileCallback) }
.progressCallback(progressCallback)
.run(IO.file(fromPath), toPath); /**
* 创建解压器
*
* @param fromStream 源压缩输入流
* @return 解压器
*/
public static Z7Decompressor of(InputStream fromStream) {
return new Z7Decompressor().from(fromStream);
} }
@Override @Override
protected void doRunFromFile(File fromFile, String toPath) throws Exception { protected void toPath(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.doRunFromFile(fromFile, toPath); super.toPath(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
protected void doRun(InputStream fromStream, String toPath) throws Exception { protected void toPath(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,7 +1,6 @@
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;
@@ -20,36 +19,27 @@ import java.util.zip.ZipOutputStream;
public class ZipCompressor extends Compressor<ZipCompressor> { public class ZipCompressor extends Compressor<ZipCompressor> {
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromPath 源路径
* @param toPath 目标文件路径 * @return 压缩器
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static ZipCompressor run(String fromPath, String toPath) throws Exception { public static ZipCompressor of(String fromPath) {
return run(fromPath, toPath, null, null); return new ZipCompressor().from(fromPath);
} }
/** /**
* 静态执行压缩 * 创建压缩
* *
* @param fromPath 源路径 * @param fromFile 源文件
* @param toPath 目标文件路径 * @return 压缩器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前压缩器
* @throws Exception 压缩失败
*/ */
public static ZipCompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static ZipCompressor of(File fromFile) {
return new ZipCompressor() return new ZipCompressor().from(fromFile);
.fileCallback(fileCallback)
.progressCallback(progressCallback)
.run(fromPath, IO.file(toPath));
} }
@Override @Override
protected void doRun(String fromPath, OutputStream toStream) throws Exception { protected void toStream(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,7 +1,6 @@
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;
@@ -20,46 +19,47 @@ import java.util.zip.ZipInputStream;
public class ZipDecompressor extends Decompressor<ZipDecompressor> { public class ZipDecompressor extends Decompressor<ZipDecompressor> {
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromFile 源压缩文件
* @param toPath 目标目录路径 * @return 解压器
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static ZipDecompressor run(String fromPath, String toPath) throws Exception { public static ZipDecompressor of(File fromFile) {
return run(fromPath, toPath, null, null); return new ZipDecompressor().from(fromFile);
} }
/** /**
* 静态执行解压 * 创建解压
* *
* @param fromPath 源压缩文件路径 * @param fromPath 源压缩文件路径
* @param toPath 目标目录路径 * @return 解压器
* @param fileCallback 文件处理回调
* @param progressCallback 进度回调
* @return 当前解压器
* @throws Exception 解压失败
*/ */
public static ZipDecompressor run(String fromPath, String toPath, CallbackArg<File> fileCallback, CallbackArg<Double> progressCallback) throws Exception { public static ZipDecompressor of(String fromPath) {
return new ZipDecompressor() return new ZipDecompressor().from(fromPath);
.fileCallback(fileCallback) }
.progressCallback(progressCallback)
.run(IO.file(fromPath), toPath); /**
* 创建解压器
*
* @param fromStream 源压缩输入流
* @return 解压器
*/
public static ZipDecompressor of(InputStream fromStream) {
return new ZipDecompressor().from(fromStream);
} }
@Override @Override
protected void doRunFromFile(File fromFile, String toPath) throws Exception { protected void toPath(File fromFile, String toPath) throws Exception {
initByteProgress(readTotalSize(fromFile)); initByteProgress(readTotalSize(fromFile));
try { try {
super.doRunFromFile(fromFile, toPath); super.toPath(fromFile, toPath);
} finally { } finally {
resetProgress(); resetProgress();
} }
} }
@Override @Override
protected void doRun(InputStream fromStream, String toPath) throws Exception { protected void toPath(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

@@ -18,4 +18,4 @@ public class ClearTest {
IO.destroy(new File("testOutDe")); IO.destroy(new File("testOutDe"));
IO.dir("testOutDe"); IO.dir("testOutDe");
} }
} }

View File

@@ -21,22 +21,22 @@ public class GzipTest {
@Test @Test
public void testStaticCompress() throws Exception { public void testStaticCompress() throws Exception {
GZipCompressor compressor = GZipCompressor.run("testSrc", "testOut/test.gz"); GZipCompressor compressor = GZipCompressor.of("testSrc").toFile("testOut/test.gz");
Assertions.assertNotNull(compressor); Assertions.assertNotNull(compressor);
} }
@Test @Test
public void testChainCompress() throws Exception { public void testChainCompress() throws Exception {
File out = IO.file("testOut/test-chain.gz"); File out = IO.file("testOut/test-chain.gz");
GZipCompressor compressor = new GZipCompressor();
AtomicInteger fileCount = new AtomicInteger(); AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D); AtomicReference<Double> progress = new AtomicReference<>(0D);
GZipCompressor compressor = GZipCompressor.of("testSrc");
Assertions.assertSame( Assertions.assertSame(
compressor, compressor,
compressor compressor
.fileCallback(file -> fileCount.incrementAndGet()) .fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set) .progressCallback(progress::set)
.run("testSrc", out) .toFile(out)
); );
Assertions.assertTrue(0 < fileCount.get()); Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get()); Assertions.assertEquals(1D, progress.get());
@@ -45,15 +45,15 @@ public class GzipTest {
@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()); GZipCompressor.of("testSrc").toFile(in);
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).toPath(out.getAbsolutePath());
} }
@Test @Test
public void testCompressToStream() throws Exception { public void testCompressToStream() throws Exception {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.GZIP.getCompressor().run("testSrc", outputStream); CompressType.GZIP.ofCompress("testSrc").toStream(outputStream);
} }
} }
@@ -61,22 +61,20 @@ public class GzipTest {
public void testChainDecompressFromStream() 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.ofCompress("testSrc").toStream(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(); AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D); AtomicReference<Double> progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { GZipDecompressor decompressor = GZipDecompressor.of(new ByteArrayInputStream(bytes));
Assertions.assertSame( Assertions.assertSame(
decompressor, decompressor,
decompressor decompressor
.fileCallback(file -> fileCount.incrementAndGet()) .fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set) .progressCallback(progress::set)
.run(inputStream, out.getAbsolutePath()) .toPath(out.getAbsolutePath())
); );
}
Assertions.assertTrue(0 < fileCount.get()); Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get()); Assertions.assertEquals(1D, progress.get());
} }

View File

@@ -21,22 +21,22 @@ public class TarTest {
@Test @Test
public void testStaticCompress() throws Exception { public void testStaticCompress() throws Exception {
TarCompressor compressor = TarCompressor.run("testSrc", "testOut/test.tar"); TarCompressor compressor = TarCompressor.of("testSrc").toFile("testOut/test.tar");
Assertions.assertNotNull(compressor); Assertions.assertNotNull(compressor);
} }
@Test @Test
public void testChainCompress() throws Exception { public void testChainCompress() throws Exception {
File out = IO.file("testOut/test-chain.tar"); File out = IO.file("testOut/test-chain.tar");
TarCompressor compressor = new TarCompressor();
AtomicInteger fileCount = new AtomicInteger(); AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D); AtomicReference<Double> progress = new AtomicReference<>(0D);
TarCompressor compressor = TarCompressor.of("testSrc");
Assertions.assertSame( Assertions.assertSame(
compressor, compressor,
compressor compressor
.fileCallback(file -> fileCount.incrementAndGet()) .fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set) .progressCallback(progress::set)
.run("testSrc", out) .toFile(out)
); );
Assertions.assertTrue(0 < fileCount.get()); Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get()); Assertions.assertEquals(1D, progress.get());
@@ -45,15 +45,15 @@ public class TarTest {
@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()); TarCompressor.of("testSrc").toFile(in);
File out = IO.dir("testOutDe"); File out = IO.dir("testOutDe");
CompressType.fromFile(in).run(in, out.getAbsolutePath()); CompressType.fromFile(in).toPath(out.getAbsolutePath());
} }
@Test @Test
public void testCompressToStream() throws Exception { public void testCompressToStream() throws Exception {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
CompressType.TAR.getCompressor().run("testSrc", outputStream); CompressType.TAR.ofCompress("testSrc").toStream(outputStream);
} }
} }
@@ -61,22 +61,20 @@ public class TarTest {
public void testChainDecompressFromStream() 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.ofCompress("testSrc").toStream(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(); AtomicInteger fileCount = new AtomicInteger();
AtomicReference<Double> progress = new AtomicReference<>(0D); AtomicReference<Double> progress = new AtomicReference<>(0D);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { TarDecompressor decompressor = TarDecompressor.of(new ByteArrayInputStream(bytes));
Assertions.assertSame( Assertions.assertSame(
decompressor, decompressor,
decompressor decompressor
.fileCallback(file -> fileCount.incrementAndGet()) .fileCallback(file -> fileCount.incrementAndGet())
.progressCallback(progress::set) .progressCallback(progress::set)
.run(inputStream, out.getAbsolutePath()) .toPath(out.getAbsolutePath())
); );
}
Assertions.assertTrue(0 < fileCount.get()); Assertions.assertTrue(0 < fileCount.get());
Assertions.assertEquals(1D, progress.get()); Assertions.assertEquals(1D, progress.get());
} }

View File

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

View File

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