Initial project

This commit is contained in:
Timi
2025-07-14 15:27:37 +08:00
parent 063520868e
commit 47f1c9520e
27 changed files with 1043 additions and 94 deletions

View File

@@ -0,0 +1,60 @@
package com.imyeyu.compress;
import com.imyeyu.io.IO;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;
/**
* @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));
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();
}
}
if (fileCallback != null) {
fileCallback.handler(toFile);
}
if (progressCallback != null) {
progressCallback.handler(1D * i / total);
}
if (isInterrupt) {
break;
}
}
tarOS.finish();
tarOS.close();
gzipOS.finish();
gzipOS.close();
byteOS.flush();
byteOS.close();
os.close();
}
}