MCPcopy Create free account
hub / github.com/adobe/react-spectrum / zip

Function zip

packages/dev/s2-docs/src/zip.tsx:40–89  ·  view source on GitHub ↗
(files: {[name: string]: string})

Source from the content-addressed store, hash-verified

38}
39
40export function zip(files: {[name: string]: string}): Blob {
41 let records: Uint8Array[] = [];
42 let cdrs: Uint8Array[] = [];
43 let te = new TextEncoder();
44
45 let offset = 0;
46 let cdSz = 0;
47
48 for (let name in files) {
49 let fh = new Uint8Array(30 + name.length);
50 let fname = te.encode(name);
51 let data = te.encode(files[name]);
52 let chksum = crc32(data);
53
54 putUint32s(fh, 0, 0x04034b50);
55 putUint32s(fh, 14, chksum, data.length, data.length);
56 putUint16s(fh, 26, name.length);
57
58 fh.set(fname, 30);
59
60 records.push(fh);
61 records.push(data);
62
63 // Create CD records pending flush at the end...
64 let cdr = new Uint8Array(46 + name.length);
65
66 putUint32s(cdr, 0, 0x02014b50);
67 putUint32s(cdr, 16, chksum, data.length, data.length);
68 putUint16s(cdr, 28, name.length);
69 putUint32s(cdr, 42, offset);
70
71 cdr.set(fname, 46);
72 cdrs.push(cdr);
73
74 cdSz += cdr.length;
75 offset += fh.length + data.length;
76 }
77
78 // Push all accrued CD records..
79 records.push(...cdrs);
80
81 // Add EOCD record...
82 let eocd = new Uint8Array(22);
83 putUint32s(eocd, 0, 0x06054b50);
84 putUint16s(eocd, 8, cdrs.length, cdrs.length);
85 putUint32s(eocd, 12, cdSz, offset);
86 records.push(eocd);
87
88 return new Blob(records as BlobPart[], {type: 'application/zip'});
89}

Callers 1

CodePlatterFunction · 0.90

Calls 5

crc32Function · 0.85
putUint32sFunction · 0.85
putUint16sFunction · 0.85
pushMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected