32 lines
869 B
TypeScript
32 lines
869 B
TypeScript
import archiver from "archiver";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
export function createZipFile(
|
|
sourceDir: string,
|
|
zipPath: string,
|
|
englishName: string | null
|
|
): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
const output = fs.createWriteStream(zipPath);
|
|
const archive = archiver("zip", { zlib: { level: 9 } });
|
|
|
|
output.on("close", () => resolve());
|
|
archive.on("error", reject);
|
|
archive.pipe(output);
|
|
|
|
if (englishName) {
|
|
const files = fs.readdirSync(sourceDir);
|
|
const targetFiles = files.filter(
|
|
(f) => f.startsWith(englishName) && (f.endsWith(".in") || f.endsWith(".out"))
|
|
);
|
|
for (const file of targetFiles) {
|
|
archive.file(path.join(sourceDir, file), { name: file });
|
|
}
|
|
} else {
|
|
archive.directory(sourceDir, false);
|
|
}
|
|
archive.finalize();
|
|
});
|
|
}
|