This commit is contained in:
@@ -5,46 +5,81 @@ import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 7z 压缩器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2024-06-30 19:40
|
||||
*/
|
||||
public class Z7Compressor 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();
|
||||
|
||||
SevenZOutputFile out = new SevenZOutputFile(toFile);
|
||||
SevenZArchiveEntry entry;
|
||||
for (int i = 0, total = files.size(); i < total; i++) {
|
||||
String name = files.get(i).getAbsolutePath().substring(basePath.length() + 1);
|
||||
entry = out.createArchiveEntry(files.get(i), name.replaceAll("\\\\", "/"));
|
||||
out.putArchiveEntry(entry);
|
||||
out.write(IO.toBytes(files.get(i)));
|
||||
out.closeArchiveEntry();
|
||||
|
||||
if (isPause) {
|
||||
synchronized (pauseLock) {
|
||||
pauseLock.wait();
|
||||
}
|
||||
}
|
||||
if (fileCallback != null) {
|
||||
fileCallback.handler(toFile);
|
||||
}
|
||||
if (progressCallback != null) {
|
||||
progressCallback.handler(1D * i / total);
|
||||
}
|
||||
if (isInterrupt) {
|
||||
break;
|
||||
public void run(String fromPath, OutputStream toStream) throws Exception {
|
||||
Path tempFile = Files.createTempFile("timi-compress-", ".7z");
|
||||
try {
|
||||
run(fromPath, tempFile.toFile());
|
||||
try (InputStream inputStream = Files.newInputStream(tempFile)) {
|
||||
transfer(inputStream, toStream, false);
|
||||
toStream.flush();
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String fromPath, File toFile) throws Exception {
|
||||
File fromFile = new File(fromPath);
|
||||
List<File> files = IO.listFile(fromFile);
|
||||
String basePath = files.getFirst().getParentFile().getAbsolutePath();
|
||||
initByteProgress(IO.calcSize(fromFile));
|
||||
try (SevenZOutputFile outputFile = new SevenZOutputFile(toFile)) {
|
||||
for (File sourceFile : files) {
|
||||
String name = sourceFile.getAbsolutePath().substring(basePath.length() + 1);
|
||||
SevenZArchiveEntry entry = outputFile.createArchiveEntry(sourceFile, normalizeEntryName(name));
|
||||
outputFile.putArchiveEntry(entry);
|
||||
writeSevenZEntry(outputFile, sourceFile);
|
||||
outputFile.closeArchiveEntry();
|
||||
handleFile(sourceFile);
|
||||
}
|
||||
outputFile.finish();
|
||||
finishProgress();
|
||||
} catch (Exception exception) {
|
||||
if (isInterrupt) {
|
||||
IO.destroy(toFile);
|
||||
}
|
||||
throw exception;
|
||||
} finally {
|
||||
resetProgress();
|
||||
}
|
||||
out.close();
|
||||
if (isInterrupt) {
|
||||
IO.destroy(toFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入 7z 条目内容
|
||||
*
|
||||
* @param outputFile 7z 输出文件
|
||||
* @param sourceFile 源文件
|
||||
* @throws Exception 写入失败
|
||||
*/
|
||||
private void writeSevenZEntry(SevenZOutputFile outputFile, File sourceFile) throws Exception {
|
||||
byte[] buffer = new byte[8192];
|
||||
try (InputStream inputStream = IO.getInputStream(sourceFile)) {
|
||||
int length;
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
awaitIfPaused();
|
||||
ensureRunning();
|
||||
outputFile.write(buffer, 0, length);
|
||||
handleTransferred(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user