253 lines
7.1 KiB
TypeScript
253 lines
7.1 KiB
TypeScript
import fs from "fs";
|
||
import path from "path";
|
||
import { execSync } from "child_process";
|
||
import AdmZip from "adm-zip";
|
||
|
||
const DISALLOWED_EXTENSIONS = new Set([
|
||
".exe",
|
||
".o",
|
||
".class",
|
||
".bin",
|
||
".dll",
|
||
".vcxproj",
|
||
".filters",
|
||
".user",
|
||
".sln",
|
||
]);
|
||
const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
|
||
const DISALLOWED_DIRECTORIES = new Set([
|
||
".vs",
|
||
".idea",
|
||
"__pycache__",
|
||
"node_modules",
|
||
".git",
|
||
"x64",
|
||
"x86",
|
||
"__MACOSX",
|
||
".vscode",
|
||
]);
|
||
let deletedItems: string[] = [];
|
||
|
||
// 清理文件名
|
||
const sanitizeFileName = (fileName: Buffer): string => {
|
||
// 对文件名进行标准化处理,解决编码问题
|
||
const decodedName = fileName.toString("utf8");
|
||
return decodedName;
|
||
};
|
||
|
||
// 检查是否是压缩文件
|
||
const isCompressedFile = (filePath: string): boolean => {
|
||
return (
|
||
filePath.endsWith(".zip") ||
|
||
filePath.endsWith(".7z") ||
|
||
filePath.endsWith(".rar")
|
||
);
|
||
};
|
||
|
||
// 测试是否安装了7z和unrar
|
||
const preTest = () => {
|
||
try {
|
||
execSync("7z --help");
|
||
} catch (e) {
|
||
console.error("[ERROR] 7z 未安装,请先安装 7z。");
|
||
process.exit(1);
|
||
}
|
||
|
||
try {
|
||
execSync("unrar");
|
||
} catch (e) {
|
||
console.error("[ERROR] unrar 未安装,请先安装 unrar。");
|
||
process.exit(1);
|
||
}
|
||
};
|
||
|
||
// 检查文件是否可能是Linux可执行文件(无后缀的二进制)
|
||
const isLinuxExecutable = (filePath: string): boolean => {
|
||
if (fs.statSync(filePath).isFile() && path.extname(filePath) === "") {
|
||
const stats = fs.statSync(filePath);
|
||
return (stats.mode & 0o111) !== 0;
|
||
}
|
||
return false;
|
||
};
|
||
|
||
// 使用7z解压文件
|
||
const extractUsing7z = (filePath: string, extractTo: string): void => {
|
||
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
||
if (!fs.existsSync(extractTo)) {
|
||
fs.mkdirSync(extractTo, { recursive: true });
|
||
}
|
||
try {
|
||
execSync(`7z x "${filePath}" -o"${extractTo}"`);
|
||
console.log(`[INFO] 解压完成: ${filePath}`);
|
||
} catch (e) {
|
||
console.error(`[ERROR] 解压失败: ${e}`);
|
||
}
|
||
};
|
||
|
||
// 使用unrar解压文件
|
||
const extractRar = (filePath: string, extractTo: string): void => {
|
||
console.log(`[INFO] 使用 unrar 解压文件: ${filePath} 到目录: ${extractTo}`);
|
||
if (!fs.existsSync(extractTo)) {
|
||
fs.mkdirSync(extractTo, { recursive: true });
|
||
}
|
||
try {
|
||
execSync(`unrar x -o+ "${filePath}" "${extractTo}"`);
|
||
console.log(`[INFO] 解压完成: ${filePath}`);
|
||
} catch (e) {
|
||
console.error(`[ERROR] 解压失败: ${e}`);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 解压缩文件
|
||
* @param inputDirectory 输入目录
|
||
* @param outputDirectory 输出目录
|
||
* @deprecated 存在编码问题,暂时不使用
|
||
*/
|
||
const extractZip = (filePath: string, extractTo: string): void => {
|
||
console.log(`[INFO] 解压缩文件: ${filePath} 到目录: ${extractTo}`);
|
||
if (!fs.existsSync(extractTo)) {
|
||
fs.mkdirSync(extractTo, { recursive: true });
|
||
}
|
||
const zip = new AdmZip(filePath);
|
||
|
||
// 遍历所有的文件条目,清理文件名中的非法字符
|
||
zip.getEntries().forEach((entry) => {
|
||
if (entry.entryName) {
|
||
try {
|
||
// 清理文件名中的非法字符
|
||
const sanitizedName = sanitizeFileName(entry.rawEntryName);
|
||
entry.entryName = sanitizedName; // 更新文件名
|
||
} catch (e) {
|
||
console.error(
|
||
`[ERROR] 解压时处理文件名失败: ${entry.entryName}`
|
||
);
|
||
}
|
||
}
|
||
});
|
||
|
||
// 解压文件
|
||
zip.extractAllTo(extractTo, true);
|
||
};
|
||
|
||
// 递归处理目录并过滤文件
|
||
const processDirectory = (
|
||
inputDirectory: string,
|
||
outputDirectory: string
|
||
): void => {
|
||
console.log(`[INFO] 开始处理目录: ${inputDirectory}`);
|
||
fs.readdirSync(inputDirectory).forEach((item) => {
|
||
const itemPath = path.join(inputDirectory, item);
|
||
const stat = fs.statSync(itemPath);
|
||
|
||
if (stat.isDirectory()) {
|
||
// 排除不需要的目录
|
||
if (DISALLOWED_DIRECTORIES.has(item)) {
|
||
deletedItems.push(`目录: ${itemPath}`);
|
||
fs.rmdirSync(itemPath, { recursive: true });
|
||
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
||
} else {
|
||
// 创建对应的输出目录
|
||
const outputDirPath = path.join(outputDirectory, item);
|
||
if (!fs.existsSync(outputDirPath)) {
|
||
fs.mkdirSync(outputDirPath, { recursive: true });
|
||
}
|
||
processDirectory(itemPath, outputDirPath); // 递归处理子目录
|
||
}
|
||
} else if (stat.isFile()) {
|
||
const ext = path.extname(item).toLowerCase();
|
||
|
||
// 如果是压缩文件,解压后递归处理
|
||
if (isCompressedFile(itemPath)) {
|
||
const relativePath = path.relative(
|
||
inputDirectory,
|
||
path.dirname(itemPath)
|
||
);
|
||
const outputDirPath = path.join(
|
||
outputDirectory,
|
||
relativePath,
|
||
path.basename(itemPath, path.extname(itemPath)) +
|
||
"_extracted"
|
||
);
|
||
|
||
if (!fs.existsSync(outputDirPath)) {
|
||
fs.mkdirSync(outputDirPath, { recursive: true });
|
||
}
|
||
|
||
if (itemPath.endsWith(".zip")) {
|
||
// extractZip(itemPath, outputDirPath); // 存在编码问题,暂时不使用
|
||
extractUsing7z(itemPath, outputDirPath);
|
||
} else if (itemPath.endsWith(".7z")) {
|
||
extractUsing7z(itemPath, outputDirPath);
|
||
} else if (itemPath.endsWith(".rar")) {
|
||
extractRar(itemPath, outputDirPath);
|
||
}
|
||
|
||
// 递归处理解压后的内容
|
||
processDirectory(outputDirPath, outputDirPath);
|
||
|
||
// 重新压缩处理后的内容
|
||
const outputZip = path.join(
|
||
outputDirectory,
|
||
relativePath,
|
||
path.basename(itemPath)
|
||
);
|
||
const zip = new AdmZip();
|
||
zip.addLocalFolder(outputDirPath);
|
||
zip.writeZip(outputZip);
|
||
|
||
// 删除临时目录
|
||
console.log(`[DELETE] 删除临时目录: ${outputDirPath}`);
|
||
fs.rmSync(outputDirPath, { recursive: true, force: true });
|
||
} else if (
|
||
DISALLOWED_EXTENSIONS.has(ext) ||
|
||
DISALLOWED_KEYWORDS.has(item) ||
|
||
isLinuxExecutable(itemPath)
|
||
) {
|
||
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
||
deletedItems.push(`文件: ${itemPath}`);
|
||
fs.rmSync(itemPath); // 删除不需要的文件
|
||
} else {
|
||
// 将文件复制到对应的输出目录
|
||
const outputFilePath = path.join(outputDirectory, item);
|
||
fs.copyFileSync(itemPath, outputFilePath);
|
||
console.log(
|
||
`[KEEP] 保留文件: ${itemPath} 并复制到 ${outputFilePath}`
|
||
);
|
||
}
|
||
}
|
||
});
|
||
};
|
||
|
||
// 写入统计文件
|
||
const writeLog = (outputDirectory: string): void => {
|
||
const logFile = path.join(outputDirectory, "result_log.txt");
|
||
fs.writeFileSync(logFile, "以下是被删除的目录和文件列表:\n");
|
||
fs.appendFileSync(logFile, deletedItems.join("\n"));
|
||
console.log(`[INFO] 统计文件已生成: ${logFile}`);
|
||
};
|
||
|
||
// 主函数
|
||
const main = (): void => {
|
||
preTest(); // 测试是否安装了7z和unrar
|
||
const inputDirectory = path.join(process.cwd(), "in"); // 输入文件夹
|
||
const outputDirectory = path.join(process.cwd(), "out"); // 输出文件夹
|
||
|
||
if (!fs.existsSync(inputDirectory)) {
|
||
console.error(`[ERROR] 输入目录 'in' 不存在,请检查。`);
|
||
return;
|
||
}
|
||
|
||
if (fs.existsSync(outputDirectory)) {
|
||
fs.rmSync(outputDirectory, { recursive: true }); // 清空输出目录
|
||
}
|
||
fs.mkdirSync(outputDirectory, { recursive: true });
|
||
|
||
console.log("[INFO] 开始处理压缩文件...");
|
||
processDirectory(inputDirectory, outputDirectory);
|
||
writeLog(outputDirectory);
|
||
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
||
};
|
||
|
||
main();
|