This commit is contained in:
@@ -7,54 +7,44 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Gzip 压缩器
|
||||
* 当前实现保持与原行为一致,实际输出为 tar.gz
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:40
|
||||
*/
|
||||
public class GZipCompressor extends Compressor {
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, File toFile) throws Exception {
|
||||
List<File> files = IO.listFile(new File(fromPath));
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
|
||||
OutputStream os = IO.getOutputStream(toFile);
|
||||
BufferedOutputStream byteOS = new BufferedOutputStream(os);
|
||||
GzipCompressorOutputStream gzipOS = new GzipCompressorOutputStream(byteOS);
|
||||
TarArchiveOutputStream tarOS = new TarArchiveOutputStream(gzipOS);
|
||||
|
||||
TarArchiveEntry tarEntry;
|
||||
for (int i = 0, total = files.size(); i < total; i++) {
|
||||
String name = files.get(i).getAbsolutePath().substring(basePath.length() + 1);
|
||||
tarEntry = new TarArchiveEntry(files.get(i), name.replaceAll("\\\\", "/"));
|
||||
tarOS.putArchiveEntry(tarEntry);
|
||||
tarOS.write(IO.toBytes(files.get(i)));
|
||||
tarOS.closeArchiveEntry();
|
||||
|
||||
if (isPause) {
|
||||
synchronized (pauseLock) {
|
||||
pauseLock.wait();
|
||||
initByteProgress(IO.calcSize(fromFile));
|
||||
try (
|
||||
GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(new BufferedOutputStream(nonClosing(toStream)));
|
||||
TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream)
|
||||
) {
|
||||
for (File sourceFile : files) {
|
||||
String name = sourceFile.getAbsolutePath().substring(basePath.length() + 1);
|
||||
TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, normalizeEntryName(name));
|
||||
tarOutputStream.putArchiveEntry(tarEntry);
|
||||
try (InputStream inputStream = IO.getInputStream(sourceFile)) {
|
||||
transfer(inputStream, tarOutputStream);
|
||||
}
|
||||
tarOutputStream.closeArchiveEntry();
|
||||
handleFile(sourceFile);
|
||||
}
|
||||
if (fileCallback != null) {
|
||||
fileCallback.handler(toFile);
|
||||
}
|
||||
if (progressCallback != null) {
|
||||
progressCallback.handler(1D * i / total);
|
||||
}
|
||||
if (isInterrupt) {
|
||||
break;
|
||||
}
|
||||
tarOutputStream.finish();
|
||||
gzipOutputStream.finish();
|
||||
finishProgress();
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
tarOS.finish();
|
||||
tarOS.close();
|
||||
gzipOS.finish();
|
||||
gzipOS.close();
|
||||
byteOS.flush();
|
||||
byteOS.close();
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user