mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-21 16:00:59 -05:00
Added a few utility methods to work with zip
(In test environment)
This commit is contained in:
parent
344239c2fc
commit
86dafdd03f
1 changed files with 31 additions and 0 deletions
31
tests/utils/zip-utils.ts
Normal file
31
tests/utils/zip-utils.ts
Normal file
|
@ -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<Buffer> {
|
||||
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<Buffer> {
|
||||
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);
|
||||
}
|
Loading…
Reference in a new issue