This commit is contained in:
@@ -4,57 +4,40 @@ import com.imyeyu.io.IO;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* Zip 压缩器
|
||||
*
|
||||
* @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));
|
||||
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);
|
||||
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();
|
||||
initByteProgress(IO.calcSize(fromFile));
|
||||
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(nonClosing(toStream)))) {
|
||||
for (File sourceFile : files) {
|
||||
String name = sourceFile.getAbsolutePath().substring(basePath.length() + 1);
|
||||
zipOutputStream.putNextEntry(new ZipEntry(normalizeEntryName(name)));
|
||||
try (InputStream inputStream = IO.getInputStream(sourceFile)) {
|
||||
transfer(inputStream, zipOutputStream);
|
||||
}
|
||||
zipOutputStream.closeEntry();
|
||||
handleFile(sourceFile);
|
||||
}
|
||||
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);
|
||||
zipOutputStream.finish();
|
||||
finishProgress();
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user