diff --git a/unzipper/.gitignore b/unzipper/.gitignore new file mode 100644 index 0000000..81a38be --- /dev/null +++ b/unzipper/.gitignore @@ -0,0 +1 @@ +.env* diff --git a/unzipper/README.md b/unzipper/README.md new file mode 100644 index 0000000..f8a6c97 --- /dev/null +++ b/unzipper/README.md @@ -0,0 +1,21 @@ +## Overview +zip and unzip files + +## Preparation + +Run the following command in your favorite terminal to install dependenies. +``` +npm i +``` + +## Development setup + +Run the following command in your favorite terminal if you fancy log messages for debugging. +``` +export DEBUG=$DEBUG,zip,debug +``` + +Run the following command in your favorite terminal to start the service in development mode. +``` +npm run dev +``` diff --git a/unzipper/README.md.license b/unzipper/README.md.license new file mode 100644 index 0000000..2e76dc1 --- /dev/null +++ b/unzipper/README.md.license @@ -0,0 +1,5 @@ +/* + * SPDX-FileCopyrightText: 2022 Stefan Begerad + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ diff --git a/unzipper/index.js b/unzipper/index.js new file mode 100644 index 0000000..e06d237 --- /dev/null +++ b/unzipper/index.js @@ -0,0 +1,48 @@ +const debug=require('debug')('debug'); +const fs = require('fs'); +const pathInput='/home/begerad/git/sib/sandbox-node/request'; +const fileInput='connect-top-dhid-1654514173016.zip'; +debug('fileInput: '+fileInput); +const filePathInput=pathInput+'/'+fileInput; +debug('filePathInput: '+filePathInput); +const fileInputNoType=fileInput.slice(0,fileInput.length-4); +debug('fileInputNoType: '+fileInputNoType); +const pathOutput='/tmp'+'/'+fileInputNoType; +debug('pathOutput: '+pathOutput); + +//create new folder on demand +let isUnzipped=false; +try { + //check if folder already exists + if (!fs.existsSync(pathOutput)) { + fs.mkdirSync(pathOutput); + debug("Directory is created."); + } else { + debug("Directory already exists."); + isUnzipped=true; + } +} catch (err) { + debug(err); +} + +//double check if output path exists +if (fs.existsSync(pathOutput)) { + debug('Directory exists!'); +} else { + debug('Directory not found.'); +} + +if(!isUnzipped){ + debug('unzip'); + const unzipper=require('./unzipper'); + (async () => { + try { + await unzipper.unzip(filePathInput,pathOutput); + } catch (err) { + console.error(err); + } + })(); +}else{ + debug('unzip already done'); +} +debug('done.'); diff --git a/unzipper/package.json b/unzipper/package.json new file mode 100644 index 0000000..c26ced9 --- /dev/null +++ b/unzipper/package.json @@ -0,0 +1,22 @@ +{ + "private": true, + "name": "unzipper", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "dev": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Stefan Begerad", + "license": "GPL-3.0", + "devDependencies": { + "nodemon": "^2.0.7" + }, + "dependencies": { + "fs": "0.0.1-security", + "http": "0.0.1-security", + "request": "^2.88.2", + "unzipper": "^0.10.11" + } +} diff --git a/unzipper/unzipper.js b/unzipper/unzipper.js new file mode 100644 index 0000000..110bcdc --- /dev/null +++ b/unzipper/unzipper.js @@ -0,0 +1,23 @@ +const debug=require('debug')('unzipper'); +const {Parse} = require('unzipper'); +const {createWriteStream, createReadStream} = require('fs'); + +const unzip = (pathInput,pathOutput) => { + debug('pathInput: '+pathInput); + const stream=createReadStream(pathInput).pipe(Parse()); + return new Promise((resolve, reject) => { + debug('pathOutput: '+pathOutput); + stream.on('entry', (entry) => { + let file=`${pathOutput}/${entry.path}`; + debug('file: '+file); + const writeStream=createWriteStream(file); + return entry.pipe(writeStream); + }); + stream.on('finish', () => resolve()); + stream.on('error', (error) => reject(error)); + }); +}; + +module.exports={ + unzip +}