|
1 | 1 | import { tmpdir } from 'node:os'; |
2 | | -import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { deflateRawSync } from 'node:zlib'; |
| 3 | +import { constants, mkdir, mkdtemp, open, rm, writeFile } from 'node:fs/promises'; |
3 | 4 | import { join } from 'node:path'; |
| 5 | +import { Queue } from '../util/Queue.mts'; |
4 | 6 | import 'lean-test'; |
5 | 7 |
|
6 | 8 | export type FilesDefinition = { [k in string]: string | FilesDefinition }; |
@@ -37,3 +39,92 @@ export async function makeFileStructure(dir: string, structure: FilesDefinition) |
37 | 39 | } |
38 | 40 | } |
39 | 41 | } |
| 42 | + |
| 43 | +export async function writeTestZip(path: string, structure: FilesDefinition) { |
| 44 | + // this writes enough data for readZip to understand the contents |
| 45 | + // in particular, it does not write the modification time, permission flags, or crc32 |
| 46 | + |
| 47 | + const cdItems: { |
| 48 | + filename: Buffer; |
| 49 | + pos: number; |
| 50 | + compression: number; |
| 51 | + compressedSize: number; |
| 52 | + uncompressedSize: number; |
| 53 | + }[] = []; |
| 54 | + const handle = await open(path, constants.O_CREAT | constants.O_WRONLY); |
| 55 | + try { |
| 56 | + const stream = handle.createWriteStream(); |
| 57 | + let pos = 0; |
| 58 | + |
| 59 | + const queue = new Queue({ path: [] as string[], structure }); |
| 60 | + for (const { path, structure } of queue) { |
| 61 | + for (const [name, content] of Object.entries(structure)) { |
| 62 | + const itemPath = [...path, name]; |
| 63 | + let compressed = VOID; |
| 64 | + let uncompressedSize = 0; |
| 65 | + let compression = 0; |
| 66 | + let filename: Buffer; |
| 67 | + if (typeof content === 'string') { |
| 68 | + filename = Buffer.from(itemPath.join('/'), 'utf-8'); |
| 69 | + const uncompressed = Buffer.from(content, 'utf-8'); |
| 70 | + uncompressedSize = uncompressed.byteLength; |
| 71 | + if (uncompressedSize < 20) { |
| 72 | + compressed = uncompressed; |
| 73 | + } else { |
| 74 | + compressed = deflateRawSync(uncompressed); |
| 75 | + compression = 8; |
| 76 | + } |
| 77 | + } else { |
| 78 | + queue.push({ path: itemPath, structure: content }); |
| 79 | + filename = Buffer.from(itemPath.join('/') + '/', 'utf-8'); |
| 80 | + } |
| 81 | + cdItems.push({ |
| 82 | + filename, |
| 83 | + pos, |
| 84 | + compression, |
| 85 | + compressedSize: compressed.byteLength, |
| 86 | + uncompressedSize, |
| 87 | + }); |
| 88 | + const localFileHeader = Buffer.alloc(30); |
| 89 | + localFileHeader.writeUint32BE(0x504b0304, 0); |
| 90 | + localFileHeader.writeUint16LE(20, 4); |
| 91 | + localFileHeader[6] = 0x40; |
| 92 | + localFileHeader.writeUint16LE(compression, 8); |
| 93 | + localFileHeader.writeUint32LE(compressed.byteLength, 18); |
| 94 | + localFileHeader.writeUint32LE(uncompressedSize, 22); |
| 95 | + localFileHeader.writeUint16LE(filename.byteLength, 26); |
| 96 | + stream.write(localFileHeader); |
| 97 | + stream.write(filename); |
| 98 | + stream.write(compressed); |
| 99 | + pos += localFileHeader.byteLength + filename.byteLength + compressed.byteLength; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const cdPos = pos; |
| 104 | + for (const item of cdItems) { |
| 105 | + const cdFileHeader = Buffer.alloc(46); |
| 106 | + cdFileHeader.writeUint32BE(0x504b0102, 0); |
| 107 | + cdFileHeader.writeUint16LE(20, 6); |
| 108 | + cdFileHeader[8] = 0x40; |
| 109 | + cdFileHeader.writeUint16LE(item.compression, 10); |
| 110 | + cdFileHeader.writeUint32LE(item.compressedSize, 20); |
| 111 | + cdFileHeader.writeUint32LE(item.uncompressedSize, 24); |
| 112 | + cdFileHeader.writeUint16LE(item.filename.byteLength, 28); |
| 113 | + cdFileHeader.writeUint32LE(item.pos, 42); |
| 114 | + stream.write(cdFileHeader); |
| 115 | + stream.write(item.filename); |
| 116 | + pos += cdFileHeader.byteLength + item.filename.byteLength; |
| 117 | + } |
| 118 | + const eocd = Buffer.alloc(22); |
| 119 | + eocd.writeUint32BE(0x504b0506, 0); |
| 120 | + eocd.writeUint16LE(cdItems.length, 8); |
| 121 | + eocd.writeUint16LE(cdItems.length, 10); |
| 122 | + eocd.writeUint32LE(pos - cdPos, 12); |
| 123 | + eocd.writeUint32LE(cdPos, 16); |
| 124 | + stream.write(eocd); |
| 125 | + } finally { |
| 126 | + await handle.close(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +const VOID = Buffer.alloc(0); |
0 commit comments