Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/wasi-js/src/unzip.ts
1067 views
1
import { dirname, join } from "path";
2
import { unzipSync } from "fflate";
3
4
export type UnzipOptions = {
5
data: ArrayBuffer | Uint8Array;
6
fs: {
7
mkdirSync: Function;
8
statSync: Function;
9
writeFileSync: Function;
10
chmodSync: Function;
11
};
12
directory: string;
13
};
14
15
export default function unzip({ data, fs, directory }: UnzipOptions): void {
16
// const t0 = new Date().valueOf();
17
if (data instanceof ArrayBuffer) {
18
data = new Uint8Array(data);
19
}
20
if (!(data instanceof Uint8Array)) {
21
throw Error("impossible"); // was converted above. this is for typescript.
22
}
23
const z = unzipSync(data);
24
for (const [relativePath, content] of Object.entries(z)) {
25
const outputFilename = join(directory, relativePath);
26
fs.mkdirSync(dirname(outputFilename), { recursive: true });
27
if(outputFilename.endsWith('/')) {
28
// it is a directory, not a file.
29
continue;
30
}
31
fs.writeFileSync(outputFilename, content);
32
fs.chmodSync(outputFilename, 0o777);
33
}
34
// console.log(
35
// `extract ${data.length / 10 ** 6} MB in ${new Date().valueOf() - t0}ms`
36
// );
37
}
38
39