Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/zip.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { createWriteStream, WriteStream, promises } from 'fs';
7
import { Readable } from 'stream';
8
import { createCancelablePromise, Sequencer } from '../common/async.js';
9
import { CancellationToken } from '../common/cancellation.js';
10
import * as path from '../common/path.js';
11
import { assertReturnsDefined } from '../common/types.js';
12
import { Promises } from './pfs.js';
13
import * as nls from '../../nls.js';
14
import type { Entry, ZipFile } from 'yauzl';
15
16
export const CorruptZipMessage: string = 'end of central directory record signature not found';
17
const CORRUPT_ZIP_PATTERN = new RegExp(CorruptZipMessage);
18
19
export interface IExtractOptions {
20
overwrite?: boolean;
21
22
/**
23
* Source path within the ZIP archive. Only the files contained in this
24
* path will be extracted.
25
*/
26
sourcePath?: string;
27
}
28
29
interface IOptions {
30
sourcePathRegex: RegExp;
31
}
32
33
export type ExtractErrorType = 'CorruptZip' | 'Incomplete';
34
35
export class ExtractError extends Error {
36
37
readonly type?: ExtractErrorType;
38
39
constructor(type: ExtractErrorType | undefined, cause: Error) {
40
let message = cause.message;
41
42
switch (type) {
43
case 'CorruptZip': message = `Corrupt ZIP: ${message}`; break;
44
}
45
46
super(message);
47
this.type = type;
48
this.cause = cause;
49
}
50
}
51
52
function modeFromEntry(entry: Entry) {
53
const attr = entry.externalFileAttributes >> 16 || 33188;
54
55
return [448 /* S_IRWXU */, 56 /* S_IRWXG */, 7 /* S_IRWXO */]
56
.map(mask => attr & mask)
57
.reduce((a, b) => a + b, attr & 61440 /* S_IFMT */);
58
}
59
60
function toExtractError(err: Error): ExtractError {
61
if (err instanceof ExtractError) {
62
return err;
63
}
64
65
let type: ExtractErrorType | undefined = undefined;
66
67
if (CORRUPT_ZIP_PATTERN.test(err.message)) {
68
type = 'CorruptZip';
69
}
70
71
return new ExtractError(type, err);
72
}
73
74
function extractEntry(stream: Readable, fileName: string, mode: number, targetPath: string, options: IOptions, token: CancellationToken): Promise<void> {
75
const dirName = path.dirname(fileName);
76
const targetDirName = path.join(targetPath, dirName);
77
if (!targetDirName.startsWith(targetPath)) {
78
return Promise.reject(new Error(nls.localize('invalid file', "Error extracting {0}. Invalid file.", fileName)));
79
}
80
const targetFileName = path.join(targetPath, fileName);
81
82
let istream: WriteStream;
83
84
token.onCancellationRequested(() => {
85
istream?.destroy();
86
});
87
88
return Promise.resolve(promises.mkdir(targetDirName, { recursive: true })).then(() => new Promise<void>((c, e) => {
89
if (token.isCancellationRequested) {
90
return;
91
}
92
93
try {
94
istream = createWriteStream(targetFileName, { mode });
95
istream.once('close', () => c());
96
istream.once('error', e);
97
stream.once('error', e);
98
stream.pipe(istream);
99
} catch (error) {
100
e(error);
101
}
102
}));
103
}
104
105
function extractZip(zipfile: ZipFile, targetPath: string, options: IOptions, token: CancellationToken): Promise<void> {
106
let last = createCancelablePromise<void>(() => Promise.resolve());
107
let extractedEntriesCount = 0;
108
109
const listener = token.onCancellationRequested(() => {
110
last.cancel();
111
zipfile.close();
112
});
113
114
return new Promise<void>((c, e) => {
115
const throttler = new Sequencer();
116
117
const readNextEntry = (token: CancellationToken) => {
118
if (token.isCancellationRequested) {
119
return;
120
}
121
122
extractedEntriesCount++;
123
zipfile.readEntry();
124
};
125
126
zipfile.once('error', e);
127
zipfile.once('close', () => last.then(() => {
128
if (token.isCancellationRequested || zipfile.entryCount === extractedEntriesCount) {
129
c();
130
} else {
131
e(new ExtractError('Incomplete', new Error(nls.localize('incompleteExtract', "Incomplete. Found {0} of {1} entries", extractedEntriesCount, zipfile.entryCount))));
132
}
133
}, e));
134
zipfile.readEntry();
135
zipfile.on('entry', (entry: Entry) => {
136
137
if (token.isCancellationRequested) {
138
return;
139
}
140
141
if (!options.sourcePathRegex.test(entry.fileName)) {
142
readNextEntry(token);
143
return;
144
}
145
146
const fileName = entry.fileName.replace(options.sourcePathRegex, '');
147
148
// directory file names end with '/'
149
if (/\/$/.test(fileName)) {
150
const targetFileName = path.join(targetPath, fileName);
151
last = createCancelablePromise(token => promises.mkdir(targetFileName, { recursive: true }).then(() => readNextEntry(token)).then(undefined, e));
152
return;
153
}
154
155
const stream = openZipStream(zipfile, entry);
156
const mode = modeFromEntry(entry);
157
158
last = createCancelablePromise(token => throttler.queue(() => stream.then(stream => extractEntry(stream, fileName, mode, targetPath, options, token).then(() => readNextEntry(token)))).then(null, e));
159
});
160
}).finally(() => listener.dispose());
161
}
162
163
async function openZip(zipFile: string, lazy: boolean = false): Promise<ZipFile> {
164
const { open } = await import('yauzl');
165
166
return new Promise<ZipFile>((resolve, reject) => {
167
open(zipFile, lazy ? { lazyEntries: true } : undefined!, (error: Error | null, zipfile?: ZipFile) => {
168
if (error) {
169
reject(toExtractError(error));
170
} else {
171
resolve(assertReturnsDefined(zipfile));
172
}
173
});
174
});
175
}
176
177
function openZipStream(zipFile: ZipFile, entry: Entry): Promise<Readable> {
178
return new Promise<Readable>((resolve, reject) => {
179
zipFile.openReadStream(entry, (error: Error | null, stream?: Readable) => {
180
if (error) {
181
reject(toExtractError(error));
182
} else {
183
resolve(assertReturnsDefined(stream));
184
}
185
});
186
});
187
}
188
189
export interface IFile {
190
path: string;
191
contents?: Buffer | string;
192
localPath?: string;
193
}
194
195
export async function zip(zipPath: string, files: IFile[]): Promise<string> {
196
const { ZipFile } = await import('yazl');
197
198
return new Promise<string>((c, e) => {
199
const zip = new ZipFile();
200
files.forEach(f => {
201
if (f.contents) {
202
zip.addBuffer(typeof f.contents === 'string' ? Buffer.from(f.contents, 'utf8') : f.contents, f.path);
203
} else if (f.localPath) {
204
zip.addFile(f.localPath, f.path);
205
}
206
});
207
zip.end();
208
209
const zipStream = createWriteStream(zipPath);
210
zip.outputStream.pipe(zipStream);
211
212
zip.outputStream.once('error', e);
213
zipStream.once('error', e);
214
zipStream.once('finish', () => c(zipPath));
215
});
216
}
217
218
export function extract(zipPath: string, targetPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> {
219
const sourcePathRegex = new RegExp(options.sourcePath ? `^${options.sourcePath}` : '');
220
221
let promise = openZip(zipPath, true);
222
223
if (options.overwrite) {
224
promise = promise.then(zipfile => Promises.rm(targetPath).then(() => zipfile));
225
}
226
227
return promise.then(zipfile => extractZip(zipfile, targetPath, { sourcePathRegex }, token));
228
}
229
230
function read(zipPath: string, filePath: string): Promise<Readable> {
231
return openZip(zipPath).then(zipfile => {
232
return new Promise<Readable>((c, e) => {
233
zipfile.on('entry', (entry: Entry) => {
234
if (entry.fileName === filePath) {
235
openZipStream(zipfile, entry).then(stream => c(stream), err => e(err));
236
}
237
});
238
239
zipfile.once('close', () => e(new Error(nls.localize('notFound', "{0} not found inside zip.", filePath))));
240
});
241
});
242
}
243
244
export function buffer(zipPath: string, filePath: string): Promise<Buffer> {
245
return read(zipPath, filePath).then(stream => {
246
return new Promise<Buffer>((c, e) => {
247
const buffers: Buffer[] = [];
248
stream.once('error', e);
249
stream.on('data', (b: Buffer) => buffers.push(b));
250
stream.on('end', () => c(Buffer.concat(buffers)));
251
});
252
});
253
}
254
255