加载远程压缩文件

const path = require("path");
const mkdirp = require("mkdirp");
const semver = require("semver");
const zlib = require("zlib");
const tar = require("tar");
const fse = require("fs-extra");
const constant_1 = require("@appworks/constant");
const axios_1 = require("axios");
const urlJoin = require("url-join");

function getAndExtractTarball(destDir, tarball, progressFunc = (state) => { }, formatFilename = (filename) => {
    // 为了兼容
    if (filename === '_package.json') {
        return filename.replace(/^_/, '');
    }
    else {
        return filename.replace(/^_/, '.');
    }
}) {
    return new Promise((resolve, reject) => {
        const allFiles = [];
        const allWriteStream = [];
        const dirCollector = [];
        axios_1.default({
            url: tarball,
            timeout: 10000,
            responseType: 'stream',
            onDownloadProgress: (progressEvent) => {
                progressFunc(progressEvent);
            },
        }).then((response) => {
            const totalLength = Number(response.headers['content-length']);
            let downloadLength = 0;
            response.data
                // @ts-ignore
                .on('data', (chunk) => {
                downloadLength += chunk.length;
                progressFunc({
                    percent: (downloadLength - 50) / totalLength,
                });
            })
                // @ts-ignore
                .pipe(zlib.Unzip())
                // @ts-ignore
                .pipe(new tar.Parse())
                .on('entry', (entry) => {
                if (entry.type === 'Directory') {
                    entry.resume();
                    return;
                }
                const realPath = entry.path.replace(/^package\//, '');
                let filename = path.basename(realPath);
                filename = formatFilename(filename);
                const destPath = path.join(destDir, path.dirname(realPath), filename);
                const dirToBeCreate = path.dirname(destPath);
                if (!dirCollector.includes(dirToBeCreate)) {
                    dirCollector.push(dirToBeCreate);
                    mkdirp.sync(dirToBeCreate);
                }
                allFiles.push(destPath);
                allWriteStream.push(new Promise((streamResolve) => {
                    entry
                        .pipe(fse.createWriteStream(destPath))
                        .on('finish', () => streamResolve())
                        .on('close', () => streamResolve()); // resolve when file is empty in node v8
                }));
            })
                .on('end', () => {
                if (progressFunc) {
                    progressFunc({
                        percent: 1,
                    });
                }
                Promise.all(allWriteStream)
                    .then(() => resolve(allFiles))
                    .catch(reject);
            });
        });
    });
}

getAndExtractTarball()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值