This commit is contained in:
@@ -7,8 +7,12 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Gzip 解压器
|
||||
* 当前实现保持与原行为一致,实际按 tar.gz 解压
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:47
|
||||
*/
|
||||
@@ -16,51 +20,63 @@ public class GZipDecompressor extends Decompressor {
|
||||
|
||||
@Override
|
||||
public void run(File fromFile, String toPath) throws Exception {
|
||||
InputStream is = IO.getInputStream(fromFile);
|
||||
GzipCompressorInputStream gzipIS = new GzipCompressorInputStream(is);
|
||||
TarArchiveInputStream tarIS = new TarArchiveInputStream(gzipIS);
|
||||
initByteProgress(readTotalSize(fromFile));
|
||||
try {
|
||||
super.run(fromFile, toPath);
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
{
|
||||
@Override
|
||||
public void run(InputStream fromStream, String toPath) throws Exception {
|
||||
try (
|
||||
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(nonClosing(fromStream));
|
||||
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)
|
||||
) {
|
||||
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);
|
||||
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(inputStream);
|
||||
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)
|
||||
) {
|
||||
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();
|
||||
gzipIS.close();
|
||||
is.close();
|
||||
return totalSize;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user