|
| 1 | +import { Entry as ZipEntry, open, Options as ZipOptions, ZipFile } from "yauzl"; |
| 2 | +import { Readable } from "stream"; |
| 3 | +import { dirname, join } from "path"; |
| 4 | +import { WriteStream } from "fs"; |
| 5 | +import { createWriteStream, ensureDir } from "fs-extra"; |
| 6 | + |
| 7 | +// We can't use promisify because it picks up the wrong overload. |
| 8 | +function openZip(path: string, options: ZipOptions = {}): Promise<ZipFile> { |
| 9 | + return new Promise((resolve, reject) => { |
| 10 | + open(path, options, (err, zipFile) => { |
| 11 | + if (err) { |
| 12 | + reject(err); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + resolve(zipFile); |
| 17 | + }); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function readZipEntries(zipFile: ZipFile): Promise<ZipEntry[]> { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const files: ZipEntry[] = []; |
| 24 | + |
| 25 | + zipFile.readEntry(); |
| 26 | + zipFile.on("entry", (entry: ZipEntry) => { |
| 27 | + if (/\/$/.test(entry.fileName)) { |
| 28 | + // Directory file names end with '/' |
| 29 | + // We don't need to do anything for directories. |
| 30 | + } else { |
| 31 | + files.push(entry); |
| 32 | + } |
| 33 | + |
| 34 | + zipFile.readEntry(); |
| 35 | + }); |
| 36 | + |
| 37 | + zipFile.on("end", () => { |
| 38 | + resolve(files); |
| 39 | + }); |
| 40 | + |
| 41 | + zipFile.on("error", (err) => { |
| 42 | + reject(err); |
| 43 | + }); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function openZipReadStream( |
| 48 | + zipFile: ZipFile, |
| 49 | + entry: ZipEntry, |
| 50 | +): Promise<Readable> { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + zipFile.openReadStream(entry, (err, readStream) => { |
| 53 | + if (err) { |
| 54 | + reject(err); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + resolve(readStream); |
| 59 | + }); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +async function copyStream( |
| 64 | + readable: Readable, |
| 65 | + writeStream: WriteStream, |
| 66 | +): Promise<void> { |
| 67 | + return new Promise((resolve, reject) => { |
| 68 | + readable.on("error", (err) => { |
| 69 | + reject(err); |
| 70 | + }); |
| 71 | + readable.on("end", () => { |
| 72 | + resolve(); |
| 73 | + }); |
| 74 | + |
| 75 | + readable.pipe(writeStream); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +export async function unzipToDirectory( |
| 80 | + archivePath: string, |
| 81 | + destinationPath: string, |
| 82 | +): Promise<void> { |
| 83 | + const zipFile = await openZip(archivePath, { |
| 84 | + autoClose: false, |
| 85 | + strictFileNames: true, |
| 86 | + lazyEntries: true, |
| 87 | + }); |
| 88 | + |
| 89 | + try { |
| 90 | + const entries = await readZipEntries(zipFile); |
| 91 | + |
| 92 | + for (const entry of entries) { |
| 93 | + const path = join(destinationPath, entry.fileName); |
| 94 | + |
| 95 | + if (/\/$/.test(entry.fileName)) { |
| 96 | + // Directory file names end with '/' |
| 97 | + |
| 98 | + await ensureDir(path); |
| 99 | + } else { |
| 100 | + // Ensure the directory exists |
| 101 | + await ensureDir(dirname(path)); |
| 102 | + |
| 103 | + const readable = await openZipReadStream(zipFile, entry); |
| 104 | + |
| 105 | + let mode: number | undefined = entry.externalFileAttributes >>> 16; |
| 106 | + if (mode <= 0) { |
| 107 | + mode = undefined; |
| 108 | + } |
| 109 | + |
| 110 | + const writeStream = createWriteStream(path, { |
| 111 | + autoClose: true, |
| 112 | + mode, |
| 113 | + }); |
| 114 | + |
| 115 | + await copyStream(readable, writeStream); |
| 116 | + } |
| 117 | + } |
| 118 | + } finally { |
| 119 | + zipFile.close(); |
| 120 | + } |
| 121 | +} |
0 commit comments