log
This commit is contained in:
parent
871fd04802
commit
e16d231e3a
95
lib/index.js
95
lib/index.js
|
|
@ -30,10 +30,10 @@ const DISALLOWED_DIRECTORIES = new Set([
|
||||||
"__MACOSX",
|
"__MACOSX",
|
||||||
".vscode",
|
".vscode",
|
||||||
]);
|
]);
|
||||||
let deletedItems = [];
|
let deletedFiles = []; // 存储删除的文件信息
|
||||||
|
let deletedDirectories = []; // 存储删除的文件夹信息
|
||||||
// 清理文件名
|
// 清理文件名
|
||||||
const sanitizeFileName = (fileName) => {
|
const sanitizeFileName = (fileName) => {
|
||||||
// 对文件名进行标准化处理,解决编码问题
|
|
||||||
const decodedName = fileName.toString("utf8");
|
const decodedName = fileName.toString("utf8");
|
||||||
return decodedName;
|
return decodedName;
|
||||||
};
|
};
|
||||||
|
|
@ -60,14 +60,6 @@ const preTest = () => {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// 检查文件是否可能是Linux可执行文件(无后缀的二进制)
|
|
||||||
const isLinuxExecutable = (filePath) => {
|
|
||||||
if (fs_1.default.statSync(filePath).isFile() && path_1.default.extname(filePath) === "") {
|
|
||||||
const stats = fs_1.default.statSync(filePath);
|
|
||||||
return (stats.mode & 0o111) !== 0;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
// 使用7z解压文件
|
// 使用7z解压文件
|
||||||
const extractUsing7z = (filePath, extractTo) => {
|
const extractUsing7z = (filePath, extractTo) => {
|
||||||
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
||||||
|
|
@ -96,34 +88,6 @@ const extractRar = (filePath, extractTo) => {
|
||||||
console.error(`[ERROR] 解压失败: ${e}`);
|
console.error(`[ERROR] 解压失败: ${e}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* 解压缩文件
|
|
||||||
* @param inputDirectory 输入目录
|
|
||||||
* @param outputDirectory 输出目录
|
|
||||||
* @deprecated 存在编码问题,暂时不使用
|
|
||||||
*/
|
|
||||||
const extractZip = (filePath, extractTo) => {
|
|
||||||
console.log(`[INFO] 解压缩文件: ${filePath} 到目录: ${extractTo}`);
|
|
||||||
if (!fs_1.default.existsSync(extractTo)) {
|
|
||||||
fs_1.default.mkdirSync(extractTo, { recursive: true });
|
|
||||||
}
|
|
||||||
const zip = new adm_zip_1.default(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, outputDirectory) => {
|
const processDirectory = (inputDirectory, outputDirectory) => {
|
||||||
console.log(`[INFO] 开始处理目录: ${inputDirectory}`);
|
console.log(`[INFO] 开始处理目录: ${inputDirectory}`);
|
||||||
|
|
@ -133,7 +97,11 @@ const processDirectory = (inputDirectory, outputDirectory) => {
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
// 排除不需要的目录
|
// 排除不需要的目录
|
||||||
if (DISALLOWED_DIRECTORIES.has(item)) {
|
if (DISALLOWED_DIRECTORIES.has(item)) {
|
||||||
deletedItems.push(`目录: ${itemPath}`);
|
deletedDirectories.push({
|
||||||
|
type: "文件夹",
|
||||||
|
name: item,
|
||||||
|
originalPath: itemPath,
|
||||||
|
});
|
||||||
fs_1.default.rmSync(itemPath, { recursive: true, force: true });
|
fs_1.default.rmSync(itemPath, { recursive: true, force: true });
|
||||||
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
||||||
}
|
}
|
||||||
|
|
@ -151,13 +119,11 @@ const processDirectory = (inputDirectory, outputDirectory) => {
|
||||||
// 如果是压缩文件,解压后递归处理
|
// 如果是压缩文件,解压后递归处理
|
||||||
if (isCompressedFile(itemPath)) {
|
if (isCompressedFile(itemPath)) {
|
||||||
const relativePath = path_1.default.relative(inputDirectory, path_1.default.dirname(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)) +
|
const outputDirPath = path_1.default.join(outputDirectory, relativePath, path_1.default.basename(itemPath, path_1.default.extname(itemPath)) + "_extracted");
|
||||||
"_extracted");
|
|
||||||
if (!fs_1.default.existsSync(outputDirPath)) {
|
if (!fs_1.default.existsSync(outputDirPath)) {
|
||||||
fs_1.default.mkdirSync(outputDirPath, { recursive: true });
|
fs_1.default.mkdirSync(outputDirPath, { recursive: true });
|
||||||
}
|
}
|
||||||
if (itemPath.endsWith(".zip")) {
|
if (itemPath.endsWith(".zip")) {
|
||||||
// extractZip(itemPath, outputDirPath); // 存在编码问题,暂时不使用
|
|
||||||
extractUsing7z(itemPath, outputDirPath);
|
extractUsing7z(itemPath, outputDirPath);
|
||||||
}
|
}
|
||||||
else if (itemPath.endsWith(".7z")) {
|
else if (itemPath.endsWith(".7z")) {
|
||||||
|
|
@ -178,16 +144,18 @@ const processDirectory = (inputDirectory, outputDirectory) => {
|
||||||
fs_1.default.rmSync(outputDirPath, { recursive: true, force: true });
|
fs_1.default.rmSync(outputDirPath, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
else if (DISALLOWED_EXTENSIONS.has(ext) ||
|
else if (DISALLOWED_EXTENSIONS.has(ext) ||
|
||||||
DISALLOWED_KEYWORDS.has(item) ||
|
DISALLOWED_KEYWORDS.has(item)) {
|
||||||
isLinuxExecutable(itemPath)) {
|
deletedFiles.push({
|
||||||
|
type: "文件",
|
||||||
|
name: item,
|
||||||
|
originalPath: itemPath,
|
||||||
|
});
|
||||||
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
||||||
deletedItems.push(`文件: ${itemPath}`);
|
|
||||||
fs_1.default.rmSync(itemPath, { recursive: true, force: true }); // 删除不需要的文件
|
fs_1.default.rmSync(itemPath, { recursive: true, force: true }); // 删除不需要的文件
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 将文件复制到对应的输出目录
|
// 将文件复制到对应的输出目录
|
||||||
const outputFilePath = path_1.default.join(outputDirectory, item);
|
const outputFilePath = path_1.default.join(outputDirectory, item);
|
||||||
// 如果要复制的路径相同就跳过
|
|
||||||
if (itemPath === outputFilePath) {
|
if (itemPath === outputFilePath) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -197,13 +165,30 @@ const processDirectory = (inputDirectory, outputDirectory) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
// 写入统计文件
|
// 输出删除信息到控制台
|
||||||
const writeLog = (outputDirectory) => {
|
const printDeletedItems = () => {
|
||||||
const logFile = path_1.default.join(outputDirectory, "result_log.txt");
|
let result = "\n[INFO] 删除的文件和文件夹:";
|
||||||
fs_1.default.writeFileSync(logFile, "以下是被删除的目录和文件列表:\n");
|
// 输出文件夹
|
||||||
fs_1.default.appendFileSync(logFile, deletedItems.join("\n"));
|
if (deletedDirectories.length > 0) {
|
||||||
console.log(`[INFO] 已删除${deletedItems.length}个目录或文件。`);
|
result += "\n[INFO] 被删除的文件夹:";
|
||||||
console.log(`[INFO] 统计文件已生成: ${logFile}`);
|
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}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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] 统计信息已保存到 result.log 文件中。`);
|
||||||
};
|
};
|
||||||
// 主函数
|
// 主函数
|
||||||
const main = () => {
|
const main = () => {
|
||||||
|
|
@ -220,7 +205,9 @@ const main = () => {
|
||||||
fs_1.default.mkdirSync(outputDirectory, { recursive: true });
|
fs_1.default.mkdirSync(outputDirectory, { recursive: true });
|
||||||
console.log("[INFO] 开始处理压缩文件...");
|
console.log("[INFO] 开始处理压缩文件...");
|
||||||
processDirectory(inputDirectory, outputDirectory);
|
processDirectory(inputDirectory, outputDirectory);
|
||||||
writeLog(outputDirectory);
|
// 输出删除的文件和文件夹
|
||||||
|
const deletedItemsLog = printDeletedItems();
|
||||||
|
writeLogToFile(deletedItemsLog);
|
||||||
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
||||||
};
|
};
|
||||||
main();
|
main();
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
112
src/index.ts
112
src/index.ts
|
|
@ -26,11 +26,20 @@ const DISALLOWED_DIRECTORIES = new Set([
|
||||||
"__MACOSX",
|
"__MACOSX",
|
||||||
".vscode",
|
".vscode",
|
||||||
]);
|
]);
|
||||||
let deletedItems: string[] = [];
|
|
||||||
|
let deletedFiles: {
|
||||||
|
type: string;
|
||||||
|
name: string;
|
||||||
|
originalPath: string;
|
||||||
|
}[] = []; // 存储删除的文件信息
|
||||||
|
let deletedDirectories: {
|
||||||
|
type: string;
|
||||||
|
name: string;
|
||||||
|
originalPath: string;
|
||||||
|
}[] = []; // 存储删除的文件夹信息
|
||||||
|
|
||||||
// 清理文件名
|
// 清理文件名
|
||||||
const sanitizeFileName = (fileName: Buffer): string => {
|
const sanitizeFileName = (fileName: Buffer): string => {
|
||||||
// 对文件名进行标准化处理,解决编码问题
|
|
||||||
const decodedName = fileName.toString("utf8");
|
const decodedName = fileName.toString("utf8");
|
||||||
return decodedName;
|
return decodedName;
|
||||||
};
|
};
|
||||||
|
|
@ -61,15 +70,6 @@ const preTest = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 检查文件是否可能是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解压文件
|
// 使用7z解压文件
|
||||||
const extractUsing7z = (filePath: string, extractTo: string): void => {
|
const extractUsing7z = (filePath: string, extractTo: string): void => {
|
||||||
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
console.log(`[INFO] 使用 7z 解压文件: ${filePath} 到目录: ${extractTo}`);
|
||||||
|
|
@ -98,38 +98,6 @@ const extractRar = (filePath: string, extractTo: string): void => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* 解压缩文件
|
|
||||||
* @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 = (
|
const processDirectory = (
|
||||||
inputDirectory: string,
|
inputDirectory: string,
|
||||||
|
|
@ -143,7 +111,11 @@ const processDirectory = (
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
// 排除不需要的目录
|
// 排除不需要的目录
|
||||||
if (DISALLOWED_DIRECTORIES.has(item)) {
|
if (DISALLOWED_DIRECTORIES.has(item)) {
|
||||||
deletedItems.push(`目录: ${itemPath}`);
|
deletedDirectories.push({
|
||||||
|
type: "文件夹",
|
||||||
|
name: item,
|
||||||
|
originalPath: itemPath,
|
||||||
|
});
|
||||||
fs.rmSync(itemPath, { recursive: true, force: true });
|
fs.rmSync(itemPath, { recursive: true, force: true });
|
||||||
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
console.log(`[DELETE] 删除目录: ${itemPath}`);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -166,8 +138,7 @@ const processDirectory = (
|
||||||
const outputDirPath = path.join(
|
const outputDirPath = path.join(
|
||||||
outputDirectory,
|
outputDirectory,
|
||||||
relativePath,
|
relativePath,
|
||||||
path.basename(itemPath, path.extname(itemPath)) +
|
path.basename(itemPath, path.extname(itemPath)) + "_extracted"
|
||||||
"_extracted"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!fs.existsSync(outputDirPath)) {
|
if (!fs.existsSync(outputDirPath)) {
|
||||||
|
|
@ -175,7 +146,6 @@ const processDirectory = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemPath.endsWith(".zip")) {
|
if (itemPath.endsWith(".zip")) {
|
||||||
// extractZip(itemPath, outputDirPath); // 存在编码问题,暂时不使用
|
|
||||||
extractUsing7z(itemPath, outputDirPath);
|
extractUsing7z(itemPath, outputDirPath);
|
||||||
} else if (itemPath.endsWith(".7z")) {
|
} else if (itemPath.endsWith(".7z")) {
|
||||||
extractUsing7z(itemPath, outputDirPath);
|
extractUsing7z(itemPath, outputDirPath);
|
||||||
|
|
@ -201,16 +171,18 @@ const processDirectory = (
|
||||||
fs.rmSync(outputDirPath, { recursive: true, force: true });
|
fs.rmSync(outputDirPath, { recursive: true, force: true });
|
||||||
} else if (
|
} else if (
|
||||||
DISALLOWED_EXTENSIONS.has(ext) ||
|
DISALLOWED_EXTENSIONS.has(ext) ||
|
||||||
DISALLOWED_KEYWORDS.has(item) ||
|
DISALLOWED_KEYWORDS.has(item)
|
||||||
isLinuxExecutable(itemPath)
|
|
||||||
) {
|
) {
|
||||||
|
deletedFiles.push({
|
||||||
|
type: "文件",
|
||||||
|
name: item,
|
||||||
|
originalPath: itemPath,
|
||||||
|
});
|
||||||
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
console.log(`[DELETE] 删除文件: ${itemPath}`);
|
||||||
deletedItems.push(`文件: ${itemPath}`);
|
|
||||||
fs.rmSync(itemPath, { recursive: true, force: true }); // 删除不需要的文件
|
fs.rmSync(itemPath, { recursive: true, force: true }); // 删除不需要的文件
|
||||||
} else {
|
} else {
|
||||||
// 将文件复制到对应的输出目录
|
// 将文件复制到对应的输出目录
|
||||||
const outputFilePath = path.join(outputDirectory, item);
|
const outputFilePath = path.join(outputDirectory, item);
|
||||||
// 如果要复制的路径相同就跳过
|
|
||||||
if (itemPath === outputFilePath) {
|
if (itemPath === outputFilePath) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -223,13 +195,31 @@ const processDirectory = (
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 写入统计文件
|
// 输出删除信息到控制台
|
||||||
const writeLog = (outputDirectory: string): void => {
|
const printDeletedItems = () => {
|
||||||
const logFile = path.join(outputDirectory, "result_log.txt");
|
let result = "\n[INFO] 删除的文件和文件夹:";
|
||||||
fs.writeFileSync(logFile, "以下是被删除的目录和文件列表:\n");
|
// 输出文件夹
|
||||||
fs.appendFileSync(logFile, deletedItems.join("\n"));
|
if (deletedDirectories.length > 0) {
|
||||||
console.log(`[INFO] 已删除${deletedItems.length}个目录或文件。`);
|
result += "\n[INFO] 被删除的文件夹:";
|
||||||
console.log(`[INFO] 统计文件已生成: ${logFile}`);
|
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}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 写入日志到文件
|
||||||
|
const writeLogToFile = (log: string) => {
|
||||||
|
const logFilePath = path.join(process.cwd(), "out/result.log");
|
||||||
|
fs.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
|
||||||
|
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 主函数
|
// 主函数
|
||||||
|
|
@ -250,7 +240,11 @@ const main = (): void => {
|
||||||
|
|
||||||
console.log("[INFO] 开始处理压缩文件...");
|
console.log("[INFO] 开始处理压缩文件...");
|
||||||
processDirectory(inputDirectory, outputDirectory);
|
processDirectory(inputDirectory, outputDirectory);
|
||||||
writeLog(outputDirectory);
|
|
||||||
|
// 输出删除的文件和文件夹
|
||||||
|
const deletedItemsLog = printDeletedItems();
|
||||||
|
writeLogToFile(deletedItemsLog);
|
||||||
|
|
||||||
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
console.log('[INFO] 处理完成!结果保存在 "out" 目录中。');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue