Last active
June 1, 2022 03:20
-
-
Save noyobo/b0ed1722a59302cfc99c2ec93be6807c to your computer and use it in GitHub Desktop.
decompress tgz in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var zlib = require('zlib'); | |
var path = require('path'); | |
var tar = require('tar'); | |
var mkdirp = require('mkdirp'); | |
// 全解压 | |
fs.createReadStream(path.resolve('./ice-2.0.50.tgz')) | |
.on('error', console.log) | |
.pipe(zlib.Unzip()) | |
.pipe(tar.Extract({ | |
path: './aaaa', | |
strip: 1 | |
})) | |
// 选择文件 | |
fs.createReadStream(path.resolve('./ice-2.0.50.tgz')) | |
.on('error', console.log) | |
.pipe(zlib.Unzip()) | |
.pipe(tar.Parse()) | |
.on('entry', function(entry) { | |
if (/\.scss$/.test(entry.path)) { | |
var filepath = path.join(__dirname, entry.path); | |
mkdirp.sync(path.dirname(filepath)) | |
entry.pipe(fs.createWriteStream(filepath)); | |
} | |
}) |
I know this is an old gist, but this came up in my search so I figured I'd post an updated approach. The following are not replacing part of the original gist, they are the full gists.
Unzip synchronously
tar.x({ C: `./aaaa`, file: `./ice-2.0.50.tgz`, sync: true });
Unzip with callback
tar.x({ C: `./aaaa`, file: `./ice-2.0.50.tgz` }, undefined, (err) => {
// Error handling code here
});
Unzip asynchronously
await tar.x({ C: `./aaaa`, file: `./ice-2.0.50.tgz` })
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeError: tar.Extract is not a function
The latest tar implementation from npm replaces
Extract
withx
andpath
withC
.