Skip to content

Commit

Permalink
feat: 新增rar文件的解压
Browse files Browse the repository at this point in the history
  • Loading branch information
jamebal committed Jul 5, 2024
1 parent b1827d5 commit aa988d7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/com/jmal/clouddisk/util/CompressUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public static void decompress(String filePath, String outputDir, boolean isWrite
decompressTar(file, outputDir, isWrite);
} else if (filePath.endsWith(".7z")) {
decompressSevenZ(file, outputDir, isWrite);
} else if (filePath.endsWith(".rar")) {
decompressRar(file, outputDir, isWrite);
} else if (filePath.endsWith(".jar")) {
decompressJar(file, outputDir, isWrite);
} else if (filePath.endsWith(".tar.gz") || filePath.endsWith(".tgz") || filePath.endsWith(".gz")) {
Expand Down Expand Up @@ -156,7 +158,7 @@ public static void decompressTarGz(File file, String outputDir, boolean isWrite)

public static void decompressJar(File file, String outputDir, boolean isWrite) throws IOException {
JarArchiveInputStream inputStream = new JarArchiveInputStream(new FileInputStream(file));
//创建输出目录
// 创建输出目录
createDirectory(outputDir, null);
JarArchiveEntry entry;
while (Objects.nonNull(entry = inputStream.getNextEntry())) {
Expand All @@ -165,16 +167,31 @@ public static void decompressJar(File file, String outputDir, boolean isWrite) t
}

private static void decompressSevenZ(File sevenZFile, String outputDir, boolean isWrite) throws IOException {
// 创建输出目录
createDirectory(outputDir, null);
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("7z", "x", sevenZFile.getAbsolutePath(), "-o" + outputDir, "-y");
// 将输出和错误流重定向到空输出流
executingCommand(processBuilder, "7z");
}

private static void decompressRar(File sevenZFile, String outputDir, boolean isWrite) throws IOException {
// 创建输出目录
createDirectory(outputDir, null);
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("unrar", "x", "-o+", sevenZFile.getAbsolutePath(), outputDir);
executingCommand(processBuilder, "rar");
}

private static void executingCommand(ProcessBuilder processBuilder, String type) throws IOException {
// 将输出和错误流重定向到空输出流
processBuilder.redirectOutput(ProcessBuilder.Redirect.DISCARD);
processBuilder.redirectError(ProcessBuilder.Redirect.DISCARD);
try {
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Failed to extract 7z file. Exit code: " + exitCode);
throw new RuntimeException("Failed to extract " + type + " file. Exit code: " + exitCode);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down

0 comments on commit aa988d7

Please sign in to comment.