Skip to content

Instantly share code, notes, and snippets.

@noyobo
Last active June 1, 2022 03:20
Show Gist options
  • Save noyobo/b0ed1722a59302cfc99c2ec93be6807c to your computer and use it in GitHub Desktop.
Save noyobo/b0ed1722a59302cfc99c2ec93be6807c to your computer and use it in GitHub Desktop.
decompress tgz in nodejs
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));
}
})
@just-paja
Copy link

just-paja commented Sep 23, 2020

TypeError: tar.Extract is not a function

The latest tar implementation from npm replaces Extract with x and path with C.

fs.createReadStream(path.resolve('./ice-2.0.50.tgz'))
  .on('error', console.log)
  .pipe(zlib.Unzip())
  .pipe(tar.x({
    C: './aaaa',
    strip: 1
  }))

@sforcier
Copy link

sforcier commented May 5, 2021

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
});

@antongolub
Copy link

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