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 java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author 夜雨
* @version 2024-06-30 19:46
*/
public class ZipCompressor 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);
ZipOutputStream zipOS = new ZipOutputStream(byteOS);
for (int i = 0, total = files.size(); i < total; i++) {
String name = files.get(i).getAbsolutePath().substring(basePath.length() + 1);
ZipEntry zipEntry = new ZipEntry(name.replaceAll("\\\\", "/"));
zipOS.putNextEntry(zipEntry);
zipOS.write(IO.toBytes(files.get(i)));
zipOS.closeEntry();
if (isPause) {
synchronized (pauseLock) {
pauseLock.wait();
}
}
if (fileCallback != null) {
fileCallback.handler(toFile);
}
if (progressCallback != null) {
progressCallback.handler(1D * i / total);
}
if (isInterrupt) {
break;
}
}
zipOS.finish();
zipOS.close();
byteOS.flush();
byteOS.close();
os.flush();
os.close();
if (isInterrupt) {
IO.destroy(toFile);
}
}
}