org.metaeffekt.bundle.sevenzip.SevenZipExecutableUtils Maven / Gradle / Ivy
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.metaeffekt.bundle.sevenzip;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class SevenZipExecutableUtils {
public static final Logger LOG = LoggerFactory.getLogger(SevenZipExecutableUtils.class);
public static String detectOS() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
return "windows";
} else if (osName.contains("mac")) {
return "mac";
} else if (osName.contains("nux") || osName.contains("nix")) {
return "linux";
} else {
return "unknown";
}
}
public static String detectArch() {
String osArch = System.getProperty("os.arch").toLowerCase();
if (osArch.contains("amd64") || osArch.contains("x86_64")) {
return "x64";
} else if (osArch.contains("86")) {
return "ia32";
} else if (osArch.contains("arm64") || osArch.contains("aarch64")) {
return "arm64";
} else if (osArch.contains("arm")) {
return "arm";
} else {
return "unknown";
}
}
public static void extractExecutable(File zipFile, File outputDirectory) throws Exception {
try (ZipFile zip = new ZipFile(zipFile)) {
String os = detectOS();
String entryName = String.format("%s/%s/7z%s",
os, detectArch(), os.equals("windows") ? "a.exe" : "z");
LOG.info("Looking for entry: [{}] in archive: [{}}", entryName, zipFile.getAbsolutePath());
ZipArchiveEntry entry = zip.getEntry(entryName);
if (entry == null) {
throw new Exception("Executable not found in archive: " + entryName);
}
File outputFile = new File(outputDirectory, entryName);
outputFile.getParentFile().mkdirs();
try (InputStream is = zip.getInputStream(entry);
OutputStream osStream = new FileOutputStream(outputFile)) {
IOUtils.copy(is, osStream);
}
boolean setToExecutableSuccess = outputFile.setExecutable(true);
if (!setToExecutableSuccess) {
throw new Exception("Failed to set executable flag on extracted file");
}
LOG.info("Extracted 7-Zip executable to: [{}]", outputFile.getAbsolutePath());
} catch (IOException e) {
throw new Exception("Failed to extract executable from archive", e);
}
}
public static File getBinaryFile() {
File outputDirectory = new File(System.getProperty("java.io.tmpdir"), "7zip");
String os = detectOS();
String binaryPath = String.format("%s/%s/7z%s",
os, detectArch(), os.equals("windows") ? "a.exe" : "z");
return new File(outputDirectory, binaryPath);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy