chore: cache current working dir in static field, other minor changes
This commit is contained in:
@@ -41,7 +41,7 @@ public enum ResourceType {
|
||||
}
|
||||
|
||||
public static ResourceType getFileType(String fileName) {
|
||||
if (fileName.matches("[^/]+/resources.pb")) {
|
||||
if (fileName.endsWith("/resources.pb")) {
|
||||
return ARSC;
|
||||
}
|
||||
int dot = fileName.lastIndexOf('.');
|
||||
|
||||
@@ -14,14 +14,11 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -117,7 +114,11 @@ public class FileUtils {
|
||||
try (Stream<Path> pathStream = Files.walk(dir)) {
|
||||
pathStream.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
.forEach(file -> {
|
||||
if (!file.delete()) {
|
||||
LOG.warn("Failed to remove file: {}", file.getAbsolutePath());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new JadxRuntimeException("Failed to delete directory " + dir, e);
|
||||
}
|
||||
@@ -259,45 +260,6 @@ public class FileUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<String> getZipFileList(File file) {
|
||||
List<String> filesList = new ArrayList<>();
|
||||
try (ZipFile zipFile = new ZipFile(file)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
filesList.add(entry.getName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error read zip file '{}'", file.getAbsolutePath(), e);
|
||||
}
|
||||
return filesList;
|
||||
}
|
||||
|
||||
public static boolean isApkFile(File file) {
|
||||
if (!isZipFile(file)) {
|
||||
return false;
|
||||
}
|
||||
List<String> filesList = getZipFileList(file);
|
||||
return filesList.contains("AndroidManifest.xml")
|
||||
&& filesList.contains("classes.dex");
|
||||
}
|
||||
|
||||
public static boolean isZipDexFile(File file) {
|
||||
if (!isZipFile(file) || !isZipFileCanBeOpen(file)) {
|
||||
return false;
|
||||
}
|
||||
List<String> filesList = getZipFileList(file);
|
||||
return filesList.contains("classes.dex");
|
||||
}
|
||||
|
||||
private static boolean isZipFileCanBeOpen(File file) {
|
||||
try (ZipFile zipFile = new ZipFile(file)) {
|
||||
return zipFile.entries().hasMoreElements();
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getPathBaseName(Path file) {
|
||||
String fileName = file.getFileName().toString();
|
||||
int extEndIndex = fileName.lastIndexOf('.');
|
||||
|
||||
+12
@@ -1,6 +1,7 @@
|
||||
package jadx.api.plugins.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -9,6 +10,17 @@ import java.nio.file.Path;
|
||||
|
||||
public class CommonFileUtils {
|
||||
|
||||
public static final File CWD = getCWD();
|
||||
public static final Path CWD_PATH = CWD.toPath();
|
||||
|
||||
private static File getCWD() {
|
||||
try {
|
||||
return new File(".").getCanonicalFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to init current working dir constant", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Path saveToTempFile(InputStream in, String suffix) throws IOException {
|
||||
return saveToTempFile(null, in, suffix);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ZipSecurity {
|
||||
// and prevents cases like "../classes.dex", to limit output only to the specified directory
|
||||
public static boolean isValidZipEntryName(String entryName) {
|
||||
try {
|
||||
File currentPath = new File(".").getCanonicalFile();
|
||||
File currentPath = CommonFileUtils.CWD;
|
||||
File canonical = new File(currentPath, entryName).getCanonicalFile();
|
||||
if (isInSubDirectoryInternal(currentPath, canonical)) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user