v0.0.1
Some checks failed
CI/CD / build-deploy (pull_request) Failing after 1m31s

This commit is contained in:
Timi
2026-04-01 14:09:24 +08:00
parent 47f1c9520e
commit afcc902cbf
20 changed files with 986 additions and 396 deletions

View File

@@ -6,8 +6,11 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Tar 解压器
*
* @author 夜雨
* @version 2024-06-30 19:48
*/
@@ -15,49 +18,56 @@ public class TarDecompressor extends Decompressor {
@Override
public void run(File fromFile, String toPath) throws Exception {
InputStream is = IO.getInputStream(fromFile);
TarArchiveInputStream tarIS = new TarArchiveInputStream(is);
initByteProgress(readTotalSize(fromFile));
try {
super.run(fromFile, toPath);
} finally {
resetProgress();
}
}
int total = 0;
{
@Override
public void run(InputStream fromStream, String toPath) throws Exception {
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(nonClosing(fromStream))) {
TarArchiveEntry entry;
while ((entry = tarIS.getNextEntry()) != null) {
boolean processed = false;
while ((entry = tarInputStream.getNextEntry()) != null) {
String path = IO.fitPath(toPath) + entry.getName();
if (entry.isDirectory()) {
IO.dir(path);
} else {
File toFile = IO.file(path);
try (OutputStream outputStream = IO.getOutputStream(toFile)) {
transfer(tarInputStream, outputStream);
outputStream.flush();
}
handleFile(toFile);
processed = true;
}
}
if (processed) {
finishProgress();
}
}
}
/**
* 读取压缩包解压总字节数
*
* @param fromFile 压缩文件
* @return 总字节数
* @throws Exception 读取失败
*/
private long readTotalSize(File fromFile) throws Exception {
long totalSize = 0L;
try (InputStream inputStream = IO.getInputStream(fromFile); TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream)) {
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextEntry()) != null) {
if (!entry.isDirectory()) {
total++;
totalSize += entry.getSize();
}
}
}
tarIS.close();
is.close();
is = IO.getInputStream(fromFile);
tarIS = new TarArchiveInputStream(is);
TarArchiveEntry entry;
for (int i = 0; (entry = tarIS.getNextEntry()) != null; i++) {
String path = IO.fitPath(toPath) + entry.getName();
if (entry.isDirectory()) {
IO.dir(path);
} else {
File toFile = IO.file(path);
IO.toFile(toFile, tarIS);
if (fileCallback != null) {
fileCallback.handler(toFile);
}
}
if (isPause) {
synchronized (pauseLock) {
pauseLock.wait();
}
}
if (progressCallback != null && total != -1) {
progressCallback.handler(1D * i / total);
}
if (isInterrupt) {
break;
}
}
tarIS.close();
is.close();
return totalSize;
}
}