Compare commits
8 Commits
0b078278f0
...
v0.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 279289fb22 | |||
|
|
b56f4e8969 | ||
| 913b9b5328 | |||
|
|
56dec33c94 | ||
| 88b1fd6e02 | |||
|
|
a24b1855aa | ||
| 1f13cdd7bc | |||
|
|
f38975f633 |
@@ -28,7 +28,7 @@ jobs:
|
||||
echo "Running test suite..."
|
||||
- name: Build project
|
||||
run: |
|
||||
mvn -B -DskipTests clean package -Dhttps.protocols=TLSv1.2 -Djdk.tls.client.protocols=TLSv1.2
|
||||
mvn -B -DskipTests clean package source:jar javadoc:jar
|
||||
- name: Deploy to Nexus
|
||||
if: success()
|
||||
run: |
|
||||
@@ -43,7 +43,7 @@ jobs:
|
||||
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
||||
<servers>
|
||||
<server>
|
||||
<id>timi_nexus</id>
|
||||
<id>timi-nexus</id>
|
||||
<username>${{ secrets.NEXUS_USERNAME }}</username>
|
||||
<password>${{ secrets.NEXUS_PASSWORD }}</password>
|
||||
</server>
|
||||
@@ -65,7 +65,7 @@ jobs:
|
||||
-Djavadoc="$javadoc_jar" \
|
||||
-DpomFile="./pom.xml" \
|
||||
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
|
||||
-DrepositoryId="timi_nexus" \
|
||||
-DrepositoryId="timi-nexus" \
|
||||
-Dhttps.protocols=TLSv1.2 \
|
||||
-Djdk.tls.client.protocols=TLSv1.2
|
||||
- name: Create release
|
||||
|
||||
18
README.md
18
README.md
@@ -1,3 +1,19 @@
|
||||
# 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");
|
||||
```
|
||||
|
||||
43
pom.xml
43
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.compress</groupId>
|
||||
<artifactId>timi-compress</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.3</version>
|
||||
|
||||
<properties>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
@@ -20,21 +20,12 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.4</version>
|
||||
<version>3.1.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -58,29 +49,20 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.44</version>
|
||||
<version>1.18.36</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.12.0</version>
|
||||
<version>3.11.2</version>
|
||||
<configuration>
|
||||
<sourcepath>${project.build.directory}/delombok</sourcepath>
|
||||
<encoding>UTF-8</encoding>
|
||||
<charset>UTF-8</charset>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
@@ -105,19 +87,6 @@
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>timi_nexus</id>
|
||||
<url>https://nexus.imyeyu.com/repository/maven-public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.io</groupId>
|
||||
@@ -137,7 +106,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.44</version>
|
||||
<version>1.18.40</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.imyeyu.compress;
|
||||
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.utils.OS;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilterInputStream;
|
||||
@@ -12,19 +12,18 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* 抽象压缩执行器
|
||||
* 抽象处理器
|
||||
*
|
||||
* @param <T> 处理器类型
|
||||
* @author 夜雨
|
||||
* @since 2024-06-30 18:09
|
||||
*/
|
||||
public abstract class AbstractRunner implements OS.FileSystem {
|
||||
public abstract class AbstractRunner<T extends AbstractRunner<T>> implements OS.FileSystem {
|
||||
|
||||
/** 文件处理回调 */
|
||||
@Setter
|
||||
protected CallbackArg<File> fileCallback;
|
||||
|
||||
/** 进度回调 */
|
||||
@Setter
|
||||
protected CallbackArg<Double> progressCallback;
|
||||
|
||||
/** 中断标记 */
|
||||
@@ -42,6 +41,48 @@ public abstract class AbstractRunner implements OS.FileSystem {
|
||||
/** 当前已处理字节数 */
|
||||
private long progressBytes = 0L;
|
||||
|
||||
/**
|
||||
* 设置文件处理回调
|
||||
*
|
||||
* @param fileCallback 文件处理回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T setFileCallback(CallbackArg<File> fileCallback) {
|
||||
this.fileCallback = fileCallback;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件处理回调
|
||||
*
|
||||
* @param fileCallback 文件处理回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T fileCallback(CallbackArg<File> fileCallback) {
|
||||
return setFileCallback(fileCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置进度回调
|
||||
*
|
||||
* @param progressCallback 进度回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T setProgressCallback(CallbackArg<Double> progressCallback) {
|
||||
this.progressCallback = progressCallback;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置进度回调
|
||||
*
|
||||
* @param progressCallback 进度回调
|
||||
* @return 当前处理器
|
||||
*/
|
||||
public T progressCallback(CallbackArg<Double> progressCallback) {
|
||||
return setProgressCallback(progressCallback);
|
||||
}
|
||||
|
||||
/** 暂停任务 */
|
||||
public void pause() {
|
||||
isPause = true;
|
||||
@@ -60,6 +101,16 @@ public abstract class AbstractRunner implements OS.FileSystem {
|
||||
isInterrupt = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前处理器
|
||||
*
|
||||
* @return 当前处理器
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected final T self() {
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化字节进度
|
||||
*
|
||||
@@ -223,16 +274,12 @@ public abstract class AbstractRunner implements OS.FileSystem {
|
||||
* @return 规范化后的名称
|
||||
*/
|
||||
protected String normalizeEntryName(String entryName) {
|
||||
if (entryName == null || entryName.isBlank()) {
|
||||
throw new IllegalArgumentException("条目名称不能为空");
|
||||
}
|
||||
TimiException.required(entryName, "not found entryName");
|
||||
String normalized = entryName.replace('\\', '/');
|
||||
while (normalized.startsWith("/")) {
|
||||
normalized = normalized.substring(1);
|
||||
}
|
||||
if (normalized.isBlank()) {
|
||||
throw new IllegalArgumentException("条目名称不能为空");
|
||||
}
|
||||
TimiException.required(normalized, "not found normalized entryName");
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,13 +30,13 @@ public enum CompressType {
|
||||
/** Tar */
|
||||
TAR(TarCompressor.class, TarDecompressor.class);
|
||||
|
||||
/** 压缩类 */
|
||||
final Class<? extends Compressor> compressorType;
|
||||
/** 压缩器类型 */
|
||||
final Class<? extends Compressor<?>> compressorType;
|
||||
|
||||
/** 解压类 */
|
||||
final Class<? extends Decompressor> decompressorType;
|
||||
/** 解压器类型 */
|
||||
final Class<? extends Decompressor<?>> decompressorType;
|
||||
|
||||
CompressType(Class<? extends Compressor> compressorType, Class<? extends Decompressor> decompressorType) {
|
||||
CompressType(Class<? extends Compressor<?>> compressorType, Class<? extends Decompressor<?>> decompressorType) {
|
||||
this.compressorType = compressorType;
|
||||
this.decompressorType = decompressorType;
|
||||
}
|
||||
@@ -47,48 +47,93 @@ public enum CompressType {
|
||||
* @return 压缩操作对象
|
||||
* @throws Exception 实例化失败
|
||||
*/
|
||||
public Compressor getCompressor() throws Exception {
|
||||
public Compressor<?> getCompressor() throws Exception {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取解压操作对象
|
||||
*
|
||||
* @return 解压操作对象
|
||||
* @throws Exception 实例化失败
|
||||
*/
|
||||
public Decompressor getDecompressor() throws Exception {
|
||||
public Decompressor<?> getDecompressor() throws Exception {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件获取解压操作对象
|
||||
* 会读取文件头识别压缩格式
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 解压操作对象
|
||||
* @throws UnsupportedOperationException 不支持的文件
|
||||
* @throws Exception 实例化失败
|
||||
*/
|
||||
public static Decompressor fromFile(File file) throws Exception {
|
||||
public static Decompressor<?> fromFile(File file) throws Exception {
|
||||
try (InputStream inputStream = IO.getInputStream(file)) {
|
||||
byte[] head = new byte[512];
|
||||
int length = inputStream.read(head);
|
||||
if (length == -1) {
|
||||
throw new UnsupportedOperationException("not support file");
|
||||
throw new UnsupportedOperationException("empty file");
|
||||
}
|
||||
if (SevenZFile.matches(head, length)) {
|
||||
return Z7.getDecompressor();
|
||||
return Z7.ofCompressed(file);
|
||||
}
|
||||
if (ZipArchiveInputStream.matches(head, length)) {
|
||||
return ZIP.getDecompressor();
|
||||
return ZIP.ofCompressed(file);
|
||||
}
|
||||
if (GzipCompressorInputStream.matches(head, length)) {
|
||||
return GZIP.getDecompressor();
|
||||
return GZIP.ofCompressed(file);
|
||||
}
|
||||
if (TarArchiveInputStream.matches(head, length)) {
|
||||
return TAR.getDecompressor();
|
||||
return TAR.ofCompressed(file);
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException("not support headHex");
|
||||
throw new UnsupportedOperationException("not support file");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.imyeyu.compress;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
@@ -8,21 +9,94 @@ import java.io.OutputStream;
|
||||
/**
|
||||
* 抽象压缩器
|
||||
*
|
||||
* @param <T> 压缩器类型
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 10:34
|
||||
*/
|
||||
public abstract class Compressor extends AbstractRunner {
|
||||
public abstract class Compressor<T extends Compressor<T>> extends AbstractRunner<T> {
|
||||
|
||||
/** 源路径 */
|
||||
private String fromPath;
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到目标文件
|
||||
* 绑定源路径
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @return 当前压缩器
|
||||
*/
|
||||
public T from(String fromPath) {
|
||||
TimiException.required(fromPath, "not found fromPath");
|
||||
this.fromPath = fromPath;
|
||||
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 toStream 目标输出流
|
||||
* @return 当前压缩器
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public T toStream(OutputStream toStream) throws Exception {
|
||||
toStream(requireFromPath(), toStream);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已绑定的源路径
|
||||
*
|
||||
* @return 源路径
|
||||
*/
|
||||
protected String requireFromPath() {
|
||||
TimiException.required(fromPath, "not found source");
|
||||
return fromPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行压缩到目标文件
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toFile 目标压缩文件
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public void run(String fromPath, File toFile) throws Exception {
|
||||
protected void toFile(String fromPath, File toFile) throws Exception {
|
||||
try (OutputStream outputStream = IO.getOutputStream(toFile)) {
|
||||
run(fromPath, outputStream);
|
||||
toStream(fromPath, outputStream);
|
||||
outputStream.flush();
|
||||
} catch (Exception exception) {
|
||||
if (isInterrupt) {
|
||||
@@ -36,12 +110,11 @@ public abstract class Compressor extends AbstractRunner {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定路径下的文件压缩到输出流
|
||||
* 输出流由调用方管理
|
||||
* 执行压缩到输出流
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @param toStream 目标输出流
|
||||
* @throws Exception 压缩失败
|
||||
*/
|
||||
public abstract void run(String fromPath, OutputStream toStream) throws Exception;
|
||||
protected abstract void toStream(String fromPath, OutputStream toStream) throws Exception;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.imyeyu.compress;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
@@ -8,31 +9,93 @@ import java.io.InputStream;
|
||||
/**
|
||||
* 抽象解压器
|
||||
*
|
||||
* @param <T> 解压器类型
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 18:02
|
||||
*/
|
||||
public abstract class Decompressor extends AbstractRunner {
|
||||
public abstract class Decompressor<T extends Decompressor<T>> extends AbstractRunner<T> {
|
||||
|
||||
/** 源压缩文件 */
|
||||
private File fromFile;
|
||||
|
||||
/** 源压缩输入流 */
|
||||
private InputStream fromStream;
|
||||
|
||||
/**
|
||||
* 将压缩文件解压到目标目录
|
||||
* 绑定源压缩文件
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @return 当前解压器
|
||||
*/
|
||||
public T from(File fromFile) {
|
||||
TimiException.required(fromFile, "not found fromFile");
|
||||
this.fromFile = fromFile;
|
||||
this.fromStream = null;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定源压缩文件路径
|
||||
*
|
||||
* @param fromPath 源压缩文件路径
|
||||
* @return 当前解压器
|
||||
*/
|
||||
public T from(String fromPath) {
|
||||
TimiException.required(fromPath, "not found fromPath");
|
||||
return from(new File(fromPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定源压缩输入流
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @return 当前解压器
|
||||
*/
|
||||
public T from(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);
|
||||
return self();
|
||||
}
|
||||
if (fromStream != null) {
|
||||
toPath(fromStream, toPath);
|
||||
return self();
|
||||
}
|
||||
throw new IllegalStateException("not found source");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行从文件解压
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @param toPath 目标目录
|
||||
* @throws Exception 解压失败
|
||||
*/
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
protected void toPath(File fromFile, String toPath) throws Exception {
|
||||
try (InputStream inputStream = IO.getInputStream(fromFile)) {
|
||||
run(inputStream, toPath);
|
||||
toPath(inputStream, toPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将压缩输入流解压到目标目录
|
||||
* 输入流由调用方管理
|
||||
* 执行从输入流解压
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @param toPath 目标目录
|
||||
* @throws Exception 解压失败
|
||||
*/
|
||||
public abstract void run(InputStream fromStream, String toPath) throws Exception;
|
||||
protected abstract void toPath(InputStream fromStream, String toPath) throws Exception;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,30 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:40
|
||||
*/
|
||||
public class GZipCompressor extends Compressor {
|
||||
public class GZipCompressor extends Compressor<GZipCompressor> {
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static GZipCompressor of(String fromPath) {
|
||||
return new GZipCompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromFile 源文件
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static GZipCompressor of(File fromFile) {
|
||||
return new GZipCompressor().from(fromFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
protected void toStream(String fromPath, OutputStream toStream) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -11,25 +11,54 @@ import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Gzip 解压器
|
||||
* 当前实现保持与原行为一致,实际按 tar.gz 解压
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:47
|
||||
*/
|
||||
public class GZipDecompressor extends Decompressor {
|
||||
public class GZipDecompressor extends Decompressor<GZipDecompressor> {
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @return 解压器
|
||||
*/
|
||||
public static GZipDecompressor of(File fromFile) {
|
||||
return new GZipDecompressor().from(fromFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromPath 源压缩文件路径
|
||||
* @return 解压器
|
||||
*/
|
||||
public static GZipDecompressor of(String fromPath) {
|
||||
return new GZipDecompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @return 解压器
|
||||
*/
|
||||
public static GZipDecompressor of(InputStream fromStream) {
|
||||
return new GZipDecompressor().from(fromStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
protected void toPath(File fromFile, String toPath) throws Exception {
|
||||
initByteProgress(readTotalSize(fromFile));
|
||||
try {
|
||||
super.run(fromFile, toPath);
|
||||
super.toPath(fromFile, toPath);
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(InputStream fromStream, String toPath) throws Exception {
|
||||
protected void toPath(InputStream fromStream, String toPath) throws Exception {
|
||||
try (
|
||||
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(nonClosing(fromStream));
|
||||
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)
|
||||
|
||||
@@ -16,10 +16,30 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:48
|
||||
*/
|
||||
public class TarCompressor extends Compressor {
|
||||
public class TarCompressor extends Compressor<TarCompressor> {
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static TarCompressor of(String fromPath) {
|
||||
return new TarCompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromFile 源文件
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static TarCompressor of(File fromFile) {
|
||||
return new TarCompressor().from(fromFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
protected void toStream(String fromPath, OutputStream toStream) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -14,20 +14,50 @@ import java.io.OutputStream;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:48
|
||||
*/
|
||||
public class TarDecompressor extends Decompressor {
|
||||
public class TarDecompressor extends Decompressor<TarDecompressor> {
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @return 解压器
|
||||
*/
|
||||
public static TarDecompressor of(File fromFile) {
|
||||
return new TarDecompressor().from(fromFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromPath 源压缩文件路径
|
||||
* @return 解压器
|
||||
*/
|
||||
public static TarDecompressor of(String fromPath) {
|
||||
return new TarDecompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @return 解压器
|
||||
*/
|
||||
public static TarDecompressor of(InputStream fromStream) {
|
||||
return new TarDecompressor().from(fromStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
protected void toPath(File fromFile, String toPath) throws Exception {
|
||||
initByteProgress(readTotalSize(fromFile));
|
||||
try {
|
||||
super.run(fromFile, toPath);
|
||||
super.toPath(fromFile, toPath);
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(InputStream fromStream, String toPath) throws Exception {
|
||||
protected void toPath(InputStream fromStream, String toPath) throws Exception {
|
||||
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(nonClosing(fromStream))) {
|
||||
TarArchiveEntry entry;
|
||||
boolean processed = false;
|
||||
|
||||
@@ -17,13 +17,33 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:40
|
||||
*/
|
||||
public class Z7Compressor extends Compressor {
|
||||
public class Z7Compressor extends Compressor<Z7Compressor> {
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static Z7Compressor of(String fromPath) {
|
||||
return new Z7Compressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromFile 源文件
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static Z7Compressor of(File fromFile) {
|
||||
return new Z7Compressor().from(fromFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
protected void toStream(String fromPath, OutputStream toStream) throws Exception {
|
||||
Path tempFile = Files.createTempFile("timi-compress-", ".7z");
|
||||
try {
|
||||
run(fromPath, tempFile.toFile());
|
||||
from(fromPath).toFile(tempFile.toFile());
|
||||
try (InputStream inputStream = Files.newInputStream(tempFile)) {
|
||||
transfer(inputStream, toStream, false);
|
||||
toStream.flush();
|
||||
@@ -34,7 +54,7 @@ public class Z7Compressor extends Compressor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, File toFile) throws Exception {
|
||||
protected void toFile(String fromPath, File toFile) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -16,20 +16,50 @@ import java.nio.file.Path;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:42
|
||||
*/
|
||||
public class Z7Decompressor extends Decompressor {
|
||||
public class Z7Decompressor extends Decompressor<Z7Decompressor> {
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @return 解压器
|
||||
*/
|
||||
public static Z7Decompressor of(File fromFile) {
|
||||
return new Z7Decompressor().from(fromFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromPath 源压缩文件路径
|
||||
* @return 解压器
|
||||
*/
|
||||
public static Z7Decompressor of(String fromPath) {
|
||||
return new Z7Decompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @return 解压器
|
||||
*/
|
||||
public static Z7Decompressor of(InputStream fromStream) {
|
||||
return new Z7Decompressor().from(fromStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
protected void toPath(File fromFile, String toPath) throws Exception {
|
||||
initByteProgress(readTotalSize(fromFile));
|
||||
try {
|
||||
super.run(fromFile, toPath);
|
||||
super.toPath(fromFile, toPath);
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(InputStream fromStream, String toPath) throws Exception {
|
||||
protected void toPath(InputStream fromStream, String toPath) throws Exception {
|
||||
Path tempFile = writeTempFile(fromStream);
|
||||
try (SevenZFile file = SevenZFile.builder().setFile(tempFile.toFile()).get()) {
|
||||
SevenZArchiveEntry entry;
|
||||
|
||||
@@ -16,10 +16,30 @@ import java.util.zip.ZipOutputStream;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:46
|
||||
*/
|
||||
public class ZipCompressor extends Compressor {
|
||||
public class ZipCompressor extends Compressor<ZipCompressor> {
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromPath 源路径
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static ZipCompressor of(String fromPath) {
|
||||
return new ZipCompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建压缩器
|
||||
*
|
||||
* @param fromFile 源文件
|
||||
* @return 压缩器
|
||||
*/
|
||||
public static ZipCompressor of(File fromFile) {
|
||||
return new ZipCompressor().from(fromFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
protected void toStream(String fromPath, OutputStream toStream) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
@@ -16,20 +16,50 @@ import java.util.zip.ZipInputStream;
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:47
|
||||
*/
|
||||
public class ZipDecompressor extends Decompressor {
|
||||
public class ZipDecompressor extends Decompressor<ZipDecompressor> {
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromFile 源压缩文件
|
||||
* @return 解压器
|
||||
*/
|
||||
public static ZipDecompressor of(File fromFile) {
|
||||
return new ZipDecompressor().from(fromFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromPath 源压缩文件路径
|
||||
* @return 解压器
|
||||
*/
|
||||
public static ZipDecompressor of(String fromPath) {
|
||||
return new ZipDecompressor().from(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建解压器
|
||||
*
|
||||
* @param fromStream 源压缩输入流
|
||||
* @return 解压器
|
||||
*/
|
||||
public static ZipDecompressor of(InputStream fromStream) {
|
||||
return new ZipDecompressor().from(fromStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
protected void toPath(File fromFile, String toPath) throws Exception {
|
||||
initByteProgress(readTotalSize(fromFile));
|
||||
try {
|
||||
super.run(fromFile, toPath);
|
||||
super.toPath(fromFile, toPath);
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(InputStream fromStream, String toPath) throws Exception {
|
||||
protected void toPath(InputStream fromStream, String toPath) throws Exception {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(nonClosing(fromStream))) {
|
||||
ZipEntry entry;
|
||||
boolean processed = false;
|
||||
|
||||
@@ -18,4 +18,4 @@ public class ClearTest {
|
||||
IO.destroy(new File("testOutDe"));
|
||||
IO.dir("testOutDe");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.compress.CompressType;
|
||||
import com.imyeyu.compress.GZipCompressor;
|
||||
import com.imyeyu.compress.GZipDecompressor;
|
||||
import com.imyeyu.io.IO;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@@ -15,35 +20,62 @@ import java.io.File;
|
||||
public class GzipTest {
|
||||
|
||||
@Test
|
||||
public void testCompress() throws Exception {
|
||||
File out = IO.file("testOut/test.gz");
|
||||
CompressType.GZIP.getCompressor().run("testSrc", out);
|
||||
public void testStaticCompress() throws Exception {
|
||||
GZipCompressor compressor = GZipCompressor.of("testSrc").toFile("testOut/test.gz");
|
||||
Assertions.assertNotNull(compressor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainCompress() throws Exception {
|
||||
File out = IO.file("testOut/test-chain.gz");
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
GZipCompressor compressor = GZipCompressor.of("testSrc");
|
||||
Assertions.assertSame(
|
||||
compressor,
|
||||
compressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toFile(out)
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompress() throws Exception {
|
||||
File in = IO.file("testOut/test.gz");
|
||||
GZipCompressor.of("testSrc").toFile(in);
|
||||
File out = IO.dir("testOutDe");
|
||||
CompressType.fromFile(in).run(in, out.getAbsolutePath());
|
||||
CompressType.fromFile(in).toPath(out.getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompressToStream() throws Exception {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.GZIP.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.GZIP.ofCompress("testSrc").toStream(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompressFromStream() throws Exception {
|
||||
public void testChainDecompressFromStream() throws Exception {
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.GZIP.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.GZIP.ofCompress("testSrc").toStream(outputStream);
|
||||
bytes = outputStream.toByteArray();
|
||||
}
|
||||
File out = IO.dir("testOutDeStream/gzip");
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
|
||||
CompressType.GZIP.getDecompressor().run(inputStream, out.getAbsolutePath());
|
||||
}
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
GZipDecompressor decompressor = GZipDecompressor.of(new ByteArrayInputStream(bytes));
|
||||
Assertions.assertSame(
|
||||
decompressor,
|
||||
decompressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toPath(out.getAbsolutePath())
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.compress.CompressType;
|
||||
import com.imyeyu.compress.TarCompressor;
|
||||
import com.imyeyu.compress.TarDecompressor;
|
||||
import com.imyeyu.io.IO;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@@ -15,35 +20,62 @@ import java.io.File;
|
||||
public class TarTest {
|
||||
|
||||
@Test
|
||||
public void testCompress() throws Exception {
|
||||
File out = IO.file("testOut/test.tar");
|
||||
CompressType.TAR.getCompressor().run("testSrc", out);
|
||||
public void testStaticCompress() throws Exception {
|
||||
TarCompressor compressor = TarCompressor.of("testSrc").toFile("testOut/test.tar");
|
||||
Assertions.assertNotNull(compressor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainCompress() throws Exception {
|
||||
File out = IO.file("testOut/test-chain.tar");
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
TarCompressor compressor = TarCompressor.of("testSrc");
|
||||
Assertions.assertSame(
|
||||
compressor,
|
||||
compressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toFile(out)
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompress() throws Exception {
|
||||
File in = IO.file("testOut/test.tar");
|
||||
TarCompressor.of("testSrc").toFile(in);
|
||||
File out = IO.dir("testOutDe");
|
||||
CompressType.fromFile(in).run(in, out.getAbsolutePath());
|
||||
CompressType.fromFile(in).toPath(out.getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompressToStream() throws Exception {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.TAR.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.TAR.ofCompress("testSrc").toStream(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompressFromStream() throws Exception {
|
||||
public void testChainDecompressFromStream() throws Exception {
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.TAR.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.TAR.ofCompress("testSrc").toStream(outputStream);
|
||||
bytes = outputStream.toByteArray();
|
||||
}
|
||||
File out = IO.dir("testOutDeStream/tar");
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
|
||||
CompressType.TAR.getDecompressor().run(inputStream, out.getAbsolutePath());
|
||||
}
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
TarDecompressor decompressor = TarDecompressor.of(new ByteArrayInputStream(bytes));
|
||||
Assertions.assertSame(
|
||||
decompressor,
|
||||
decompressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toPath(out.getAbsolutePath())
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.compress.CompressType;
|
||||
import com.imyeyu.compress.Z7Compressor;
|
||||
import com.imyeyu.compress.Z7Decompressor;
|
||||
import com.imyeyu.io.IO;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@@ -15,35 +20,62 @@ import java.io.File;
|
||||
public class Z7Test {
|
||||
|
||||
@Test
|
||||
public void testCompress() throws Exception {
|
||||
File out = IO.file("testOut/test.7z");
|
||||
CompressType.Z7.getCompressor().run("testSrc", out);
|
||||
public void testStaticCompress() throws Exception {
|
||||
Z7Compressor compressor = Z7Compressor.of("testSrc").toFile("testOut/test.7z");
|
||||
Assertions.assertNotNull(compressor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainCompress() throws Exception {
|
||||
File out = IO.file("testOut/test-chain.7z");
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
Z7Compressor compressor = Z7Compressor.of("testSrc");
|
||||
Assertions.assertSame(
|
||||
compressor,
|
||||
compressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toFile(out)
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompress() throws Exception {
|
||||
File in = IO.file("testOut/test.7z");
|
||||
Z7Compressor.of("testSrc").toFile(in);
|
||||
File out = IO.dir("testOutDe");
|
||||
CompressType.fromFile(in).run(in, out.getAbsolutePath());
|
||||
CompressType.fromFile(in).toPath(out.getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompressToStream() throws Exception {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.Z7.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.Z7.ofCompress("testSrc").toStream(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompressFromStream() throws Exception {
|
||||
public void testChainDecompressFromStream() throws Exception {
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.Z7.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.Z7.ofCompress("testSrc").toStream(outputStream);
|
||||
bytes = outputStream.toByteArray();
|
||||
}
|
||||
File out = IO.dir("testOutDeStream/z7");
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
|
||||
CompressType.Z7.getDecompressor().run(inputStream, out.getAbsolutePath());
|
||||
}
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
Z7Decompressor decompressor = Z7Decompressor.of(new ByteArrayInputStream(bytes));
|
||||
Assertions.assertSame(
|
||||
decompressor,
|
||||
decompressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toPath(out.getAbsolutePath())
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.compress.CompressType;
|
||||
import com.imyeyu.compress.ZipCompressor;
|
||||
import com.imyeyu.compress.ZipDecompressor;
|
||||
import com.imyeyu.io.IO;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@@ -15,35 +20,62 @@ import java.io.File;
|
||||
public class ZipTest {
|
||||
|
||||
@Test
|
||||
public void testCompress() throws Exception {
|
||||
File out = IO.file("testOut/test.zip");
|
||||
CompressType.ZIP.getCompressor().run("testSrc", out);
|
||||
public void testStaticCompress() throws Exception {
|
||||
ZipCompressor compressor = ZipCompressor.of("testSrc").toFile("testOut/test.zip");
|
||||
Assertions.assertNotNull(compressor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainCompress() throws Exception {
|
||||
File out = IO.file("testOut/test-chain.zip");
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
ZipCompressor compressor = ZipCompressor.of("testSrc");
|
||||
Assertions.assertSame(
|
||||
compressor,
|
||||
compressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toFile(out)
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompress() throws Exception {
|
||||
File in = IO.file("testOut/test.zip");
|
||||
ZipCompressor.of("testSrc").toFile(in);
|
||||
File out = IO.dir("testOutDe");
|
||||
CompressType.fromFile(in).run(in, out.getAbsolutePath());
|
||||
CompressType.fromFile(in).toPath(out.getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompressToStream() throws Exception {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.ZIP.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.ZIP.ofCompress("testSrc").toStream(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecompressFromStream() throws Exception {
|
||||
public void testChainDecompressFromStream() throws Exception {
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
CompressType.ZIP.getCompressor().run("testSrc", outputStream);
|
||||
CompressType.ZIP.ofCompress("testSrc").toStream(outputStream);
|
||||
bytes = outputStream.toByteArray();
|
||||
}
|
||||
File out = IO.dir("testOutDeStream/zip");
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
|
||||
CompressType.ZIP.getDecompressor().run(inputStream, out.getAbsolutePath());
|
||||
}
|
||||
AtomicInteger fileCount = new AtomicInteger();
|
||||
AtomicReference<Double> progress = new AtomicReference<>(0D);
|
||||
ZipDecompressor decompressor = ZipDecompressor.of(new ByteArrayInputStream(bytes));
|
||||
Assertions.assertSame(
|
||||
decompressor,
|
||||
decompressor
|
||||
.fileCallback(file -> fileCount.incrementAndGet())
|
||||
.progressCallback(progress::set)
|
||||
.toPath(out.getAbsolutePath())
|
||||
);
|
||||
Assertions.assertTrue(0 < fileCount.get());
|
||||
Assertions.assertEquals(1D, progress.get());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user