31 lines
No EOL
1.1 KiB
TypeScript
31 lines
No EOL
1.1 KiB
TypeScript
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<typeof StreamZip.async>;
|
|
export async function readZip(path : string): Promise<ZipAsync>{
|
|
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<NodeJS.ReadableStream>{
|
|
return await zip.stream(entry);
|
|
}
|
|
export async function readAllFromZip(zip:ZipAsync,entry: ZipEntry):Promise<Buffer>{
|
|
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)));
|
|
});
|
|
} |