|
@@ -4,12 +4,11 @@ import com.github.junrar.Archive;
|
|
|
import com.github.junrar.rarfile.FileHeader;
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
+import java.io.*;
|
|
|
|
|
|
/**
|
|
|
- * @author : wangping
|
|
|
- * @createDate: 2021/7/12
|
|
|
+ * @author : wanger
|
|
|
+ * @createDate: 2025/2/24
|
|
|
* @description:解压缩工具
|
|
|
**/
|
|
|
|
|
@@ -24,18 +23,63 @@ public class UnPackageUtils {
|
|
|
* //* @param password 解压密码(如果有)
|
|
|
*/
|
|
|
public static void unPackZip(File zipFile, String destPath) {
|
|
|
-
|
|
|
try {
|
|
|
- ZipFile zip = new ZipFile(zipFile);
|
|
|
- /*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
|
|
|
- zip.setFileNameCharset("GBK");
|
|
|
- zip.extractAll(destPath);
|
|
|
+ String system = getSystem();
|
|
|
+ String command = "";
|
|
|
+ if (system.equals("windows")) {
|
|
|
+ command = String.format("\"%s\" x \"%s\" \"%s\"", "C:\\Program Files\\WinRAR\\WinRAR.exe", zipFile.getAbsolutePath(), destPath);
|
|
|
+ } else {
|
|
|
+ command = String.format("%s \"%s\" -d \"%s\"", "sudo unzip", zipFile.getAbsolutePath(), destPath);
|
|
|
+ // 创建ProcessBuilder对象
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder("unzip", zipFile.getAbsolutePath(), "-d", destPath);
|
|
|
+
|
|
|
+ // 启动进程
|
|
|
+ Process process = processBuilder.start();
|
|
|
+
|
|
|
+ // 读取标准输出流
|
|
|
+ BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = stdInput.readLine()) != null) {
|
|
|
+ System.out.println("STDOUT: " + line);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取标准错误流
|
|
|
+ BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
|
|
+ while ((line = stdError.readLine()) != null) {
|
|
|
+ System.out.println("STDERR: " + line);
|
|
|
+ }
|
|
|
|
|
|
- // 如果解压需要密码
|
|
|
-// if (zip.isEncrypted()) {
|
|
|
-// zip.setPassword(password);
|
|
|
-// }
|
|
|
+ // 等待命令执行完成
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ System.out.println("Exited with code: " + exitCode);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ System.out.println("开始执行解压命令");
|
|
|
+ System.out.println(command);
|
|
|
+ // 执行命令
|
|
|
+ Process process = Runtime.getRuntime().exec(command);
|
|
|
+ InputStream inputStream = process.getInputStream();
|
|
|
+ // 读取输出
|
|
|
+ int ch;
|
|
|
+ while ((ch = inputStream.read()) != -1) {
|
|
|
+ System.out.print((char) ch);
|
|
|
+ }
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ System.out.println("Exit code: " + exitCode);
|
|
|
+ System.out.println("结束执行解压命令");
|
|
|
} catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSystem() {
|
|
|
+ String osName = System.getProperty("os.name").toLowerCase();
|
|
|
+ if (osName.contains("win")) {
|
|
|
+ return "windows";
|
|
|
+ } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
|
|
|
+ return "linux";
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|