Path: blob/trunk/javascript/selenium-webdriver/io/zip.js
4500 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const jszip = require('jszip')20const path = require('node:path')21const fs = require('fs/promises')22const io = require('./index')23const { InvalidArgumentError } = require('../lib/error')2425/**26* Manages a zip archive.27*/28class Zip {29constructor() {30/** @private @const */31this.z_ = new jszip()3233/** @private @const {!Set<!Promise<?>>} */34this.pendingAdds_ = new Set()35}3637/**38* Adds a file to this zip.39*40* @param {string} filePath path to the file to add.41* @param {string=} zipPath path to the file in the zip archive, defaults42* to the basename of `filePath`.43* @return {!Promise<?>} a promise that will resolve when added.44*/45addFile(filePath, zipPath = path.basename(filePath)) {46let add = Promise.all([io.read(filePath), fs.stat(filePath)]).then(([buffer, stats]) =>47this.z_.file(/** @type {string} */ (zipPath.replace(/\\/g, '/')), buffer, {48date: stats.mtime, // preserve file's "last modified" value49}),50)51this.pendingAdds_.add(add)52return add.then(53() => this.pendingAdds_.delete(add),54(e) => {55this.pendingAdds_.delete(add)56throw e57},58)59}6061/**62* Recursively adds a directory and all of its contents to this archive.63*64* @param {string} dirPath path to the directory to add.65* @param {string=} zipPath path to the folder in the archive to add the66* directory contents to. Defaults to the root folder.67* @return {!Promise<?>} returns a promise that will resolve when68* the operation is complete.69*/70addDir(dirPath, zipPath = '') {71return io.walkDir(dirPath).then((entries) => {72let archive = this.z_73if (zipPath) {74archive = archive.folder(zipPath)75}7677let files = []78entries.forEach((spec) => {79if (spec.dir) {80archive.folder(spec.path)81} else {82files.push(this.addFile(path.join(dirPath, spec.path), path.join(zipPath, spec.path)))83}84})8586return Promise.all(files)87})88}8990/**91* @param {string} path File path to test for within the archive.92* @return {boolean} Whether this zip archive contains an entry with the given93* path.94*/95has(path) {96return this.z_.file(path) !== null97}9899/**100* Returns the contents of the file in this zip archive with the given `path`.101* The returned promise will be rejected with an {@link InvalidArgumentError}102* if either `path` does not exist within the archive, or if `path` refers103* to a directory.104*105* @param {string} path the path to the file whose contents to return.106* @return {!Promise<!Buffer>} a promise that will be resolved with the file's107* contents as a buffer.108*/109getFile(path) {110let file = this.z_.file(path)111if (!file) {112return Promise.reject(new InvalidArgumentError(`No such file in zip archive: ${path}`))113}114115if (file.dir) {116return Promise.reject(new InvalidArgumentError(`The requested file is a directory: ${path}`))117}118119return Promise.resolve(file.async('nodebuffer'))120}121122/**123* Returns the compressed data for this archive in a buffer. _This method will124* not wait for any outstanding {@link #addFile add}125* {@link #addDir operations} before encoding the archive._126*127* @param {string} compression The desired compression.128* Must be `STORE` (the default) or `DEFLATE`.129* @return {!Promise<!Buffer>} a promise that will resolve with this archive130* as a buffer.131*/132toBuffer(compression = 'STORE') {133if (compression !== 'STORE' && compression !== 'DEFLATE') {134return Promise.reject(new InvalidArgumentError(`compression must be one of {STORE, DEFLATE}, got ${compression}`))135}136return Promise.resolve(this.z_.generateAsync({ compression, type: 'nodebuffer' }))137}138}139140/**141* Asynchronously opens a zip archive.142*143* @param {string} path to the zip archive to load.144* @return {!Promise<!Zip>} a promise that will resolve with the opened145* archive.146*/147function load(path) {148return io.read(path).then((data) => {149let zip = new Zip()150return zip.z_.loadAsync(data).then(() => zip)151})152}153154/**155* Asynchronously unzips an archive file.156*157* @param {string} src path to the source file to unzip.158* @param {string} dst path to the destination directory.159* @return {!Promise<string>} a promise that will resolve with `dst` once the160* archive has been unzipped.161*/162function unzip(src, dst) {163return load(src).then((zip) => {164const promisedDirs = new Map()165const promises = []166167zip.z_.forEach((relPath, file) => {168let p169if (file.dir) {170p = createDir(relPath)171} else {172let dirname = path.dirname(relPath)173if (dirname === '.') {174p = writeFile(relPath, file)175} else {176p = createDir(dirname).then(() => writeFile(relPath, file))177}178}179promises.push(p)180})181182return Promise.all(promises).then(() => dst)183184function createDir(dir) {185let p = promisedDirs.get(dir)186if (!p) {187p = io.mkdirp(path.join(dst, dir))188promisedDirs.set(dir, p)189}190return p191}192193function writeFile(relPath, file) {194return file.async('nodebuffer').then((buffer) => io.write(path.join(dst, relPath), buffer))195}196})197}198199// PUBLIC API200module.exports = { Zip, load, unzip }201202203