60 lines
1.4 KiB
Java
60 lines
1.4 KiB
Java
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);
|
|
}
|
|
}
|
|
} |