I want to:
- download a large zip file (
curl
), - if #1 is successful, unzip the file to a target directory (
unzip
) - if #2 is successful, delete the file (
rm
)
I am writing a simple nodejs
script to do the above, and am using the child_process.execSync
for this.
const execSync = require('child_process').execSync
execSync(`curl --output ${source} ${target}`)
execSync(`unzip -q ${target} -d ${target-dir}`)
execSync(`rm ${target}`)
I am using the *sync
version of exec
to ensure things happen in sequence. Since these are long-ish running processes (large zip archive with lots of files inside), I would like to see a progress as time goes on. I can either use something like npm progress
or, since curl
and unzip
already show progress, I wouldn't mind using just that. How do I go about achieving this?
Update: here is what I've tried so far. I can get a progress bar for the download, but I am stuck with staring at a blank screen with unzip
(which does take a long time because the archive is really large). I could simply perform unzip
without the quiet option q
but then I would get a listing of every file unzipped. I don't want that. I simply want a progress bar. I tried using node modules unzip
and others like that, but they don't work.
const spawn = require('child_process').spawn
const ProgressBar = require('progress')
const http = require('http')
const fs = require('fs')
const target = fs.createWriteStream(target_file)
const req = http.request({ hostname: hostname, path: filename })
req.on('response', function(res) {
const len = parseInt(res.headers['content-length'], 10)
const bar = new ProgressBar(`downloading ${hostname}/${filename} [:bar] :rate/bps :percent :etas`, {
complete: '=',
incomplete: ' ',
width: 20,
total: len
})
res.on('data', function (chunk) {
bar.tick(chunk.length);
target.write(chunk);
})
res.on('end', function () {
target.end(function () {
console.log(`downloaded ${len} bytes to data/${filename}`)
const unzip = spawn('unzip', ['-q', filename, '-d', target_dir])
unzip.on('close', (code) => {
console.log(`unzip exited with code ${code}`)
const rm = spawn('rm', [new_filename])
rm.on('close', (code) => {
console.log(`rm exited with code ${code}`)
})
})
})
})
}).on('error', (err) => {
console.log('Error: ', err.message)
})
req.end()