zip.ts 3.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import nls = require('vs/nls');
import * as path from 'path';
import { createWriteStream } from 'fs';
import { Readable } from 'stream';
import { nfcall, ninvoke } from 'vs/base/common/async';
import { mkdirp, rimraf } from 'vs/base/node/pfs';
import { Promise, TPromise } from 'vs/base/common/winjs.base';
import { open as openZip, Entry, ZipFile } from 'yauzl';

export interface IExtractOptions {
	overwrite?: boolean;

	/**
	 * Source path within the ZIP archive. Only the files contained in this
	 * path will be extracted.
	 */
	sourcePath?: string;
}

interface IOptions {
	sourcePathRegex: RegExp;
}

function modeFromEntry(entry: Entry) {
B
Benjamin Pasero 已提交
30
	let attr = entry.externalFileAttributes >> 16 || 33188;
E
Erich Gamma 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

	return [448 /* S_IRWXU */, 56 /* S_IRWXG */, 7 /* S_IRWXO */]
		.map(mask => attr & mask)
		.reduce((a, b) => a + b, attr & 61440 /* S_IFMT */);
}

function extractEntry(zipfile: ZipFile, entry: Entry, targetPath: string, options: IOptions): Promise {
	const fileName = entry.fileName.replace(options.sourcePathRegex, '');
	const dirName = path.dirname(fileName);
	const targetDirName = path.join(targetPath, dirName);
	const targetFileName = path.join(targetPath, fileName);
	const mode = modeFromEntry(entry);

	return ninvoke(zipfile, zipfile.openReadStream, entry)
		.then(ostream => mkdirp(targetDirName)
			.then(() => new Promise((c, e) => {
B
Benjamin Pasero 已提交
47
				let istream = createWriteStream(targetFileName, { mode });
E
Erich Gamma 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
				istream.once('finish', () => c(null));
				istream.once('error', e);
				ostream.once('error', e);
				ostream.pipe(istream);
			})));
}

function extractZip(zipfile: ZipFile, targetPath: string, options: IOptions): Promise {
	return new Promise((c, e) => {
		const promises: Promise[] = [];

		zipfile.once('error', e);
		zipfile.on('entry', (entry: Entry) => {
			if (!options.sourcePathRegex.test(entry.fileName)) {
				return;
			}

			promises.push(extractEntry(zipfile, entry, targetPath, options));
		});
		zipfile.once('close', () => Promise.join(promises).done(c, e));
	});
}

export function extract(zipPath: string, targetPath: string, options: IExtractOptions): Promise {
	const sourcePathRegex = new RegExp(options.sourcePath ? `^${ options.sourcePath }` : '');

	let promise = nfcall<ZipFile>(openZip, zipPath);

	if (options.overwrite) {
		promise = promise.then(zipfile => { rimraf(targetPath); return zipfile; });
	}

	return promise.then(zipfile => extractZip(zipfile, targetPath, { sourcePathRegex }));
}

function read(zipPath: string, filePath: string): TPromise<Readable> {
	return nfcall(openZip, zipPath).then((zipfile: ZipFile) => {
		return new TPromise<Readable>((c, e) => {
			zipfile.on('entry', (entry: Entry) => {
				if (entry.fileName === filePath) {
					ninvoke<Readable>(zipfile, zipfile.openReadStream, entry).done(stream => c(stream), err => e(err));
				}
			});

			zipfile.once('close', () => e(new Error(nls.localize('notFound', "{0} not found inside zip.", filePath))));
		});
	});
}

export function buffer(zipPath: string, filePath: string): TPromise<Buffer> {
	return read(zipPath, filePath).then(stream => {
		return new TPromise<Buffer>((c, e) => {
			const buffers = [];
			stream.once('error', e);
			stream.on('data', b => buffers.push(b));
			stream.on('end', () => c(Buffer.concat(buffers)));
		});
	});
}