260 lines
9.8 KiB
JavaScript
260 lines
9.8 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const child_process_1 = require("child_process");
|
|
const adm_zip_1 = __importDefault(require("adm-zip"));
|
|
const DISALLOWED_EXTENSIONS = new Set([
|
|
".exe",
|
|
".o",
|
|
".obj",
|
|
".ilk",
|
|
".pdb",
|
|
".idb",
|
|
".recipe",
|
|
".class",
|
|
".bin",
|
|
".dll",
|
|
".vcxproj",
|
|
".filters",
|
|
".user",
|
|
".sln",
|
|
]);
|
|
const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
|
|
const DISALLOWED_DIRECTORIES = new Set([
|
|
".vs",
|
|
".idea",
|
|
".tlog",
|
|
"__pycache__",
|
|
"node_modules",
|
|
"pthreads.2",
|
|
"pthreadVC2",
|
|
".git",
|
|
"x64",
|
|
"x86",
|
|
"Debug",
|
|
"Release",
|
|
"build",
|
|
"dist",
|
|
"bin",
|
|
"obj",
|
|
"output",
|
|
"__MACOSX",
|
|
".vscode",
|
|
]);
|
|
let deletedFiles = []; // 存储删除的文件信息
|
|
let deletedDirectories = []; // 存储删除的文件夹信息
|
|
let deletedEmptyDirectories = []; // 存储删除的空文件夹
|
|
// 清理文件名
|
|
const sanitizeFileName = (fileName) => {
|
|
const decodedName = fileName.toString("utf8");
|
|
return decodedName;
|
|
};
|
|
// 检查是否是压缩文件
|
|
const isCompressedFile = (filePath) => {
|
|
return (filePath.endsWith(".zip") ||
|
|
filePath.endsWith(".7z") ||
|
|
filePath.endsWith(".rar"));
|
|
};
|
|
// 测试是否安装了7z和unrar
|
|
const preTest = () => {
|
|
try {
|
|
(0, child_process_1.execSync)("7z --help");
|
|
}
|
|
catch (e) {
|
|
console.error("[ERROR] 7z 未安装,请先安装 7z。");
|
|
process.exit(1);
|
|
}
|
|
try {
|
|
(0, child_process_1.execSync)("unrar");
|
|
}
|
|
catch (e) {
|
|
console.error("[ERROR] unrar 未安装,请先安装 unrar。");
|
|
process.exit(1);
|
|
}
|
|
};
|
|
// 使用7z解压文件
|
|
const extractUsing7z = (filePath, extractTo) => {
|
|
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
|
if (!fs_1.default.existsSync(extractTo)) {
|
|
fs_1.default.mkdirSync(extractTo, { recursive: true });
|
|
}
|
|
try {
|
|
(0, child_process_1.execSync)(`7z x "${filePath}" -o"${extractTo}"`);
|
|
console.log(`[INFO] 解压完成: ${filePath}`);
|
|
}
|
|
catch (e) {
|
|
console.error(`[ERROR] 解压失败: ${e}`);
|
|
}
|
|
};
|
|
// 使用unrar解压文件
|
|
const extractRar = (filePath, extractTo) => {
|
|
console.log(`[INFO] 使用 unrar 解压文件: ${filePath} 到目录: ${extractTo}`);
|
|
if (!fs_1.default.existsSync(extractTo)) {
|
|
fs_1.default.mkdirSync(extractTo, { recursive: true });
|
|
}
|
|
try {
|
|
(0, child_process_1.execSync)(`unrar x -o+ "${filePath}" "${extractTo}"`);
|
|
console.log(`[INFO] 解压完成: ${filePath}`);
|
|
}
|
|
catch (e) {
|
|
console.error(`[ERROR] 解压失败: ${e}`);
|
|
}
|
|
};
|
|
// 递归处理目录并过滤文件
|
|
const processDirectory = (inputDirectory, outputDirectory) => {
|
|
console.log(`[INFO] 开始处理目录: ${inputDirectory}`);
|
|
fs_1.default.readdirSync(inputDirectory).forEach((item) => {
|
|
const itemPath = path_1.default.join(inputDirectory, item);
|
|
const stat = fs_1.default.statSync(itemPath);
|
|
if (stat.isDirectory()) {
|
|
// 排除不需要的目录
|
|
if (DISALLOWED_DIRECTORIES.has(item)) {
|
|
deletedDirectories.push({
|
|
type: "文件夹",
|
|
name: item,
|
|
originalPath: itemPath,
|
|
});
|
|
fs_1.default.rmSync(itemPath, { recursive: true, force: true });
|
|
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
|
}
|
|
else {
|
|
// 创建对应的输出目录
|
|
const outputDirPath = path_1.default.join(outputDirectory, item);
|
|
if (!fs_1.default.existsSync(outputDirPath)) {
|
|
fs_1.default.mkdirSync(outputDirPath, { recursive: true });
|
|
}
|
|
processDirectory(itemPath, outputDirPath); // 递归处理子目录
|
|
}
|
|
}
|
|
else if (stat.isFile()) {
|
|
const ext = path_1.default.extname(item).toLowerCase();
|
|
// 如果是压缩文件,解压后递归处理
|
|
if (isCompressedFile(itemPath)) {
|
|
const relativePath = path_1.default.relative(inputDirectory, path_1.default.dirname(itemPath));
|
|
const outputDirPath = path_1.default.join(outputDirectory, relativePath, path_1.default.basename(itemPath, path_1.default.extname(itemPath)) + "_extracted");
|
|
if (!fs_1.default.existsSync(outputDirPath)) {
|
|
fs_1.default.mkdirSync(outputDirPath, { recursive: true });
|
|
}
|
|
if (itemPath.endsWith(".zip")) {
|
|
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_1.default.join(outputDirectory, relativePath, path_1.default.basename(itemPath));
|
|
const zip = new adm_zip_1.default();
|
|
zip.addLocalFolder(outputDirPath);
|
|
zip.writeZip(outputZip);
|
|
// 删除临时目录
|
|
console.log(`[DELETE] 删除临时目录: ${outputDirPath}`);
|
|
fs_1.default.rmSync(outputDirPath, { recursive: true, force: true });
|
|
}
|
|
else if (DISALLOWED_EXTENSIONS.has(ext) ||
|
|
DISALLOWED_KEYWORDS.has(item)) {
|
|
deletedFiles.push({
|
|
type: "文件",
|
|
name: item,
|
|
originalPath: itemPath,
|
|
});
|
|
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
|
fs_1.default.rmSync(itemPath, { recursive: true, force: true }); // 删除不需要的文件
|
|
}
|
|
else {
|
|
// 将文件复制到对应的输出目录
|
|
const outputFilePath = path_1.default.join(outputDirectory, item);
|
|
if (itemPath === outputFilePath) {
|
|
return;
|
|
}
|
|
fs_1.default.copyFileSync(itemPath, outputFilePath);
|
|
console.log(`[KEEP] 保留文件: ${itemPath} 并复制到 ${outputFilePath}`);
|
|
}
|
|
}
|
|
// 删除空目录
|
|
deleteEmptyDirectories(inputDirectory);
|
|
});
|
|
};
|
|
// 删除空目录
|
|
const deleteEmptyDirectories = (dir) => {
|
|
// 如果文件夹不存在直接跳过
|
|
if (!fs_1.default.existsSync(dir)) {
|
|
return;
|
|
}
|
|
const files = fs_1.default.readdirSync(dir);
|
|
if (files.length === 0) {
|
|
fs_1.default.rmSync(dir, { recursive: true, force: true });
|
|
console.log(`[DELETE] 删除空目录: ${dir}`);
|
|
deletedEmptyDirectories.push({
|
|
type: "文件夹",
|
|
name: path_1.default.basename(dir),
|
|
originalPath: dir,
|
|
});
|
|
const parentDir = path_1.default.dirname(dir);
|
|
deleteEmptyDirectories(parentDir); // 递归检查父目录
|
|
}
|
|
};
|
|
// 输出删除信息到控制台
|
|
const printDeletedItems = () => {
|
|
let result = "\n[INFO] 删除的文件和文件夹:";
|
|
// 输出文件夹
|
|
if (deletedDirectories.length > 0) {
|
|
result += "\n[INFO] 被删除的文件夹:";
|
|
deletedDirectories.forEach((item) => {
|
|
result += `\n${item.type} ${item.name} ${item.originalPath}`;
|
|
});
|
|
}
|
|
// 输出文件
|
|
if (deletedFiles.length > 0) {
|
|
result += "\n[INFO] 被删除的文件:";
|
|
deletedFiles.forEach((item) => {
|
|
result += `\n${item.type} ${item.name} ${item.originalPath}`;
|
|
});
|
|
}
|
|
// 输出空文件夹
|
|
if (deletedEmptyDirectories.length > 0) {
|
|
result += "\n[INFO] 被删除的空文件夹:";
|
|
deletedEmptyDirectories.forEach((item) => {
|
|
result += `\n${item.type} ${item.name} ${item.originalPath}`;
|
|
});
|
|
}
|
|
return result;
|
|
};
|
|
// 写入日志到文件
|
|
const writeLogToFile = (log) => {
|
|
const logFilePath = path_1.default.join(process.cwd(), "out/result.log");
|
|
fs_1.default.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
|
|
// 一共删除了多少
|
|
console.log(`[INFO] 共删除了 ${deletedFiles.length} 个文件,${deletedDirectories.length} 个文件夹,${deletedEmptyDirectories.length} 个空文件夹。`);
|
|
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
|
|
};
|
|
// 主函数
|
|
const main = () => {
|
|
preTest(); // 测试是否安装了7z和unrar
|
|
const inputDirectory = path_1.default.join(process.cwd(), "in"); // 输入文件夹
|
|
const outputDirectory = path_1.default.join(process.cwd(), "out"); // 输出文件夹
|
|
if (!fs_1.default.existsSync(inputDirectory)) {
|
|
console.error(`[ERROR] 输入目录 'in' 不存在,请检查。`);
|
|
return;
|
|
}
|
|
if (fs_1.default.existsSync(outputDirectory)) {
|
|
fs_1.default.rmSync(outputDirectory, { recursive: true, force: true }); // 清空输出目录
|
|
}
|
|
fs_1.default.mkdirSync(outputDirectory, { recursive: true });
|
|
console.log("[INFO] 开始处理压缩文件...");
|
|
processDirectory(inputDirectory, outputDirectory);
|
|
// 输出删除的文件和文件夹
|
|
const deletedItemsLog = printDeletedItems();
|
|
writeLogToFile(deletedItemsLog);
|
|
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
|
};
|
|
main();
|
|
//# sourceMappingURL=index.js.map
|