Last active
January 5, 2023 23:06
-
-
Save jbenner-radham/86ae3dfd9c666e8643d584a4be92d939 to your computer and use it in GitHub Desktop.
CLI script to unzip all zip files in a directory and extract them to another directory.
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
{ | |
"name": "unzipper", | |
"version": "0.0.0", | |
"description": "", | |
"exports": "./lib/index.js", | |
"type": "module", | |
"scripts": { | |
"start": "node ./lib/index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "James Benner <james.benner@gmail.com> (https://www.jamesbenner.com/)", | |
"license": "MIT", | |
"dependencies": { | |
"extract-zip": "^2.0.1" | |
} | |
} |
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
import extract from 'extract-zip'; | |
import fs from 'node:fs'; | |
import path from 'node:path'; | |
import process from 'node:process'; | |
if (process.argv.length < 3) { | |
console.error('Please provide a source directory argument'); | |
process.exit(1); | |
} | |
if (process.argv.length < 4) { | |
console.error('Please provide a target directory argument'); | |
process.exit(1); | |
} | |
const cwd = process.argv[2]; | |
const targetDir = process.argv[3]; | |
const zipPaths = fs.readdirSync(cwd, { withFileTypes: true }) | |
.filter(dirent => dirent.isFile()) | |
.filter(file => file.name.toLowerCase().endsWith('.zip')) | |
.map(file => path.resolve(cwd, file.name)); | |
await Promise.all(zipPaths.map(zip => extract(zip, { dir: targetDir }))); | |
console.log('All files unzipped!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment