|
|
|
|
@ -9,6 +9,11 @@ import java.io.IOException;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.file.*;
|
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Author: youys
|
|
|
|
|
@ -17,6 +22,14 @@ import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
|
*/
|
|
|
|
|
public class RarUtil {
|
|
|
|
|
|
|
|
|
|
private static final Set<String> SOURCE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
|
|
|
|
".java", ".py", ".c", ".cc", ".cpp", ".cxx", ".h", ".hpp",
|
|
|
|
|
".js", ".jsx", ".ts", ".tsx", ".vue", ".go", ".php", ".rb",
|
|
|
|
|
".cs", ".m", ".mm", ".swift", ".kt", ".kts", ".scala"
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
private static final double TARGET_EXTENSION_RATIO = 0.8;
|
|
|
|
|
|
|
|
|
|
public static void unrar(String sourceFilePath, String outputDirectory) {
|
|
|
|
|
String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
|
|
|
|
|
System.out.println("result====" + s);
|
|
|
|
|
@ -34,16 +47,17 @@ public class RarUtil {
|
|
|
|
|
// un7Z("/tmp/20230301/201905962241/20230519153506_i39sv5p2.7z", "/Users/youyongsheng/Desktop/aa");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String path = "/tmp/202605112";
|
|
|
|
|
// unzip();
|
|
|
|
|
String path = "/tmp/202605112";
|
|
|
|
|
// valid(path);
|
|
|
|
|
// printSingleExtensionCodeDirs(path);
|
|
|
|
|
// removeFullCode(path);
|
|
|
|
|
removeDirectory(Paths.get(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void unzip(){
|
|
|
|
|
String sourceDir = "/tmp/202605111";
|
|
|
|
|
String sourceDir = "/tmp/2026051111";
|
|
|
|
|
String targetDir = "/tmp/202605112";
|
|
|
|
|
|
|
|
|
|
File directory = new File(sourceDir);
|
|
|
|
|
@ -126,6 +140,78 @@ public class RarUtil {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void printSingleExtensionCodeDirs(String path) {
|
|
|
|
|
File directory = new File(path);
|
|
|
|
|
File[] studentDirs = directory.listFiles();
|
|
|
|
|
if (studentDirs == null) {
|
|
|
|
|
System.out.println("No student directories found.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (File studentDir : studentDirs) {
|
|
|
|
|
if (!studentDir.isDirectory()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ExtensionScanResult result = scanSingleExtensionCodeDir(studentDir.toPath());
|
|
|
|
|
if (result.isMatched()) {
|
|
|
|
|
System.out.println(studentDir.getName()
|
|
|
|
|
+ " -> 主要是 " + result.getExtension()
|
|
|
|
|
+ " 文件, count:" + result.getTargetFileCount()
|
|
|
|
|
+ "/" + result.getSourceFileCount()
|
|
|
|
|
+ ", ratio:" + String.format("%.2f", result.getTargetRatio() * 100) + "%"
|
|
|
|
|
+ ", path:" + studentDir.getAbsolutePath());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println(studentDir.getName() + " -> 检测失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ExtensionScanResult scanSingleExtensionCodeDir(Path rootPath) throws IOException {
|
|
|
|
|
ExtensionScanResult result = new ExtensionScanResult();
|
|
|
|
|
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
|
|
|
|
|
@Override
|
|
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
|
|
|
|
if (!attrs.isRegularFile()) {
|
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String fileName = file.getFileName().toString();
|
|
|
|
|
if (shouldIgnoreWhenDetectingCodeExtension(fileName)) {
|
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String extension = getExtension(fileName);
|
|
|
|
|
if (!SOURCE_EXTENSIONS.contains(extension)) {
|
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
result.addExtension(extension);
|
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result.finish(TARGET_EXTENSION_RATIO);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean isTargetCodeExtension(String extension) {
|
|
|
|
|
return ".cs".equals(extension) || ".m".equals(extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean shouldIgnoreWhenDetectingCodeExtension(String fileName) {
|
|
|
|
|
return fileName.startsWith(".") || fileName.toLowerCase().endsWith(".meta");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getExtension(String fileName) {
|
|
|
|
|
int index = fileName.lastIndexOf(".");
|
|
|
|
|
if (index < 0 || index == fileName.length() - 1) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return fileName.substring(index).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void removeFullCode(String path){
|
|
|
|
|
// 删除完整代码
|
|
|
|
|
File directory = new File(path);
|
|
|
|
|
@ -200,4 +286,56 @@ public class RarUtil {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class ExtensionScanResult {
|
|
|
|
|
private boolean matched;
|
|
|
|
|
private String extension;
|
|
|
|
|
private int sourceFileCount;
|
|
|
|
|
private int targetFileCount;
|
|
|
|
|
private double targetRatio;
|
|
|
|
|
private final Map<String, Integer> extensionCountMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public void addExtension(String extension) {
|
|
|
|
|
sourceFileCount++;
|
|
|
|
|
extensionCountMap.put(extension, extensionCountMap.getOrDefault(extension, 0) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void finish(double targetExtensionRatio) {
|
|
|
|
|
for (String targetExtension : Arrays.asList(".cs", ".m", ".ets")) {
|
|
|
|
|
int count = extensionCountMap.getOrDefault(targetExtension, 0);
|
|
|
|
|
double ratio = sourceFileCount == 0 ? 0 : count * 1.0 / sourceFileCount;
|
|
|
|
|
if (count > targetFileCount) {
|
|
|
|
|
extension = targetExtension;
|
|
|
|
|
targetFileCount = count;
|
|
|
|
|
targetRatio = ratio;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matched = sourceFileCount > 0 && targetFileCount > 0 && targetRatio >= targetExtensionRatio;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isMatched() {
|
|
|
|
|
return matched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMatched(boolean matched) {
|
|
|
|
|
this.matched = matched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getExtension() {
|
|
|
|
|
return extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getSourceFileCount() {
|
|
|
|
|
return sourceFileCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getTargetFileCount() {
|
|
|
|
|
return targetFileCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getTargetRatio() {
|
|
|
|
|
return targetRatio;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|