import { ZipEntry } from 'node-stream-zip'; import {orderBy} from 'natural-orderby'; import { ReadStream } from 'fs'; import StreamZip from 'node-stream-zip'; export type ZipAsync = InstanceType; export async function readZip(path : string): Promise{ return new StreamZip.async({ file:path, storeEntries: true }); } export async function entriesByNaturalOrder(zip: ZipAsync){ const entries = await zip.entries(); const ret = orderBy(Object.values(entries),v=>v.name); return ret; } export async function createReadableStreamFromZip(zip:ZipAsync,entry: ZipEntry):Promise{ return await zip.stream(entry); } export async function readAllFromZip(zip:ZipAsync,entry: ZipEntry):Promise{ const stream = await createReadableStreamFromZip(zip,entry); const chunks:Uint8Array[] = []; return new Promise((resolve,reject)=>{ stream.on('data',(data)=>{chunks.push(data)}); stream.on('error', (err)=>reject(err)); stream.on('end',()=>resolve(Buffer.concat(chunks))); }); }