From 86dafdd03f79d66ce8adcbf3db0d9b1b528fb4d6 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Thu, 23 Mar 2023 12:30:14 +0000 Subject: [PATCH] Added a few utility methods to work with zip (In test environment) --- tests/utils/zip-utils.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/utils/zip-utils.ts diff --git a/tests/utils/zip-utils.ts b/tests/utils/zip-utils.ts new file mode 100644 index 0000000..417214c --- /dev/null +++ b/tests/utils/zip-utils.ts @@ -0,0 +1,31 @@ +import { basename, resolve as resolvePath } from "node:path"; +import { ZipFile } from "yazl"; + +export async function zipFile(path: string | string[], zipPath?: string): Promise { + const realPath = typeof path === "string" ? path : resolvePath(...path); + zipPath ||= basename(realPath); + + const zip = new ZipFile(); + zip.addFile(realPath, zipPath); + zip.end(); + + const chunks = [] as Buffer[]; + for await (const chunk of zip.outputStream) { + chunks.push(Buffer.from(chunk)); + } + + return Buffer.concat(chunks); +} + +export async function zipContent(content: string | Buffer, zipPath: string): Promise { + const zip = new ZipFile(); + zip.addBuffer(Buffer.from(content), zipPath); + zip.end(); + + const chunks = [] as Buffer[]; + for await (const chunk of zip.outputStream) { + chunks.push(Buffer.from(chunk)); + } + + return Buffer.concat(chunks); +}