feat(unzipper): initial commit
This commit is contained in:
parent
ad731ebc29
commit
c5c8db6149
|
@ -0,0 +1 @@
|
|||
.env*
|
|
@ -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
|
||||
```
|
|
@ -0,0 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Stefan Begerad <stefan@begerad.de>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
|
@ -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.');
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue