Compare commits
8 Commits
master
...
manual-ana
| Author | SHA1 | Date |
|---|---|---|
|
|
ec35d45772 | 2 weeks ago |
|
|
23368fbab8 | 3 weeks ago |
|
|
3b6cf44e11 | 3 weeks ago |
|
|
d8e7045581 | 1 year ago |
|
|
1e30e94a99 | 2 years ago |
|
|
f58ab6d20e | 3 years ago |
|
|
c5c39bb23c | 3 years ago |
|
|
4b971be282 | 3 years ago |
@ -0,0 +1,445 @@
|
||||
package net.educoder.ecsonar.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
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;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* @Author: youys
|
||||
* @Date: 2023/5/19
|
||||
* @Description:
|
||||
*/
|
||||
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", ".ets"
|
||||
));
|
||||
|
||||
private static final Set<String> TARGET_EXTENSIONS = new HashSet<>(Arrays.asList(".cs", ".m", ".ets"));
|
||||
|
||||
private static final double TARGET_EXTENSION_RATIO = 0.8;
|
||||
|
||||
private static final Set<String> BUILD_AND_DEPENDENCY_DIRS = new HashSet<>(Arrays.asList(
|
||||
"node_modules", "target", "dist", "build", "out", "bin", "obj",
|
||||
".gradle", ".mvn", ".idea", ".vscode", "__pycache__",
|
||||
".venv", "venv", "env", "site-packages", ".pytest_cache", ".mypy_cache",
|
||||
".next", ".nuxt", ".output", "coverage", ".cache", ".parcel-cache", ".turbo",
|
||||
".pnpm-store", "vendor", "Pods", "DerivedData", ".cxx", ".externalNativeBuild"
|
||||
));
|
||||
|
||||
public static void unrar(String sourceFilePath, String outputDirectory) {
|
||||
String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
|
||||
System.out.println("result====" + s);
|
||||
}
|
||||
|
||||
|
||||
public static void un7Z(String sourceFilePath, String outputDirectory) {
|
||||
String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
|
||||
System.out.println("result====" + s);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
// unrar("/tmp/20230301/201905962222/20230519153409_wuiol3vn.rar", "/Users/youyongsheng/Desktop/aa");
|
||||
// un7Z("/tmp/20230301/201905962241/20230519153506_i39sv5p2.7z", "/Users/youyongsheng/Desktop/aa");
|
||||
|
||||
|
||||
// unzip();
|
||||
String path = "/tmp/202605112";
|
||||
// valid(path);
|
||||
// printSingleExtensionCodeDirs(path);
|
||||
// removeFullCode(path);
|
||||
removeDirectory(Paths.get(path));
|
||||
}
|
||||
|
||||
|
||||
public static void unzip(){
|
||||
String sourceDir = "/tmp/2026051111";
|
||||
String targetDir = "/tmp/202605112";
|
||||
unzip(sourceDir, targetDir);
|
||||
}
|
||||
|
||||
public static void unzip(String sourceDir, String targetDir){
|
||||
File directory = new File(sourceDir);
|
||||
File[] studentDirs = directory.listFiles();
|
||||
if (studentDirs == null) {
|
||||
System.out.println("No student directories found.");
|
||||
return;
|
||||
}
|
||||
|
||||
int i=0;
|
||||
for (File studentDir : studentDirs) {
|
||||
if (!studentDir.isDirectory()) {
|
||||
System.out.println("111--------------------------------");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
String studentId = studentDir.getName();
|
||||
String studentTargetDir = targetDir + "/" + studentId;
|
||||
|
||||
File[] zipFiles = studentDir.listFiles();
|
||||
if (zipFiles == null || zipFiles.length == 0) {
|
||||
System.out.println("No archive files found for student: " + studentId);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (File zipFile : zipFiles) {
|
||||
if (!zipFile.isFile()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
String fileName = zipFile.getName().toLowerCase();
|
||||
if(fileName.endsWith(".zip")){
|
||||
unzipArchive(zipFile.getAbsolutePath(), studentTargetDir);
|
||||
}else if(fileName.endsWith(".rar")){
|
||||
unRar(zipFile.getAbsolutePath(), studentTargetDir);
|
||||
}else{
|
||||
RarUtil.un7Z(zipFile.getAbsolutePath(), studentTargetDir);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error occurred while unzipping file: "
|
||||
+ zipFile.getName()
|
||||
+ ", path:" + zipFile.getAbsolutePath()
|
||||
+ ", reason:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("--------------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
public static void prepareForScan(String path) throws IOException {
|
||||
normalizeWindowsPathFiles(Paths.get(path));
|
||||
valid(path);
|
||||
printSingleExtensionCodeDirs(path);
|
||||
removeFullCode(path);
|
||||
removeDirectory(Paths.get(path));
|
||||
}
|
||||
|
||||
public static void normalizeWindowsPathFiles(Path rootPath) throws IOException {
|
||||
Path normalizedRootPath = rootPath.toAbsolutePath().normalize();
|
||||
Files.walkFileTree(normalizedRootPath, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
String fileName = file.getFileName().toString();
|
||||
if (!fileName.contains("\\")) {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
Path parent = file.getParent();
|
||||
if (parent == null) {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
Path targetPath = parent.resolve(fileName.replace("\\", "/")).normalize();
|
||||
if (!targetPath.startsWith(normalizedRootPath)) {
|
||||
System.out.println("Skip unsafe normalized path: " + file.toAbsolutePath());
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
if (fileName.endsWith("\\")) {
|
||||
Files.createDirectories(targetPath);
|
||||
Files.deleteIfExists(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
Path targetParent = targetPath.getParent();
|
||||
if (targetParent != null) {
|
||||
Files.createDirectories(targetParent);
|
||||
}
|
||||
Files.move(file, targetPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
System.out.println("Normalize windows path file: " + file.toAbsolutePath() + " -> " + targetPath);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void unzipArchive(String zipFilePath, String targetDir) throws IOException {
|
||||
System.out.println(zipFilePath + " to " + targetDir);
|
||||
unzipArchiveWithGbkAndWindowsPath(zipFilePath, targetDir);
|
||||
}
|
||||
|
||||
private static void unzipArchiveWithGbkAndWindowsPath(String zipFilePath, String targetDir) throws IOException {
|
||||
Path targetPath = Paths.get(targetDir).toAbsolutePath().normalize();
|
||||
Files.createDirectories(targetPath);
|
||||
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(
|
||||
new BufferedInputStream(new FileInputStream(zipFilePath)), Charset.forName("GBK"))) {
|
||||
ZipEntry entry;
|
||||
byte[] buffer = new byte[8192];
|
||||
while ((entry = zipInputStream.getNextEntry()) != null) {
|
||||
String entryName = entry.getName().replace("\\", "/");
|
||||
Path outputPath = targetPath.resolve(entryName).normalize();
|
||||
if (!outputPath.startsWith(targetPath)) {
|
||||
System.out.println("Skip unsafe zip entry: " + entry.getName());
|
||||
zipInputStream.closeEntry();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entry.isDirectory() || entryName.endsWith("/")) {
|
||||
Files.createDirectories(outputPath);
|
||||
} else {
|
||||
Path parent = outputPath.getParent();
|
||||
if (parent != null) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
try (FileOutputStream outputStream = new FileOutputStream(outputPath.toFile())) {
|
||||
int len;
|
||||
while ((len = zipInputStream.read(buffer)) > 0) {
|
||||
outputStream.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
zipInputStream.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void unRar(String zipFilePath, String targetDir) throws Exception {
|
||||
System.out.println(zipFilePath + " to " + targetDir);
|
||||
RarUtil.unrar(zipFilePath,targetDir);
|
||||
}
|
||||
|
||||
|
||||
public static void valid(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()) {
|
||||
|
||||
File[] files = studentDir.listFiles();
|
||||
if(files == null || files.length == 0){
|
||||
System.out.println(studentDir.getName()+" -> null");
|
||||
}
|
||||
for (File file : files) {
|
||||
System.out.println(studentDir.getName()+" ->" + file.getName());
|
||||
}
|
||||
System.out.println("--------------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
File[] studentDirs = directory.listFiles();
|
||||
if (studentDirs == null) {
|
||||
System.out.println("No student directories found.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (File studentDir : studentDirs) {
|
||||
if (studentDir.isDirectory()) {
|
||||
|
||||
String stuNo = studentDir.getName();
|
||||
File[] files = studentDir.listFiles();
|
||||
if(files.length == 2){
|
||||
for (File file : files) {
|
||||
if (file.getName().contains("完整")) {
|
||||
System.out.println(stuNo + "==11===" + file.getAbsolutePath());
|
||||
cn.hutool.core.io.FileUtil.del(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
}else if(files.length == 1){
|
||||
File file = files[0];
|
||||
File[] files1 = file.listFiles();
|
||||
if(files1 != null && files1.length == 2){
|
||||
for (File f : files1) {
|
||||
if (f.getName().contains("完整")) {
|
||||
System.out.println(stuNo + "==22===" + f.getAbsolutePath());
|
||||
FileUtil.del(f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void removeDirectory(Path rootPath) throws IOException {
|
||||
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
String folderName = dir.getFileName().toString();
|
||||
if (BUILD_AND_DEPENDENCY_DIRS.contains(folderName)) {
|
||||
System.out.println("Deleting directory: " + dir.toAbsolutePath());
|
||||
deleteDirectoryRecursively(dir);
|
||||
|
||||
return FileVisitResult.SKIP_SUBTREE; // 不再进入已删除目录
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
// 递归删除目录
|
||||
private static void deleteDirectoryRecursively(Path path) throws IOException {
|
||||
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
Files.deleteIfExists(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
Files.deleteIfExists(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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 : TARGET_EXTENSIONS) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in new issue